use crate::plugins::units::{Unit, Units};
pub(super) fn extended(mut units: Units) -> Units {
static EXTRA: std::sync::LazyLock<Vec<Unit>> = std::sync::LazyLock::new(|| {
let base = Units::default();
let count = base.units.len();
build(base).units.into_iter().skip(count).collect()
});
units.units.extend(EXTRA.iter().cloned());
for unit in &mut units.units {
unit.scale = match unit.symbol.as_str() {
"lbf⋅ft" => 4.4482216152605 * 0.3048,
"lbf⋅in" => 4.4482216152605 * 0.0254,
"psi" => 4.4482216152605 / (0.0254 * 0.0254),
_ => unit.scale,
};
}
units
}
fn build(mut units: Units) -> Units {
let length = [1, 0, 0, 0, 0, 0, 0, 0];
let volume = [3, 0, 0, 0, 0, 0, 0, 0];
let time = [0, 0, 1, 0, 0, 0, 0, 0];
for (aliases, symbol, dimension, scale) in [
("nanometre|nanometres", "nm", length, 1e-9),
("US gallon|US gallons", "gal", volume, 0.003785411784),
("US cup|US cups", "US cup", volume, 0.003785411784 / 16.0),
(
"imperial pint|imperial pints",
"imp pt",
volume,
0.00454609 / 8.0,
),
("US quart|US quarts", "US qt", volume, 0.003785411784 / 4.0),
(
"US fluid ounce|US fluid ounces|fluid ounce|fluid ounces|fl oz",
"fl oz",
volume,
0.003785411784 / 128.0,
),
("fortnight|fortnights", "fortnights", time, 14.0 * 86400.0),
("nanosecond|nanoseconds|ns", "ns", time, 1e-9),
("THz|terahertz", "THz", [0, 0, -1, 0, 0, 0, 0, 0], 1e12),
("statute mile|statute miles", "mi", length, 1609.344),
(
"knot|knots|kn",
"kn",
[1, 0, -1, 0, 0, 0, 0, 0],
1852.0 / 3600.0,
),
("A", "A", [0, 0, 0, 0, 0, 1, 0, 0], 1.0),
(
"VA|volt ampere|volt amperes",
"VA",
[2, 1, -3, 0, 0, 0, 0, 0],
1.0,
),
(
"var|VAR|volt ampere reactive|volt amperes reactive",
"var",
[2, 1, -3, 0, 0, 0, 0, 0],
1.0,
),
] {
units = units.with_unit(Unit {
aliases,
symbol: symbol.into(),
dimension,
scale,
offset: 0.0,
});
}
for (prefix, aliases, factor) in [
("p", "p", 1e-12),
("n", "n", 1e-9),
("μ", "μ|µ|u", 1e-6),
("m", "m", 1e-3),
("k", "k", 1e3),
("M", "M", 1e6),
("G", "G", 1e9),
] {
for (symbol, base) in [
("A", "ampere"),
("V", "volt"),
("Ω", "ohm"),
("F", "farad"),
("H", "henry"),
("S", "siemens"),
("C", "coulomb"),
("Ah", "ampere hours"),
("N", "newton"),
("W", "watt"),
("J", "joule"),
("Pa", "pascal"),
("Hz", "hertz"),
("mol", "mole"),
("VA", "volt ampere"),
("var", "volt ampere reactive"),
] {
let Some(base) = units.unit(base) else {
continue;
};
let aliases = aliases
.split('|')
.map(|p| format!("{p}{symbol}"))
.collect::<Vec<_>>()
.join("|");
units = units.with_unit(Unit {
aliases: Box::leak(aliases.into_boxed_str()),
symbol: format!("{prefix}{symbol}"),
dimension: base.dimension,
scale: base.scale * factor,
offset: 0.0,
});
}
}
units
}