simd_r_drive/utils/format_bytes.rs
1/// Converts a file size in bytes into a human-readable format.
2///
3/// This function formats byte sizes dynamically into **KB, MB, or GB** for readability.
4///
5/// # Conversion Logic:
6/// - **1,024 bytes → KB**
7/// - **1,024 KB → MB**
8/// - **1,024 MB → GB**
9///
10/// # Formatting:
11/// - Uses **two decimal places** for precision (e.g., `"10.43 MB"`).
12/// - If size is below 1 KB, it is displayed in **raw bytes** (e.g., `"512 bytes"`).
13///
14/// # Parameters:
15/// - `bytes`: The size in bytes to format.
16///
17/// # Returns:
18/// - A `String` representing the human-readable file size.
19pub fn format_bytes(bytes: u64) -> String {
20 const KB: u64 = 1024;
21 const MB: u64 = KB * 1024;
22 const GB: u64 = MB * 1024;
23
24 match bytes {
25 b if b >= GB => format!("{:.2} GB", b as f64 / GB as f64),
26 b if b >= MB => format!("{:.2} MB", b as f64 / MB as f64),
27 b if b >= KB => format!("{:.2} KB", b as f64 / KB as f64),
28 _ => format!("{bytes} bytes"),
29 }
30}