#[inline]
pub const fn kb(n: usize) -> usize {
n * 1024
}
#[inline]
pub const fn mb(n: usize) -> usize {
n * 1024 * 1024
}
#[inline]
pub const fn gb(n: usize) -> usize {
n * 1024 * 1024 * 1024
}
pub fn format_bytes(bytes: usize) -> String {
const KB: usize = 1024;
const MB: usize = KB * 1024;
const GB: usize = MB * 1024;
if bytes >= GB {
format!("{:.2} GB", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB", bytes as f64 / KB as f64)
} else {
format!("{} B", bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_size_conversions() {
assert_eq!(kb(1), 1024);
assert_eq!(mb(1), 1024 * 1024);
assert_eq!(gb(1), 1024 * 1024 * 1024);
}
#[test]
fn test_format_bytes() {
assert_eq!(format_bytes(500), "500 B");
assert_eq!(format_bytes(1024), "1.00 KB");
assert_eq!(format_bytes(1024 * 1024), "1.00 MB");
assert_eq!(format_bytes(1024 * 1024 * 1024), "1.00 GB");
}
}