pub trait ByteSize {
fn format_size(&self) -> String;
}
impl ByteSize for u64 {
fn format_size(&self) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;
const TB: u64 = GB * 1024;
match self {
bytes if *bytes >= TB => format!("{:.2} TB", *bytes as f64 / TB as f64),
bytes if *bytes >= GB => format!("{:.2} GB", *bytes as f64 / GB as f64),
bytes if *bytes >= MB => format!("{:.2} MB", *bytes as f64 / MB as f64),
bytes if *bytes >= KB => format!("{:.2} KB", *bytes as f64 / KB as f64),
bytes => format!("{} bytes", bytes),
}
}
}