@@ -8,9 +8,9 @@ use serde::Serializer;
88/// Formats a byte size value into a human-readable string.
99///
1010/// The function follows these rules:
11- /// - Uses units: B, kB and MB
12- /// - Switches from B to kB at 1500 bytes
13- /// - Switches from kB to MB at 1500 * 1024 bytes
11+ /// - Uses units: B, KiB and MiB
12+ /// - Switches from B to KiB at 1500 bytes
13+ /// - Switches from KiB to MiB at 1500 * 1024 bytes
1414/// - Limits the number to a maximum of 4 characters by adjusting decimal places
1515///
1616/// # Arguments
@@ -22,7 +22,7 @@ use serde::Serializer;
2222/// A formatted string representing the size with appropriate units
2323pub fn format_bytes ( bytes : u32 ) -> String {
2424 const THRESHOLD : f64 = 1500. ;
25- const UNITS : & [ & str ] = & [ "B" , "kB " , "MB " ] ;
25+ const UNITS : & [ & str ] = & [ "B" , "KiB " , "MiB " ] ;
2626
2727 let mut value = bytes as f64 ;
2828 let mut unit_index = 0 ;
@@ -40,15 +40,15 @@ pub fn format_bytes(bytes: u32) -> String {
4040 return format ! ( "{bytes} {unit}" ) ;
4141 }
4242
43- // For kB and MB , format with appropriate decimal places
43+ // For KiB and MiB , format with appropriate decimal places
4444
4545 // Determine number of decimal places to keep number under 4 chars
4646 if value < 10.0 {
47- format ! ( "{value:.2} {unit}" ) // e.g., 1.50 kB , 9.99 MB
47+ format ! ( "{value:.2} {unit}" ) // e.g., 1.50 KiB , 9.99 MiB
4848 } else if value < 100.0 {
49- format ! ( "{value:.1} {unit}" ) // e.g., 10.5 kB , 99.9 MB
49+ format ! ( "{value:.1} {unit}" ) // e.g., 10.5 KiB , 99.9 MiB
5050 } else {
51- format ! ( "{value:.0} {unit}" ) // e.g., 100 kB , 999 MB
51+ format ! ( "{value:.0} {unit}" ) // e.g., 100 KiB , 999 MiB
5252 }
5353}
5454
@@ -128,23 +128,23 @@ mod tests {
128128 assert_eq ! ( format_bytes( 1499 ) , "1499 B" ) ;
129129
130130 // Test kilobytes format (1500 bytes to 1500 * 1024 bytes)
131- assert_eq ! ( format_bytes( 1500 ) , "1.46 kB " ) ;
132- assert_eq ! ( format_bytes( 2048 ) , "2.00 kB " ) ;
133- assert_eq ! ( format_bytes( 5120 ) , "5.00 kB " ) ;
134- assert_eq ! ( format_bytes( 10240 ) , "10.0 kB " ) ;
135- assert_eq ! ( format_bytes( 51200 ) , "50.0 kB " ) ;
136- assert_eq ! ( format_bytes( 102400 ) , "100 kB " ) ;
137- assert_eq ! ( format_bytes( 512000 ) , "500 kB " ) ;
138- assert_eq ! ( format_bytes( 1048575 ) , "1024 kB " ) ;
131+ assert_eq ! ( format_bytes( 1500 ) , "1.46 KiB " ) ;
132+ assert_eq ! ( format_bytes( 2048 ) , "2.00 KiB " ) ;
133+ assert_eq ! ( format_bytes( 5120 ) , "5.00 KiB " ) ;
134+ assert_eq ! ( format_bytes( 10240 ) , "10.0 KiB " ) ;
135+ assert_eq ! ( format_bytes( 51200 ) , "50.0 KiB " ) ;
136+ assert_eq ! ( format_bytes( 102400 ) , "100 KiB " ) ;
137+ assert_eq ! ( format_bytes( 512000 ) , "500 KiB " ) ;
138+ assert_eq ! ( format_bytes( 1048575 ) , "1024 KiB " ) ;
139139
140140 // Test megabytes format (above 1500 * 1024 bytes)
141- assert_eq ! ( format_bytes( 1536000 ) , "1.46 MB " ) ;
142- assert_eq ! ( format_bytes( 2097152 ) , "2.00 MB " ) ;
143- assert_eq ! ( format_bytes( 5242880 ) , "5.00 MB " ) ;
144- assert_eq ! ( format_bytes( 10485760 ) , "10.0 MB " ) ;
145- assert_eq ! ( format_bytes( 52428800 ) , "50.0 MB " ) ;
146- assert_eq ! ( format_bytes( 104857600 ) , "100 MB " ) ;
147- assert_eq ! ( format_bytes( 1073741824 ) , "1024 MB " ) ;
141+ assert_eq ! ( format_bytes( 1536000 ) , "1.46 MiB " ) ;
142+ assert_eq ! ( format_bytes( 2097152 ) , "2.00 MiB " ) ;
143+ assert_eq ! ( format_bytes( 5242880 ) , "5.00 MiB " ) ;
144+ assert_eq ! ( format_bytes( 10485760 ) , "10.0 MiB " ) ;
145+ assert_eq ! ( format_bytes( 52428800 ) , "50.0 MiB " ) ;
146+ assert_eq ! ( format_bytes( 104857600 ) , "100 MiB " ) ;
147+ assert_eq ! ( format_bytes( 1073741824 ) , "1024 MiB " ) ;
148148 }
149149
150150 #[ test]
0 commit comments