1pub mod comment_storage;
12pub mod html;
14#[cfg(target_arch = "wasm32")]
16pub mod js;
17#[cfg(feature = "server")]
19pub mod server;
20#[cfg(feature = "server")]
22pub mod text;
23pub mod time;
25#[cfg(target_arch = "wasm32")]
27pub mod web_upload;
28
29pub fn format_bytes(bytes: i64) -> String {
34 const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
35 let mut size = bytes as f64;
36 let mut unit = 0;
37 while size.abs() >= 1024.0 && unit < UNITS.len() - 1 {
38 size /= 1024.0;
39 unit += 1;
40 }
41 if unit == 0 {
42 format!("{} B", bytes)
43 } else {
44 format!("{size:.1} {}", UNITS[unit])
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::format_bytes;
51
52 #[test]
53 fn bytes_under_1k_render_as_integer() {
54 assert_eq!(format_bytes(0), "0 B");
55 assert_eq!(format_bytes(512), "512 B");
56 assert_eq!(format_bytes(1023), "1023 B");
57 }
58
59 #[test]
60 fn kilo_and_mega_render_with_one_decimal() {
61 assert_eq!(format_bytes(1536), "1.5 KB");
62 assert_eq!(format_bytes(1_258_291), "1.2 MB");
63 }
64
65 #[test]
66 fn giga_and_tera_render_with_one_decimal() {
67 assert_eq!(format_bytes(2 * 1024 * 1024 * 1024), "2.0 GB");
68 assert_eq!(format_bytes(3 * 1024_i64.pow(4)), "3.0 TB");
69 }
70
71 #[test]
72 fn values_stop_at_tb() {
73 assert_eq!(format_bytes(2048 * 1024_i64.pow(4)), "2048.0 TB");
75 }
76}