diff --git a/src/main.rs b/src/main.rs index ba59ad3..3df57db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: EUPL-1.2 mod settings; +mod sizes; use { anyhow::Result, @@ -143,7 +144,7 @@ async fn s3_fileview(path: &PathBuf, settings: &Settings) -> Result Result String { - use human_size::{Any, SpecificSize}; - - let size: f64 = bytes as f64; - let digits = size.log10().floor() as u32; - let mut order = digits / 3; - let unit = match order { - 0 => Any::Byte, - 1 => Any::Kilobyte, - 2 => Any::Megabyte, - _ => { - order = 3; // Let's stop here. - Any::Gigabyte - } - }; - - format!( - "{:.3}", - SpecificSize::new(size / 10u64.pow(order * 3) as f64, unit) - .unwrap_or(SpecificSize::new(0., Any::Byte).unwrap()) - ) -} - lazy_static! { // Workaround for https://github.com/SergioBenitez/Rocket/issues/1792 static ref EMPTY_DIR: tempfile::TempDir = tempfile::tempdir() @@ -203,22 +181,3 @@ fn rocket() -> _ { .unwrap() })) } - -// ------------------------------------------------------------- - -#[cfg(test)] -mod tests { - use rstest::rstest; - - #[rstest] - #[case(1024, "1.024 kB")] - #[case(10240, "10.240 kB")] - #[case(1024*1024, "1.049 MB")] - #[case(1024*1024*1024, "1.074 GB")] - #[case(0, "0.000 B")] - #[case(u64::MAX, format!("{:.3} GB",u64::MAX as f64/(1_000_000_000.0)))] - #[case(u64::MIN, format!("{:.3} B",u64::MIN as f64))] - fn size_bytes_to_human(#[case] bytes: u64, #[case] expected: String) { - assert_eq!(super::size_bytes_to_human(bytes), expected); - } -} diff --git a/src/sizes.rs b/src/sizes.rs new file mode 100644 index 0000000..8d80287 --- /dev/null +++ b/src/sizes.rs @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: © Matteo Settenvini +// SPDX-License-Identifier: EUPL-1.2 + +pub fn bytes_to_human(bytes: u64) -> String { + use human_size::{Any, SpecificSize}; + + let size: f64 = bytes as f64; + let digits = size.log10().floor() as u32; + let mut order = digits / 3; + let unit = match order { + 0 => Any::Byte, + 1 => Any::Kilobyte, + 2 => Any::Megabyte, + _ => { + order = 3; // Let's stop here. + Any::Gigabyte + } + }; + + format!( + "{:.3}", + SpecificSize::new(size / 10u64.pow(order * 3) as f64, unit) + .unwrap_or(SpecificSize::new(0., Any::Byte).unwrap()) + ) +} + +// ------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use rstest::rstest; + + #[rstest] + #[case(1024, "1.024 kB")] + #[case(10240, "10.240 kB")] + #[case(1024*1024, "1.049 MB")] + #[case(1024*1024*1024, "1.074 GB")] + #[case(0, "0.000 B")] + #[case(u64::MAX, format!("{:.3} GB",u64::MAX as f64/(1_000_000_000.0)))] + #[case(u64::MIN, format!("{:.3} B",u64::MIN as f64))] + fn bytes_to_human(#[case] bytes: u64, #[case] expected: String) { + assert_eq!(super::bytes_to_human(bytes), expected); + } +}