Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions crates/crates_io_og_image/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use serde::Serializer;
/// Formats a byte size value into a human-readable string.
///
/// The function follows these rules:
/// - Uses units: B, kB and MB
/// - Switches from B to kB at 1500 bytes
/// - Switches from kB to MB at 1500 * 1024 bytes
/// - Uses units: B, KiB and MiB
/// - Switches from B to KiB at 1500 bytes
/// - Switches from KiB to MiB at 1500 * 1024 bytes
/// - Limits the number to a maximum of 4 characters by adjusting decimal places
///
/// # Arguments
Expand All @@ -22,7 +22,7 @@ use serde::Serializer;
/// A formatted string representing the size with appropriate units
pub fn format_bytes(bytes: u32) -> String {
const THRESHOLD: f64 = 1500.;
const UNITS: &[&str] = &["B", "kB", "MB"];
const UNITS: &[&str] = &["B", "KiB", "MiB"];

let mut value = bytes as f64;
let mut unit_index = 0;
Expand All @@ -40,15 +40,15 @@ pub fn format_bytes(bytes: u32) -> String {
return format!("{bytes} {unit}");
}

// For kB and MB, format with appropriate decimal places
// For KiB and MiB, format with appropriate decimal places

// Determine number of decimal places to keep number under 4 chars
if value < 10.0 {
format!("{value:.2} {unit}") // e.g., 1.50 kB, 9.99 MB
format!("{value:.2} {unit}") // e.g., 1.50 KiB, 9.99 MiB
} else if value < 100.0 {
format!("{value:.1} {unit}") // e.g., 10.5 kB, 99.9 MB
format!("{value:.1} {unit}") // e.g., 10.5 KiB, 99.9 MiB
} else {
format!("{value:.0} {unit}") // e.g., 100 kB, 999 MB
format!("{value:.0} {unit}") // e.g., 100 KiB, 999 MiB
}
}

Expand All @@ -73,7 +73,7 @@ pub fn serialize_bytes<S: Serializer>(bytes: &u32, serializer: S) -> Result<S::O
/// A formatted string representing the number with appropriate suffixes
pub fn format_number(number: u32) -> String {
const THRESHOLD: f64 = 1500.;
const UNITS: &[&str] = &["", "k", "M"];
const UNITS: &[&str] = &["", "K", "M"];

let mut value = number as f64;
let mut unit_index = 0;
Expand Down Expand Up @@ -128,23 +128,23 @@ mod tests {
assert_eq!(format_bytes(1499), "1499 B");

// Test kilobytes format (1500 bytes to 1500 * 1024 bytes)
assert_eq!(format_bytes(1500), "1.46 kB");
assert_eq!(format_bytes(2048), "2.00 kB");
assert_eq!(format_bytes(5120), "5.00 kB");
assert_eq!(format_bytes(10240), "10.0 kB");
assert_eq!(format_bytes(51200), "50.0 kB");
assert_eq!(format_bytes(102400), "100 kB");
assert_eq!(format_bytes(512000), "500 kB");
assert_eq!(format_bytes(1048575), "1024 kB");
assert_eq!(format_bytes(1500), "1.46 KiB");
assert_eq!(format_bytes(2048), "2.00 KiB");
assert_eq!(format_bytes(5120), "5.00 KiB");
assert_eq!(format_bytes(10240), "10.0 KiB");
assert_eq!(format_bytes(51200), "50.0 KiB");
assert_eq!(format_bytes(102400), "100 KiB");
assert_eq!(format_bytes(512000), "500 KiB");
assert_eq!(format_bytes(1048575), "1024 KiB");

// Test megabytes format (above 1500 * 1024 bytes)
assert_eq!(format_bytes(1536000), "1.46 MB");
assert_eq!(format_bytes(2097152), "2.00 MB");
assert_eq!(format_bytes(5242880), "5.00 MB");
assert_eq!(format_bytes(10485760), "10.0 MB");
assert_eq!(format_bytes(52428800), "50.0 MB");
assert_eq!(format_bytes(104857600), "100 MB");
assert_eq!(format_bytes(1073741824), "1024 MB");
assert_eq!(format_bytes(1536000), "1.46 MiB");
assert_eq!(format_bytes(2097152), "2.00 MiB");
assert_eq!(format_bytes(5242880), "5.00 MiB");
assert_eq!(format_bytes(10485760), "10.0 MiB");
assert_eq!(format_bytes(52428800), "50.0 MiB");
assert_eq!(format_bytes(104857600), "100 MiB");
assert_eq!(format_bytes(1073741824), "1024 MiB");
}

#[test]
Expand All @@ -156,14 +156,14 @@ mod tests {
assert_eq!(format_number(1499), "1499");

// Test numbers with k suffix (1500 to 1500 * 1000)
assert_eq!(format_number(1500), "1.5k");
assert_eq!(format_number(2000), "2.0k");
assert_eq!(format_number(5000), "5.0k");
assert_eq!(format_number(10000), "10k");
assert_eq!(format_number(50000), "50k");
assert_eq!(format_number(100000), "100k");
assert_eq!(format_number(500000), "500k");
assert_eq!(format_number(999999), "1000k");
assert_eq!(format_number(1500), "1.5K");
assert_eq!(format_number(2000), "2.0K");
assert_eq!(format_number(5000), "5.0K");
assert_eq!(format_number(10000), "10K");
assert_eq!(format_number(50000), "50K");
assert_eq!(format_number(100000), "100K");
assert_eq!(format_number(500000), "500K");
assert_eq!(format_number(999999), "1000K");

// Test numbers with M suffix (above 1500 * 1000)
assert_eq!(format_number(1500000), "1.5M");
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.