tool: parse systemd rc versions

This commit is contained in:
Julian Stecklina 2023-11-22 19:41:56 +01:00
parent e8ba04aab7
commit dd18daae09
1 changed files with 25 additions and 24 deletions

View File

@ -56,29 +56,28 @@ impl FromStr for SystemdVersion {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let split_version = s if let Some((major_str, rc_str)) = s.split_once("-rc") {
.split('.') // A version that looks like: 253-rc2
.take(2) Ok(Self {
.map(u32::from_str) major: major_str.parse()?,
.collect::<Result<Vec<u32>, std::num::ParseIntError>>() minor: -1,
.context("Failed to parse version string into u32 vector.")?; patch: rc_str.parse()?,
})
let major = split_version } else if let Some((major_str, minor_str)) = s.split_once('.') {
.first() // A version that looks like: 253.7
.copied() Ok(Self {
.context("Failed to parse major version.")?; major: major_str.parse()?,
let minor = split_version minor: minor_str.parse()?,
.get(1) patch: 0,
.copied() })
.unwrap_or(0) } else {
.try_into() // A version that looks like: 253
.unwrap(); Ok(Self {
major: s.parse()?,
Ok(Self { minor: 0,
major, patch: 0,
minor, })
patch: 0, }
})
} }
} }
@ -102,6 +101,7 @@ mod tests {
assert_eq!(parse_version("253"), (253, 0, 0).into()); assert_eq!(parse_version("253"), (253, 0, 0).into());
assert_eq!(parse_version("252.4"), (252, 4, 0).into()); assert_eq!(parse_version("252.4"), (252, 4, 0).into());
assert_eq!(parse_version("251.11"), (251, 11, 0).into()); assert_eq!(parse_version("251.11"), (251, 11, 0).into());
assert_eq!(parse_version("251-rc7"), (251, -1, 7).into());
} }
#[test] #[test]
@ -109,6 +109,8 @@ mod tests {
assert!(parse_version("253") > parse_version("252")); assert!(parse_version("253") > parse_version("252"));
assert!(parse_version("253") > parse_version("252.4")); assert!(parse_version("253") > parse_version("252.4"));
assert!(parse_version("251.8") == parse_version("251.8")); assert!(parse_version("251.8") == parse_version("251.8"));
assert!(parse_version("251-rc5") > parse_version("251-rc4"));
assert!(parse_version("251") > parse_version("251-rc9"));
} }
#[test] #[test]
@ -116,7 +118,6 @@ mod tests {
parse_version_error(""); parse_version_error("");
parse_version_error("213;k;13"); parse_version_error("213;k;13");
parse_version_error("-1.3.123"); parse_version_error("-1.3.123");
parse_version_error("253-rc1");
} }
fn parse_version(input: &str) -> SystemdVersion { fn parse_version(input: &str) -> SystemdVersion {