pub mod unit;
pub mod unit_dimension;
#[cfg(feature = "units-db")]
pub mod units_generated;
use std::borrow::Cow;
pub use unit::Unit;
pub use unit_dimension::UnitDimensions;
pub static DEFAULT_UNIT: Unit = Unit {
quantity: None,
ids: Cow::Borrowed(&[]),
dimensions: None,
scale: 0.0,
offset: 0.0,
};
#[allow(unused_variables)]
pub fn get_unit(unit: &str) -> Option<&'static Unit> {
#[cfg(feature = "units-db")]
{
units_generated::UNITS.get(unit).copied()
}
#[cfg(not(feature = "units-db"))]
return None;
}
pub fn get_unit_or_default(unit: &str) -> &'static Unit {
get_unit(unit).unwrap_or(&DEFAULT_UNIT)
}
#[allow(unused_variables)]
pub fn match_units(dim: UnitDimensions, scale: f64) -> Vec<&'static Unit> {
#[cfg(feature = "units-db")]
{
units_generated::UNITS
.values()
.filter_map(|u| {
if u.dimensions.as_ref() == Some(&dim) && approx_eq(u.scale, scale) {
Some(*u)
} else {
None
}
})
.collect()
}
#[cfg(not(feature = "units-db"))]
return Vec::default();
}
#[cfg(feature = "units-db")]
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
}