dv_rs/
units.rs

1use std::collections::HashMap;
2
3/// Base SI units: meter (m), kilogram (kg), second (s), kelvin (K), ampere (A), mole (mol), candela (cd), radian (rad)
4pub static BASE_UNITS: [&str; 8] = ["m", "kg", "s", "K", "A", "mol", "cd", "rad"];
5
6/// Size of the base units array
7pub static BASE_UNITS_SIZE: usize = BASE_UNITS.len();
8
9/// A physical unit, defined by its name, conversion factor to SI base units, and its representation in base units.
10pub struct Unit {
11    #[allow(dead_code)]
12    /// The human-readable name of the unit (e.g., "meter").
13    pub name: &'static str,                 // Unused, kept for documentation and future extensibility
14    /// The conversion factor to the SI base unit.
15    pub conversion_factor: f64,
16    /// The representation of the unit in terms of base units.
17    pub base_unit: [f64; BASE_UNITS_SIZE], 
18}
19
20/// Unit map from unit strings to their definitions
21pub fn unit_map() -> HashMap<&'static str, Unit> {
22    HashMap::from([
23        // ===== Length Units =====
24        ("m", Unit { name: "meter", conversion_factor: 1.0, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
25        ("cm", Unit { name: "centimeter", conversion_factor: 1e-2, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
26        ("mm", Unit { name: "millimeter", conversion_factor: 1e-3, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
27        ("um", Unit { name: "micrometer", conversion_factor: 1e-6, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
28        ("nm", Unit { name: "nanometer", conversion_factor: 1e-9, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
29        ("km", Unit { name: "kilometer", conversion_factor: 1e3, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
30
31        ("mi", Unit { name: "mile", conversion_factor: 1609.344, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
32        ("yd", Unit { name: "yard", conversion_factor: 0.9144, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
33        ("ft", Unit { name: "foot", conversion_factor: 0.3048, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
34        ("in", Unit { name: "inch", conversion_factor: 2.54e-2, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
35
36        ("ly", Unit { name: "light year", conversion_factor: 9.4607e15, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
37        ("pc", Unit { name: "parsec", conversion_factor: 3.0857e16, base_unit: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
38
39        // ===== Area Units =====
40        ("ha", Unit { name: "hectare", conversion_factor: 1e4, base_unit: [2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
41        ("ac", Unit { name: "acre", conversion_factor: 4046.8564224, base_unit: [2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
42
43        // ===== Volume Units =====
44        ("l", Unit { name: "liter", conversion_factor: 1e-3, base_unit: [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
45        ("ml", Unit { name: "milliliter", conversion_factor: 1e-6, base_unit: [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
46        ("gal", Unit { name: "gallon", conversion_factor: 3.785411784, base_unit: [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
47        ("qt", Unit { name: "quart", conversion_factor: 9.4635284e-1, base_unit: [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
48        ("pt", Unit { name: "pint", conversion_factor: 4.7317642e-1, base_unit: [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
49        ("cup", Unit { name: "cup", conversion_factor: 2.3658821e-1, base_unit: [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
50
51        // ===== Velocity Units =====
52        ("kn", Unit { name: "knot", conversion_factor: 1852.0 / 3600.0, base_unit: [1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
53
54        // ===== Force Units =====
55        ("N", Unit { name: "newton", conversion_factor: 1.0, base_unit: [1.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
56        ("kN", Unit { name: "kilonewton", conversion_factor: 1e3, base_unit: [1.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
57        ("lbf", Unit { name: "pound-force", conversion_factor: 4.4482216152605, base_unit: [1.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
58
59        // ===== Mass Units =====
60        ("kg", Unit { name: "kilogram", conversion_factor: 1.0, base_unit: [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
61        ("g", Unit { name: "gram", conversion_factor: 1e-3, base_unit: [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
62        ("mg", Unit { name: "milligram", conversion_factor: 1e-6, base_unit: [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
63        ("t", Unit { name: "metric ton", conversion_factor: 1e3, base_unit: [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
64        ("lb", Unit { name: "pound", conversion_factor: 0.45359237, base_unit: [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
65        ("oz", Unit { name: "ounce", conversion_factor: 0.028349523125, base_unit: [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
66
67        // ===== Time Units =====
68        ("ns", Unit { name: "nanosecond", conversion_factor: 1e-9, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
69        ("us", Unit { name: "microsecond", conversion_factor: 1e-6, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
70        ("ms", Unit { name: "millisecond", conversion_factor: 1e-3, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
71        ("s", Unit { name: "second", conversion_factor: 1.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
72        ("min", Unit { name: "minute", conversion_factor: 60.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
73        ("hr", Unit { name: "hour", conversion_factor: 3600.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
74        ("d", Unit { name: "day", conversion_factor: 86400.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
75        ("wk", Unit { name: "week", conversion_factor: 604800.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
76        ("mo", Unit { name: "month", conversion_factor: 2629800.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
77        ("yr", Unit { name: "year", conversion_factor: 31536000.0, base_unit: [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
78
79        // ===== Temperature Units =====
80        ("C", Unit { name: "celsius", conversion_factor: 1.0, base_unit: [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0] }),
81        ("F", Unit { name: "fahrenheit", conversion_factor: 5.0 / 9.0, base_unit: [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0] }),
82        ("K", Unit { name: "kelvin", conversion_factor: 1.0, base_unit: [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0] }),
83
84        // ===== Electric Current Units =====
85        ("A", Unit {name: "ampere", conversion_factor: 1.0, base_unit: [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] }),
86        ("mA", Unit { name: "milliampere", conversion_factor: 1e-3, base_unit: [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] }),
87        ("kA", Unit { name: "kiloampere", conversion_factor: 1e3, base_unit: [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] }),
88        ("MA", Unit { name: "megaampere", conversion_factor: 1e6, base_unit: [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] }),
89
90        // ===== Energy Units =====
91        ("ev", Unit { name: "electronvolt", conversion_factor: 1.602176634e-19, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
92        ("mJ", Unit { name: "millijoule", conversion_factor: 1e-3, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
93        ("J", Unit { name: "joule", conversion_factor: 1.0, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
94        ("kJ", Unit { name: "kilojoule", conversion_factor: 1e3, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
95        ("MJ", Unit { name: "megajoule", conversion_factor: 1e6, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
96        ("GJ", Unit { name: "gigajoule", conversion_factor: 1e9, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
97        ("Tj", Unit { name: "terajoule", conversion_factor: 1e12, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
98        ("cal", Unit { name: "calorie", conversion_factor: 4.184, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
99        ("kcal", Unit { name: "kilocalorie", conversion_factor: 4.184e3, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
100        ("Wh", Unit { name: "watt-hour", conversion_factor: 3600.0, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
101        ("kWh", Unit { name: "kilowatt-hour", conversion_factor: 3.6e6, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
102        ("BTU", Unit { name: "British thermal unit", conversion_factor: 1055.05585, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
103        ("erg", Unit { name: "erg", conversion_factor: 1e-7, base_unit: [2.0, 1.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
104
105        // ===== Power Units =====
106        ("W", Unit { name: "watt", conversion_factor: 1.0, base_unit: [2.0, 1.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
107        ("kW", Unit { name: "kilowatt", conversion_factor: 1e3, base_unit: [2.0, 1.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
108        ("MW", Unit { name: "megawatt", conversion_factor: 1e6, base_unit: [2.0, 1.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
109        ("GW", Unit { name: "gigawatt", conversion_factor: 1e9, base_unit: [2.0, 1.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
110        ("TW", Unit { name: "terawatt", conversion_factor: 1e12, base_unit: [2.0, 1.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
111        ("hp", Unit { name: "horsepower", conversion_factor: 745.6998715822702, base_unit: [2.0, 1.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0] }),
112
113        // ===== Amount of Substance Units =====
114        ("mol", Unit { name: "mole", conversion_factor: 1.0, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
115        ("kmol", Unit { name: "kilomole", conversion_factor: 1e3, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
116        ("mmol", Unit { name: "millimole", conversion_factor: 1e-3, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
117        ("umol", Unit { name: "micromole", conversion_factor: 1e-6, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
118        ("nmol", Unit { name: "nanomole", conversion_factor: 1e-9, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
119        ("pmol", Unit { name: "picomole", conversion_factor: 1e-12, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
120        ("fmol", Unit { name: "femtomole", conversion_factor: 1e-15, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0] }),
121
122        // ===== Luminous Intensity Units =====
123        ("cd", Unit { name: "candela", conversion_factor: 1.0, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0] }),
124
125        ("lux", Unit { name: "lux", conversion_factor: 1.0, base_unit: [0.0, 0.0, -2.0, 0.0, 0.0, 0.0, 1.0, 0.0] }),
126
127        // ===== Angle Units =====
128        ("rad", Unit { name: "radian", conversion_factor: 1.0, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }),
129        ("mrad", Unit { name: "milliradian", conversion_factor: 1e-3, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }),
130        ("deg", Unit { name: "degree", conversion_factor: std::f64::consts::PI / 180.0, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }),
131        ("arcmin", Unit { name: "arcminute", conversion_factor: std::f64::consts::PI / 10800.0, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }),
132        ("arcsec", Unit { name: "arcsecond", conversion_factor: std::f64::consts::PI / 648000.0, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }),
133        ("rev", Unit { name: "revolution", conversion_factor: 2.0 * std::f64::consts::PI, base_unit: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }),
134    ])
135}