Skip to content

Commit

Permalink
core: Format 0 as "00" in Id::hex_clean and Id::clean_hex_path
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Dec 17, 2024
1 parent 5c19c3a commit eadd27c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed the unused `Id::hex`.
- Deprecated `Id::hex_path` and added `Id::legacy_hex_path` as a replacement.
- Added `Id::clean_hex_path` as an alternative to `Id::legacy_hex_path`.
- Changed `Id::hex_clean` to format zero as `"00"`.

### Fixed

Expand Down
13 changes: 8 additions & 5 deletions core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ impl Id {
/// Hex representation of this ID without leading zeroes.
///
/// This implementation skips all leading bytes that are zero so that the resulting hex string
/// always has an even number of characters and does not start with more than one zero.
/// always has an even number of characters and does not start with more than one zero. 0 is
/// formatted as `"00"`.
pub fn hex_clean(&self) -> HexClean {
HexClean(self.0)
}
Expand Down Expand Up @@ -198,7 +199,8 @@ impl<'de> Deserialize<'de> for Id {
/// Hex representation of an `u128` without leading zeroes.
///
/// This implementation skips all leading bytes that are zero so that the resulting hex string
/// always has an even number of characters and does not start with more than one zero.
/// always has an even number of characters and does not start with more than one zero. 0 is
/// formatted as `"00"`.
pub struct HexClean(pub u128);

impl core::fmt::Display for HexClean {
Expand All @@ -224,8 +226,9 @@ impl<'a> HexCleanBytes<'a> {
upper: true,
}
} else {
// Format 0 as "00"
Self {
array: &[],
array: &[0],
upper: true,
}
}
Expand Down Expand Up @@ -529,7 +532,7 @@ mod tests {

#[test]
fn test_id_clean_hex_path() {
assert_eq!(Id(0).clean_hex_path().as_str(), "");
assert_eq!(Id(0).clean_hex_path().as_str(), "00");
assert_eq!(Id(1).clean_hex_path().as_str(), "01");
assert_eq!(Id(10).clean_hex_path().as_str(), "0a");
assert_eq!(Id(16).clean_hex_path().as_str(), "10");
Expand All @@ -544,7 +547,7 @@ mod tests {

#[test]
fn test_id_hex_clean() {
assert_eq!(Id(0).hex_clean().to_string(), "");
assert_eq!(Id(0).hex_clean().to_string(), "00");
assert_eq!(Id(1).hex_clean().to_string(), "01");
assert_eq!(Id(10).hex_clean().to_string(), "0a");
assert_eq!(Id(16).hex_clean().to_string(), "10");
Expand Down

0 comments on commit eadd27c

Please sign in to comment.