ferron_common/util/
sizify.rs

1// Sizify function taken from SVR.JS and rewritten from JavaScript to Rust
2// SVR.JS is licensed under MIT, so below is the copyright notice:
3//
4// Copyright (c) 2018-2025 SVR.JS
5// Portions of this file are derived from SVR.JS (https://git.svrjs.org/svrjs/svrjs).
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17
18/// Converts the file size into a human-readable one
19pub fn sizify(bytes: u64, add_i: bool) -> String {
20  if bytes == 0 {
21    return "0".to_string();
22  }
23
24  let prefixes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"];
25  let prefix_index = ((bytes as f64).log2() / 10.0).floor().min(prefixes.len() as f64 - 1.0) as usize;
26  let prefix_index_translated = 2_i64.pow(10 * prefix_index as u32);
27  let decimal_points = ((2.0 - (bytes as f64 / prefix_index_translated as f64).log10().floor()) as i32).max(0);
28
29  let size = ((bytes as f64 / prefix_index_translated as f64) * 10_f64.powi(decimal_points)).ceil()
30    / 10_f64.powi(decimal_points);
31  let prefix = prefixes[prefix_index];
32  let suffix = if prefix_index > 0 && add_i { "i" } else { "" };
33
34  format!("{size}{prefix}{suffix}")
35}
36
37#[cfg(test)]
38mod tests {
39  use super::*;
40
41  #[test]
42  fn test_sizify_zero_bytes() {
43    assert_eq!(sizify(0, false), "0");
44  }
45
46  #[test]
47  fn test_sizify_small_values() {
48    assert_eq!(sizify(1000, false), "1000");
49    assert_eq!(sizify(1024, false), "1K");
50  }
51
52  #[test]
53  fn test_sizify_larger_values() {
54    assert_eq!(sizify(1048576, false), "1M");
55    assert_eq!(sizify(1073741824, false), "1G");
56    assert_eq!(sizify(1099511627776, false), "1T");
57    assert_eq!(sizify(1125899906842624, false), "1P");
58    assert_eq!(sizify(1152921504606846976, false), "1E");
59  }
60
61  #[test]
62  fn test_sizify_add_i_suffix() {
63    assert_eq!(sizify(1024, true), "1Ki");
64    assert_eq!(sizify(1048576, true), "1Mi");
65    assert_eq!(sizify(1073741824, true), "1Gi");
66  }
67
68  #[test]
69  fn test_sizify_no_i_suffix() {
70    assert_eq!(sizify(1024, false), "1K");
71    assert_eq!(sizify(1048576, false), "1M");
72    assert_eq!(sizify(1073741824, false), "1G");
73  }
74
75  #[test]
76  fn test_sizify_decimal_points() {
77    assert_eq!(sizify(1500, false), "1.47K");
78    assert_eq!(sizify(1500000, false), "1.44M");
79    assert_eq!(sizify(1500000000, false), "1.4G");
80  }
81
82  #[test]
83  fn test_sizify_edge_cases() {
84    assert_eq!(sizify(1, false), "1");
85    assert_eq!(sizify(1023, false), "1023");
86    assert_eq!(sizify(1025, false), "1.01K");
87  }
88}