Skip to content

Commit 7018168

Browse files
committed
bls_config: use crate to compare UAPI versions
Default string comparisons are wrong for this. Signed-off-by: Colin Walters <[email protected]>
1 parent cbb6eaa commit 7018168

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ serde_ignored = "0.1.10"
6464
serde_yaml = "0.9.34"
6565
tini = "1.3.0"
6666
uuid = { version = "1.8.0", features = ["v4"] }
67+
uapi-version = "0.4.0"
6768

6869
[dev-dependencies]
6970
similar-asserts = { workspace = true }

crates/lib/src/parsers/bls_config.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use anyhow::{anyhow, Result};
66
use std::collections::HashMap;
77
use std::fmt::Display;
8+
use uapi_version::Version;
89

910
/// Represents a single Boot Loader Specification config file.
1011
///
@@ -18,7 +19,9 @@ pub(crate) struct BLSConfig {
1819
pub(crate) title: Option<String>,
1920
/// The version of the boot entry.
2021
/// See <https://uapi-group.org/specifications/specs/version_format_specification/>
21-
pub(crate) version: String,
22+
///
23+
/// This is hidden and must be accessed via [`Self::version()`];
24+
version: String,
2225
/// The path to the linux kernel to boot.
2326
pub(crate) linux: String,
2427
/// The paths to the initrd images.
@@ -63,8 +66,7 @@ impl Ord for BLSConfig {
6366
}
6467

6568
// Finally, sort by version in descending order.
66-
// FIXME: This should use <https://uapi-group.org/specifications/specs/version_format_specification/>
67-
self.version.cmp(&other.version).reverse()
69+
self.version().cmp(&other.version()).reverse()
6870
}
6971
}
7072

@@ -97,6 +99,12 @@ impl Display for BLSConfig {
9799
}
98100
}
99101

102+
impl BLSConfig {
103+
pub(crate) fn version(&self) -> Version {
104+
Version::from(&self.version)
105+
}
106+
}
107+
100108
pub(crate) fn parse_bls_config(input: &str) -> Result<BLSConfig> {
101109
let mut title = None;
102110
let mut version = None;
@@ -394,4 +402,31 @@ mod tests {
394402
assert!(config1 > config2);
395403
Ok(())
396404
}
405+
406+
#[test]
407+
fn test_ordering_by_nontrivial_version() -> Result<()> {
408+
let config_final = parse_bls_config(
409+
r#"
410+
title Entry 1
411+
version 1.0
412+
linux /vmlinuz-1
413+
initrd /initrd-1
414+
"#,
415+
)?;
416+
417+
let config_rc1 = parse_bls_config(
418+
r#"
419+
title Entry 2
420+
version 1.0~rc1
421+
linux /vmlinuz-2
422+
initrd /initrd-2
423+
"#,
424+
)?;
425+
426+
// In a sorted list, we want 1.0 to appear before 1.0~rc1 because
427+
// versions are sorted descending. This means that in Rust's sort order,
428+
// config_final should be "less than" config_rc1.
429+
assert!(config_final < config_rc1);
430+
Ok(())
431+
}
397432
}

0 commit comments

Comments
 (0)