global: snapshot

This commit is contained in:
nym21
2025-02-28 11:52:25 +01:00
parent 5b1ca3711a
commit 1b93ccf608
35 changed files with 460 additions and 271 deletions
+25 -1
View File
@@ -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)
}
}