Skip to content

Commit 57b45a7

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 71d5a99 commit 57b45a7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

CHANGELOG.md

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

2124
## uefi-macros - [Unreleased]
2225

src/table/revision.rs

Lines changed: 37 additions & 4 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,11 +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-
write!(f, "{}.{}.{}", major, minor / 10, minor % 10)
80+
81+
if major < 2 {
82+
write!(f, "{}.{:02}", major, minor)
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+
}
5891
}
5992
}
6093

0 commit comments

Comments
 (0)