Debian Codegraph @

src/utils/conversion.rs

raw symbols SCIP
1 //! This mainly concerns converting collected data into things that the canvas
2 //! can actually handle.
3
4 use craterust-analyzer cargo bottom 0.14.9 crate/::utilsrust-analyzer cargo bottom 0.14.9 utils/::data_unitsrust-analyzer cargo bottom 0.14.9 utils/data_units/::*;
5
6 /// Returns the most appropriate binary prefix unit type (e.g. kibibyte) and
7 /// denominator for the given amount of bytes.
8 ///
9 /// The expected usage is to divide out the given value with the returned
10 /// denominator in order to be able to use it with the returned binary unit
11 /// (e.g. divide 3000 bytes by 1024 to have a value in KiB).
12 #[inline]
13 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn get_binary_unit_and_denominatorrust-analyzer cargo bottom 0.14.9 utils/conversion/get_binary_unit_and_denominator().(bytes: u64) -> (&'static str, f64) {
14 match bytes {
15 b if b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().KIBI_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/KIBI_LIMIT. => ("B", 1.0),
16 b if b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().MEBI_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/MEBI_LIMIT. => ("KiB", KIBI_LIMIT_F64rust-analyzer cargo bottom 0.14.9 utils/data_units/KIBI_LIMIT_F64.),
17 b if b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().GIBI_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/GIBI_LIMIT. => ("MiB", MEBI_LIMIT_F64rust-analyzer cargo bottom 0.14.9 utils/data_units/MEBI_LIMIT_F64.),
18 b if b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().TEBI_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/TEBI_LIMIT. => ("GiB", GIBI_LIMIT_F64rust-analyzer cargo bottom 0.14.9 utils/data_units/GIBI_LIMIT_F64.),
19 _ => ("TiB", TEBI_LIMIT_F64rust-analyzer cargo bottom 0.14.9 utils/data_units/TEBI_LIMIT_F64.),
20 }
21 }
22
23 /// Returns a string given a value that is converted to the closest SI-variant,
24 /// per second. If the value is greater than a giga-X, then it will return a
25 /// decimal place.
26 #[inline]
27 pub(craterust-analyzer cargo bottom 0.14.9 crate/) fn dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(value: u64) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
28 let converted_values = get_decimal_bytesrust-analyzer cargo bottom 0.14.9 utils/data_units/get_decimal_bytes().(value);
29 if value rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]ge().GIGA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/GIGA_LIMIT. {
30 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:.1}{}/s", converted_values.0, converted_values.1)
31 } else {
32 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:.0}{}/s", converted_values.0, converted_values.1)
33 }
34 }
35
36 #[cfg(test)]
37 mod testrust-analyzer cargo bottom 0.14.9 utils/conversion/test/ {
38 use superrust-analyzer cargo bottom 0.14.9 utils/conversion/::*;
39
40 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
41 fn test_dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/test/test_dec_bytes_per_second_string().() {
42 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(0), "0B/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
43 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(1), "1B/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
44 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(900), "900B/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
45 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(999), "999B/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
46 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(KILO_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/KILO_LIMIT.), "1KB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
47 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
48 dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(KILO_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/KILO_LIMIT. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). 1),
49 "1KB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
50 );
51 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(KIBI_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/KIBI_LIMIT.), "1KB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
52 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(MEGA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/MEGA_LIMIT.), "1MB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
53 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
54 dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(GIGA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/GIGA_LIMIT.),
55 "1.0GB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
56 );
57 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
58 dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().(2 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). GIGA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/GIGA_LIMIT.),
59 "2.0GB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
60 );
61 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
62 dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().((2.5 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). GIGA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/GIGA_LIMIT. as f64) as u64),
63 "2.5GB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
64 );
65 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
66 dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().((10.34 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). TERA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/TERA_LIMIT. as f64) as u64),
67 "10.3TB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
68 );
69 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
70 dec_bytes_per_second_stringrust-analyzer cargo bottom 0.14.9 utils/conversion/dec_bytes_per_second_string().((10.36 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). TERA_LIMITrust-analyzer cargo bottom 0.14.9 utils/data_units/TERA_LIMIT. as f64) as u64),
71 "10.4TB/s".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
72 );
73 }
74 }