summaryrefslogtreecommitdiffstats
path: root/src/utils/gen_util.rs
blob: 30a257bcce5ab4b5bbff998e7bbaefdd6dee895e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::cmp::Ordering;

pub fn float_min(a: f32, b: f32) -> f32 {
	match a.partial_cmp(&b) {
		Some(x) => match x {
			Ordering::Greater => b,
			Ordering::Less => a,
			Ordering::Equal => a,
		},
		None => a,
	}
}

pub fn float_max(a: f32, b: f32) -> f32 {
	match a.partial_cmp(&b) {
		Some(x) => match x {
			Ordering::Greater => a,
			Ordering::Less => b,
			Ordering::Equal => a,
		},
		None => a,
	}
}

/// Returns a tuple containing the value and the unit.  In units of 1024.
/// This only supports up to a tebibyte.
pub fn get_exact_byte_values(bytes: u64) -> (f64, String) {
	match bytes {
		b if b < 1024 => (bytes as f64, "B".to_string()),
		b if b < 1_048_576 => (bytes as f64 / 1024.0, "KiB".to_string()),
		b if b < 1_073_741_824 => (bytes as f64 / 1_048_576.0, "MiB".to_string()),
		b if b < 1_099_511_627_776 => (bytes as f64 / 1_073_741_824.0, "GiB".to_string()),
		_ => (bytes as f64 / 1_099_511_627_776.0, "TiB".to_string()),
	}
}

/// Returns a tuple containing the value and the unit.  In units of 1000.
/// This only supports up to a terabyte.  Note the "byte" unit will have a space appended to match the others.
pub fn get_simple_byte_values(bytes: u64) -> (f64, String) {
	match bytes {
		b if b < 1000 => (bytes as f64, "B".to_string()),
		b if b < 1_000_000 => (bytes as f64 / 1000.0, "KB".to_string()),
		b if b < 1_000_000_000 => (bytes as f64 / 1_000_000.0, "MB".to_string()),
		b if b < 1_000_000_000_000 => (bytes as f64 / 1_000_000_000.0, "GB".to_string()),
		_ => (bytes as f64 / 1_000_000_000_000.0, "TB".to_string()),
	}
}