| 1 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] |
| 2 | pub enum DataUnit { |
| 3 | Byte, |
| 4 | #[default] |
| 5 | Bit, |
| 6 | } |
| 7 | |
| 8 | pub const KILO_LIMIT: u64 = 1000; |
| 9 | pub const MEGA_LIMIT: u64 = 1_000_000; |
| 10 | pub const GIGA_LIMIT: u64 = 1_000_000_000; |
| 11 | pub const TERA_LIMIT: u64 = 1_000_000_000_000; |
| 12 | pub const KIBI_LIMIT: u64 = 1024; |
| 13 | pub const MEBI_LIMIT: u64 = 1024 * 1024; |
| 14 | pub const GIBI_LIMIT: u64 = 1024 * 1024 * 1024; |
| 15 | pub const TEBI_LIMIT: u64 = 1024 * 1024 * 1024 * 1024; |
| 16 | |
| 17 | pub const KILO_LIMIT_F64: f64 = 1000.0; |
| 18 | pub const MEGA_LIMIT_F64: f64 = 1_000_000.0; |
| 19 | pub const GIGA_LIMIT_F64: f64 = 1_000_000_000.0; |
| 20 | pub const TERA_LIMIT_F64: f64 = 1_000_000_000_000.0; |
| 21 | pub const KIBI_LIMIT_F64: f64 = 1024.0; |
| 22 | pub const MEBI_LIMIT_F64: f64 = 1024.0 * 1024.0; |
| 23 | pub const GIBI_LIMIT_F64: f64 = 1024.0 * 1024.0 * 1024.0; |
| 24 | pub const TEBI_LIMIT_F64: f64 = 1024.0 * 1024.0 * 1024.0 * 1024.0; |
| 25 | |
| 26 | pub const LOG_MEGA_LIMIT: f64 = 6.0; |
| 27 | pub const LOG_GIGA_LIMIT: f64 = 9.0; |
| 28 | pub const LOG_TERA_LIMIT: f64 = 12.0; |
| 29 | pub const LOG_PETA_LIMIT: f64 = 15.0; |
| 30 | |
| 31 | pub const LOG_MEBI_LIMIT: f64 = 20.0; |
| 32 | pub const LOG_GIBI_LIMIT: f64 = 30.0; |
| 33 | pub const LOG_TEBI_LIMIT: f64 = 40.0; |
| 34 | pub const LOG_PEBI_LIMIT: f64 = 50.0; |
| 35 | |
| 36 | /// Returns a tuple containing the value and the unit in bytes. In units of |
| 37 | /// 1024. This only supports up to a tebi. Note the "single" unit will have a |
| 38 | /// space appended to match the others if `spacing` is true. |
| 39 | #[inline] |
| 40 | pub fn get_binary_bytes(bytes: u64) -> (f64, &'static str) { |
| 41 | match bytes { |
| 42 | b if b < KIBI_LIMIT => (bytes as f64, "B"), |
| 43 | b if b < MEBI_LIMIT => (bytes as f64 / KIBI_LIMIT_F64, "KiB"), |
| 44 | b if b < GIBI_LIMIT => (bytes as f64 / MEBI_LIMIT_F64, "MiB"), |
| 45 | b if b < TEBI_LIMIT => (bytes as f64 / GIBI_LIMIT_F64, "GiB"), |
| 46 | _ => (bytes as f64 / TEBI_LIMIT_F64, "TiB"), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Returns a tuple containing the value and the unit in bytes. In units of |
| 51 | /// 1000. This only supports up to a tera. Note the "single" unit will have a |
| 52 | /// space appended to match the others if `spacing` is true. |
| 53 | #[inline] |
| 54 | pub fn get_decimal_bytes(bytes: u64) -> (f64, &'static str) { |
| 55 | match bytes { |
| 56 | b if b < KILO_LIMIT => (bytes as f64, "B"), |
| 57 | b if b < MEGA_LIMIT => (bytes as f64 / KILO_LIMIT_F64, "KB"), |
| 58 | b if b < GIGA_LIMIT => (bytes as f64 / MEGA_LIMIT_F64, "MB"), |
| 59 | b if b < TERA_LIMIT => (bytes as f64 / GIGA_LIMIT_F64, "GB"), |
| 60 | _ => (bytes as f64 / TERA_LIMIT_F64, "TB"), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Given a value in _bits_, turn a tuple containing the value and a unit. |
| 65 | #[inline] |
| 66 | pub fn convert_bits(bits: u64, base_two: bool) -> (f64, &'static str) { |
| 67 | convert_bytes(bits / 8, base_two) |
| 68 | } |
| 69 | |
| 70 | /// Given a value in _bytes_, turn a tuple containing the value and a unit. |
| 71 | #[inline] |
| 72 | pub fn convert_bytes(bytes: u64, base_two: bool) -> (f64, &'static str) { |
| 73 | if base_two { |
| 74 | get_binary_bytes(bytes) |
| 75 | } else { |
| 76 | get_decimal_bytes(bytes) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// Return a tuple containing the value and a unit string to be used as a |
| 81 | /// prefix. |
| 82 | #[inline] |
| 83 | pub fn get_unit_prefix(value: u64, base_two: bool) -> (f64, &'static str) { |
| 84 | let float_value = value as f64; |
| 85 | |
| 86 | if base_two { |
| 87 | match value { |
| 88 | b if b < KIBI_LIMIT => (float_value, ""), |
| 89 | b if b < MEBI_LIMIT => (float_value / KIBI_LIMIT_F64, "Ki"), |
| 90 | b if b < GIBI_LIMIT => (float_value / MEBI_LIMIT_F64, "Mi"), |
| 91 | b if b < TEBI_LIMIT => (float_value / GIBI_LIMIT_F64, "Gi"), |
| 92 | _ => (float_value / TEBI_LIMIT_F64, "Ti"), |
| 93 | } |
| 94 | } else { |
| 95 | match value { |
| 96 | b if b < KILO_LIMIT => (float_value, ""), |
| 97 | b if b < MEGA_LIMIT => (float_value / KILO_LIMIT_F64, "K"), |
| 98 | b if b < GIGA_LIMIT => (float_value / MEGA_LIMIT_F64, "M"), |
| 99 | b if b < TERA_LIMIT => (float_value / GIGA_LIMIT_F64, "G"), |
| 100 | _ => (float_value / TERA_LIMIT_F64, "T"), |
| 101 | } |
| 102 | } |
| 103 | } |