human_units/si/
units.rs

1#![allow(deprecated)]
2
3use crate::si::si_unit;
4
5macro_rules! define_units {
6    { $((
7        $name: ident
8        $symbol: literal
9        $description: literal
10    ))* } => {
11        $(
12            #[doc = $description]
13            #[si_unit(symbol = $symbol, internal)]
14            pub struct $name(pub u64);
15        )*
16    };
17}
18
19define_units! {
20    // Base units
21    (Time "s" "Time.")
22    (Length "m" "Length.")
23    (Mass "g" "Mass.")
24    (ElectricCurrent "A" "Electric current.")
25    (ThermodynamicTemperature "K" "Thermodynamic temperature.")
26    (AmountOfSubstance "mol" "Amount of substance.")
27    (LuminousIntensity "cd" "Luminous intensity.")
28    // Derived units.
29    (PlaneAngle "rad" "Plane angle.")
30    (SolidAngle "sr" "Solid angle.")
31    (Frequency "Hz" "Frequency.")
32    (Force "N" "Force.")
33    (Pressure "Pa" "Pressure.")
34    (Energy "J" "Energy.")
35    (Power "W" "Power.")
36    (ElectricCharge "C" "Electric charge.")
37    (Voltage "V" "Voltage.")
38    (Capacitance "F" "Capacitance.")
39    (ElectricConductance "S" "Electric conductance.")
40    (MagneticFlux "Wb" "Magnetic flux.")
41    (MagneticFluxDensity "T" "Magnetic flux density.")
42    (Inductance "H" "Inductance.")
43    (CelsiusTemperature "°C" "Celsius temperature.")
44    (LuminousFlux "lm" "Luminous flux.")
45    (Illuminance "lx" "Illuminance.")
46    (Radioactivity "Bq" "Radioactivity.")
47    (AbsorbedDose "Gy" "Absorbed dose.")
48    (DoseEquivalent "Sy" "Dose equivalent.")
49    (CatalyticActivity "kat" "Catalytic activity.")
50}
51
52#[cfg(feature = "unicode")]
53define_units! {
54    (ElectricResistance "Ω" "Electric resistance.")
55}
56
57#[cfg(not(feature = "unicode"))]
58define_units! {
59    (ElectricResistance "ohm" "Electric resistance.")
60}
61
62/// Data size.
63#[si_unit(symbol = "B", min_prefix = "", max_prefix = "exa", internal)]
64pub struct Size(pub u64);
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use alloc::string::ToString;
70
71    extern crate alloc;
72
73    #[test]
74    fn from_si_works() {
75        let cpu_freq = Frequency::from_si(2_200_000_000);
76        assert_eq!("2200 MHz", cpu_freq.to_string());
77        assert_eq!("2.2 GHz", cpu_freq.format_si().to_string());
78    }
79
80    #[test]
81    fn size_works() {
82        let size = Size::from_si(1_536_000);
83        assert_eq!("1536 kB", size.to_string());
84        assert_eq!("1.5 MB", size.format_si().to_string());
85    }
86}