From 0482880211c0ef151441c15d9464be0e693930e1 Mon Sep 17 00:00:00 2001 From: Sean Klein Date: Wed, 8 Jun 2022 10:06:35 -0400 Subject: [PATCH 1/3] Implement a user-friendly display for bytecount --- common/src/api/external/mod.rs | 47 +++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/common/src/api/external/mod.rs b/common/src/api/external/mod.rs index 2f74c8e559..721ea437fa 100644 --- a/common/src/api/external/mod.rs +++ b/common/src/api/external/mod.rs @@ -336,33 +336,54 @@ impl JsonSchema for RoleName { #[derive(Copy, Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)] pub struct ByteCount(u64); +const KB: u64 = 1024; +const MB: u64 = KB * 1024; +const GB: u64 = MB * 1024; +const TB: u64 = GB * 1024; + impl ByteCount { pub fn from_kibibytes_u32(kibibytes: u32) -> ByteCount { - ByteCount::try_from(1024 * u64::from(kibibytes)).unwrap() + ByteCount::try_from(KB * u64::from(kibibytes)).unwrap() } pub fn from_mebibytes_u32(mebibytes: u32) -> ByteCount { - ByteCount::try_from(1024 * 1024 * u64::from(mebibytes)).unwrap() + ByteCount::try_from(MB * u64::from(mebibytes)).unwrap() } pub fn from_gibibytes_u32(gibibytes: u32) -> ByteCount { - ByteCount::try_from(1024 * 1024 * 1024 * u64::from(gibibytes)).unwrap() + ByteCount::try_from(GB * u64::from(gibibytes)).unwrap() } pub fn to_bytes(&self) -> u64 { self.0 } pub fn to_whole_kibibytes(&self) -> u64 { - self.to_bytes() / 1024 + self.to_bytes() / KB } pub fn to_whole_mebibytes(&self) -> u64 { - self.to_bytes() / 1024 / 1024 + self.to_bytes() / MB } pub fn to_whole_gibibytes(&self) -> u64 { - self.to_bytes() / 1024 / 1024 / 1024 + self.to_bytes() / GB } pub fn to_whole_tebibytes(&self) -> u64 { - self.to_bytes() / 1024 / 1024 / 1024 / 1024 + self.to_bytes() / TB + } +} + +impl Display for ByteCount { + fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult { + if self.to_bytes() >= TB && self.to_bytes() % TB == 0 { + write!(f, "{} TB", self.to_whole_tebibytes()) + } else if self.to_bytes() >= GB && self.to_bytes() % GB == 0 { + write!(f, "{} GB", self.to_whole_gibibytes()) + } else if self.to_bytes() >= MB && self.to_bytes() % MB == 0 { + write!(f, "{} MB", self.to_whole_mebibytes()) + } else if self.to_bytes() >= KB && self.to_bytes() % KB == 0 { + write!(f, "{} KB", self.to_whole_kibibytes()) + } else { + write!(f, "{} B", self.to_bytes()) + } } } @@ -2083,6 +2104,18 @@ mod test { assert_eq!(3, tib3.to_whole_tebibytes()); } + #[test] + fn test_bytecount_display() { + assert_eq!(format!("{}", ByteCount::from(0u32)), "0 B".to_string()); + assert_eq!(format!("{}", ByteCount::from(1023)), "1023 B".to_string()); + assert_eq!(format!("{}", ByteCount::from(1024)), "1 KB".to_string()); + assert_eq!(format!("{}", ByteCount::from(1025)), "1025 B".to_string()); + assert_eq!(format!("{}", ByteCount::from(1024 * 100)), "100 KB".to_string()); + assert_eq!(format!("{}", ByteCount::from_mebibytes_u32(1)), "1 MB".to_string()); + assert_eq!(format!("{}", ByteCount::from_gibibytes_u32(1)), "1 GB".to_string()); + assert_eq!(format!("{}", ByteCount::from_gibibytes_u32(1024)), "1 TB".to_string()); + } + #[test] fn test_ip_port_range_from_str() { assert_eq!( From caa09923cc89a84f3a2b99450461803b91fc8180 Mon Sep 17 00:00:00 2001 From: Sean Klein Date: Wed, 8 Jun 2022 10:08:02 -0400 Subject: [PATCH 2/3] fmt --- common/src/api/external/mod.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/common/src/api/external/mod.rs b/common/src/api/external/mod.rs index 721ea437fa..3558541f01 100644 --- a/common/src/api/external/mod.rs +++ b/common/src/api/external/mod.rs @@ -374,13 +374,13 @@ impl ByteCount { impl Display for ByteCount { fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult { if self.to_bytes() >= TB && self.to_bytes() % TB == 0 { - write!(f, "{} TB", self.to_whole_tebibytes()) + write!(f, "{} TiB", self.to_whole_tebibytes()) } else if self.to_bytes() >= GB && self.to_bytes() % GB == 0 { - write!(f, "{} GB", self.to_whole_gibibytes()) + write!(f, "{} GiB", self.to_whole_gibibytes()) } else if self.to_bytes() >= MB && self.to_bytes() % MB == 0 { - write!(f, "{} MB", self.to_whole_mebibytes()) + write!(f, "{} MiB", self.to_whole_mebibytes()) } else if self.to_bytes() >= KB && self.to_bytes() % KB == 0 { - write!(f, "{} KB", self.to_whole_kibibytes()) + write!(f, "{} KiB", self.to_whole_kibibytes()) } else { write!(f, "{} B", self.to_bytes()) } @@ -2108,12 +2108,24 @@ mod test { fn test_bytecount_display() { assert_eq!(format!("{}", ByteCount::from(0u32)), "0 B".to_string()); assert_eq!(format!("{}", ByteCount::from(1023)), "1023 B".to_string()); - assert_eq!(format!("{}", ByteCount::from(1024)), "1 KB".to_string()); + assert_eq!(format!("{}", ByteCount::from(1024)), "1 KiB".to_string()); assert_eq!(format!("{}", ByteCount::from(1025)), "1025 B".to_string()); - assert_eq!(format!("{}", ByteCount::from(1024 * 100)), "100 KB".to_string()); - assert_eq!(format!("{}", ByteCount::from_mebibytes_u32(1)), "1 MB".to_string()); - assert_eq!(format!("{}", ByteCount::from_gibibytes_u32(1)), "1 GB".to_string()); - assert_eq!(format!("{}", ByteCount::from_gibibytes_u32(1024)), "1 TB".to_string()); + assert_eq!( + format!("{}", ByteCount::from(1024 * 100)), + "100 KiB".to_string() + ); + assert_eq!( + format!("{}", ByteCount::from_mebibytes_u32(1)), + "1 MiB".to_string() + ); + assert_eq!( + format!("{}", ByteCount::from_gibibytes_u32(1)), + "1 GiB".to_string() + ); + assert_eq!( + format!("{}", ByteCount::from_gibibytes_u32(1024)), + "1 TiB".to_string() + ); } #[test] From dff7b32d49c9e96527f47a19a7692e7f6c6417f1 Mon Sep 17 00:00:00 2001 From: Sean Klein Date: Wed, 8 Jun 2022 13:15:09 -0400 Subject: [PATCH 3/3] Update constant names --- common/src/api/external/mod.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/common/src/api/external/mod.rs b/common/src/api/external/mod.rs index 3558541f01..bd64e78630 100644 --- a/common/src/api/external/mod.rs +++ b/common/src/api/external/mod.rs @@ -336,50 +336,54 @@ impl JsonSchema for RoleName { #[derive(Copy, Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)] pub struct ByteCount(u64); -const KB: u64 = 1024; -const MB: u64 = KB * 1024; -const GB: u64 = MB * 1024; -const TB: u64 = GB * 1024; +#[allow(non_upper_case_globals)] +const KiB: u64 = 1024; +#[allow(non_upper_case_globals)] +const MiB: u64 = KiB * 1024; +#[allow(non_upper_case_globals)] +const GiB: u64 = MiB * 1024; +#[allow(non_upper_case_globals)] +const TiB: u64 = GiB * 1024; impl ByteCount { pub fn from_kibibytes_u32(kibibytes: u32) -> ByteCount { - ByteCount::try_from(KB * u64::from(kibibytes)).unwrap() + ByteCount::try_from(KiB * u64::from(kibibytes)).unwrap() } pub fn from_mebibytes_u32(mebibytes: u32) -> ByteCount { - ByteCount::try_from(MB * u64::from(mebibytes)).unwrap() + ByteCount::try_from(MiB * u64::from(mebibytes)).unwrap() } pub fn from_gibibytes_u32(gibibytes: u32) -> ByteCount { - ByteCount::try_from(GB * u64::from(gibibytes)).unwrap() + ByteCount::try_from(GiB * u64::from(gibibytes)).unwrap() } pub fn to_bytes(&self) -> u64 { self.0 } pub fn to_whole_kibibytes(&self) -> u64 { - self.to_bytes() / KB + self.to_bytes() / KiB } pub fn to_whole_mebibytes(&self) -> u64 { - self.to_bytes() / MB + self.to_bytes() / MiB } pub fn to_whole_gibibytes(&self) -> u64 { - self.to_bytes() / GB + self.to_bytes() / GiB } pub fn to_whole_tebibytes(&self) -> u64 { - self.to_bytes() / TB + self.to_bytes() / TiB } } impl Display for ByteCount { fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult { - if self.to_bytes() >= TB && self.to_bytes() % TB == 0 { + if self.to_bytes() >= TiB && self.to_bytes() % TiB == 0 { write!(f, "{} TiB", self.to_whole_tebibytes()) - } else if self.to_bytes() >= GB && self.to_bytes() % GB == 0 { + } else if self.to_bytes() >= GiB && self.to_bytes() % GiB == 0 { write!(f, "{} GiB", self.to_whole_gibibytes()) - } else if self.to_bytes() >= MB && self.to_bytes() % MB == 0 { + } else if self.to_bytes() >= MiB && self.to_bytes() % MiB == 0 { write!(f, "{} MiB", self.to_whole_mebibytes()) - } else if self.to_bytes() >= KB && self.to_bytes() % KB == 0 { + } else if self.to_bytes() >= KiB && self.to_bytes() % KiB == 0 { write!(f, "{} KiB", self.to_whole_kibibytes()) } else { write!(f, "{} B", self.to_bytes())