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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
pub mod unit;
pub mod unit_dimension;
#[cfg(feature = "units")]
pub mod units_generated;
pub use unit::Unit;
pub use unit_dimension::UnitDimensions;
#[allow(unused_variables)]
pub fn get_unit(unit: &str) -> Option<&'static Unit> {
#[cfg(feature = "units")]
{
return units_generated::UNITS.get(unit).copied();
}
#[cfg(not(feature = "units"))]
return None;
}
pub fn get_unit_or_default(unit: &str) -> &'static Unit {
if let Some(unit) = get_unit(unit) {
unit
} else {
&*DEFAULT_UNIT
}
}
#[allow(unused_variables)]
pub fn match_units(dim: UnitDimensions, scale: f64) -> Vec<&'static Unit> {
#[cfg(feature = "units")]
{
units_generated::UNITS
.iter()
.filter_map(|(_, u)| {
if u.dimensions.as_ref() == Some(&dim) && approx_eq(u.scale, scale) {
Some(*u)
} else {
None
}
})
.collect()
}
#[cfg(not(feature = "units"))]
return Vec::default();
}
#[cfg(feature = "units")]
fn approx_eq(a: f64, b: f64) -> bool {
if a == b {
return true;
}
let min_precision = f64::min(f64::abs(a / 1e3), f64::abs(b / 1e3));
f64::abs(a - b) <= min_precision
}
lazy_static! {
pub static ref DEFAULT_UNIT: Unit = Unit::default();
}