mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-03 07:14:01 -07:00
global: snapshot
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Read},
|
||||
ops::Add,
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::Error;
|
||||
use crate::{Error, Result};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromBytes, IntoBytes, Immutable, KnownLayout)]
|
||||
pub struct Version(u32);
|
||||
@@ -19,6 +20,22 @@ impl Version {
|
||||
pub fn swap_bytes(self) -> Self {
|
||||
Self(self.0.swap_bytes())
|
||||
}
|
||||
|
||||
pub fn validate(&self, path: &Path) -> Result<()> {
|
||||
if let Ok(prev_version) = Version::try_from(path) {
|
||||
if prev_version != *self {
|
||||
if prev_version.swap_bytes() == *self {
|
||||
return Err(Error::WrongEndian);
|
||||
}
|
||||
return Err(Error::DifferentVersion {
|
||||
found: prev_version,
|
||||
expected: *self,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for Version {
|
||||
@@ -35,3 +52,10 @@ impl TryFrom<&Path> for Version {
|
||||
Ok(*(Self::ref_from_bytes(&buf)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Version> for Version {
|
||||
type Output = Version;
|
||||
fn add(self, rhs: Version) -> Self::Output {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user