Skip to content

Commit

Permalink
Improve error handling for old Peppi files
Browse files Browse the repository at this point in the history
  • Loading branch information
hohav committed Feb 11, 2024
1 parent 9231050 commit 57c01c0
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/io/peppi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@ pub use de::read;
pub use ser::write;

/// Current version of the Peppi format
pub const CURRENT_VERSION: Version = Version(2, 0);
pub const CURRENT_VERSION: Version = Version(2, 0, 0);

/// Peppi files are TAR archives, and are guaranteed to start with `peppi.json`
/// Minimum supported version of the Peppi format for reading
pub const MIN_VERSION: Version = Version(2, 0, 0);

/// Peppi files are TAR archives, guaranteed to start with `peppi.json`
pub const FILE_SIGNATURE: [u8; 10] = [0x70, 0x65, 0x70, 0x70, 0x69, 0x2e, 0x6a, 0x73, 0x6f, 0x6e];

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
pub struct Version(pub u8, pub u8);
pub struct Version(pub u8, pub u8, pub u8);

impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}", self.0, self.1)
write!(f, "{}.{}.{}", self.0, self.1, self.2)
}
}

impl str::FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let mut i = s.split('.');
match (i.next(), i.next(), i.next()) {
(Some(major), Some(minor), None) => Ok(Version(parse_u8(major)?, parse_u8(minor)?)),
match (i.next(), i.next(), i.next(), i.next()) {
(Some(major), Some(minor), Some(revision), None) => Ok(Version(
parse_u8(major)?,
parse_u8(minor)?,
parse_u8(revision)?,
)),
_ => Err(err!("invalid Peppi version: {}", s.to_string())),
}
}
Expand Down Expand Up @@ -60,13 +67,9 @@ pub struct Peppi {
}

pub fn assert_current_version(version: Version) -> Result<()> {
if version == CURRENT_VERSION {
Ok(())
if version < MIN_VERSION {
Err(err!("unsupported version ({} < {})", version, MIN_VERSION))
} else {
Err(err!(
"unsupported version ({} != {})",
version,
CURRENT_VERSION
))
Ok(())
}
}

0 comments on commit 57c01c0

Please sign in to comment.