Skip to content

Commit ad01c57

Browse files
Rework Revision formatting
Add a `Display` impl that correctly formats the revision for all spec versions. There are docstring tests that demonstrate it in action. Also removed the `Debug` impl and replaced with a derived impl.
1 parent e6d6d58 commit ad01c57

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
may not have the same semantics as the UEFI revision.
2020
- Changed `Revision` to `repr(transparent)`.
2121
- Add `Revision::EFI_2_100` constant.
22+
- The `Revision` type now implements `Display` with correct formatting
23+
for all UEFI versions. The custom `Debug` impl has been removed and
24+
replaced with a derived `Debug` impl.
2225

2326
## uefi-macros - [Unreleased]
2427

src/table/revision.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,31 @@ use core::fmt;
66
///
77
/// The minor revision number is incremented on minor changes,
88
/// it is stored as a two-digit binary-coded decimal.
9-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
9+
///
10+
/// # Display format
11+
///
12+
/// For major revision 2 and later, if the lower minor digit is zero,
13+
/// the revision is formatted as "major.minor-upper". Otherwise it's
14+
/// formatted as "major.minor-upper.minor-lower". This format is
15+
/// described in the "EFI System Table" section of the UEFI
16+
/// Specification.
17+
///
18+
/// Prior to major version 2, the revision is always formatted as
19+
/// "major.minor", with minor left-padded with zero if minor-upper is
20+
/// zero.
21+
///
22+
/// Examples:
23+
///
24+
/// ```
25+
/// use uefi::table::Revision;
26+
/// assert_eq!(Revision::EFI_1_02.to_string(), "1.02");
27+
/// assert_eq!(Revision::EFI_1_10.to_string(), "1.10");
28+
/// assert_eq!(Revision::EFI_2_00.to_string(), "2.0");
29+
/// assert_eq!(Revision::EFI_2_30.to_string(), "2.3");
30+
/// assert_eq!(Revision::EFI_2_31.to_string(), "2.3.1");
31+
/// assert_eq!(Revision::EFI_2_100.to_string(), "2.10");
32+
/// ```
33+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
1034
#[repr(transparent)]
1135
pub struct Revision(u32);
1236

@@ -50,13 +74,20 @@ impl Revision {
5074
}
5175
}
5276

53-
impl fmt::Debug for Revision {
54-
/// Formats the revision in the `major.minor.patch` format.
77+
impl fmt::Display for Revision {
5578
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5679
let (major, minor) = (self.major(), self.minor());
57-
let minor = minor / 10;
58-
let patch = minor % 10;
59-
write!(f, "{major}.{minor}.{patch}")
80+
81+
if major < 2 {
82+
write!(f, "{major}.{minor:02}")
83+
} else {
84+
let (minor, patch) = (minor / 10, minor % 10);
85+
if patch == 0 {
86+
write!(f, "{major}.{minor}")
87+
} else {
88+
write!(f, "{major}.{minor}.{patch}")
89+
}
90+
}
6091
}
6192
}
6293

0 commit comments

Comments
 (0)