@@ -8,9 +8,9 @@ use serde::Serializer;
8
8
/// Formats a byte size value into a human-readable string.
9
9
///
10
10
/// 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
14
14
/// - Limits the number to a maximum of 4 characters by adjusting decimal places
15
15
///
16
16
/// # Arguments
@@ -22,7 +22,7 @@ use serde::Serializer;
22
22
/// A formatted string representing the size with appropriate units
23
23
pub fn format_bytes ( bytes : u32 ) -> String {
24
24
const THRESHOLD : f64 = 1500. ;
25
- const UNITS : & [ & str ] = & [ "B" , "kB " , "MB " ] ;
25
+ const UNITS : & [ & str ] = & [ "B" , "KiB " , "MiB " ] ;
26
26
27
27
let mut value = bytes as f64 ;
28
28
let mut unit_index = 0 ;
@@ -40,15 +40,15 @@ pub fn format_bytes(bytes: u32) -> String {
40
40
return format ! ( "{bytes} {unit}" ) ;
41
41
}
42
42
43
- // For kB and MB , format with appropriate decimal places
43
+ // For KiB and MiB , format with appropriate decimal places
44
44
45
45
// Determine number of decimal places to keep number under 4 chars
46
46
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
48
48
} 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
50
50
} else {
51
- format ! ( "{value:.0} {unit}" ) // e.g., 100 kB , 999 MB
51
+ format ! ( "{value:.0} {unit}" ) // e.g., 100 KiB , 999 MiB
52
52
}
53
53
}
54
54
@@ -128,23 +128,23 @@ mod tests {
128
128
assert_eq ! ( format_bytes( 1499 ) , "1499 B" ) ;
129
129
130
130
// 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 " ) ;
139
139
140
140
// 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 " ) ;
148
148
}
149
149
150
150
#[ test]
0 commit comments