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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#![allow(non_upper_case_globals)]

//! Recognized SI units

use crate::SI;

/// Pi as a fraction
///
/// Its value is the smallest continued fraction where converting to f64s and dividing produces a value exactly equal to f64::consts::PI
pub const PI: (i128, u128) = (245850922, 78256779);

/// A dimensionless value
#[doc(alias = "1")]
pub const DIMENSIONLESS: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (0, 1),
    kg: (0, 1),
    A: (0, 1),
    K: (0, 1),
    mol: (0, 1),
    cd: (0, 1),
};

// Base units

/// Second (s)
#[doc(alias = "s")]
pub const second: SI = SI {
    scale: (1, 1),
    s: (1, 1),
    m: (0, 1),
    kg: (0, 1),
    A: (0, 1),
    K: (0, 1),
    mol: (0, 1),
    cd: (0, 1),
};
/// Meter (m)
#[doc(alias = "m")]
pub const meter: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (1, 1),
    kg: (0, 1),
    A: (0, 1),
    K: (0, 1),
    mol: (0, 1),
    cd: (0, 1),
};
/// Kilogram (kg)
#[doc(alias = "kg")]
pub const kilogram: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (0, 1),
    kg: (1, 1),
    A: (0, 1),
    K: (0, 1),
    mol: (0, 1),
    cd: (0, 1),
};
/// Ampere (A)
#[doc(alias = "A")]
pub const ampere: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (0, 1),
    kg: (0, 1),
    A: (1, 1),
    K: (0, 1),
    mol: (0, 1),
    cd: (0, 1),
};
/// Kelvin (K)
#[doc(alias = "K")]
pub const kelvin: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (0, 1),
    kg: (0, 1),
    A: (0, 1),
    K: (1, 1),
    mol: (0, 1),
    cd: (0, 1),
};
/// Mole (mol)
#[doc(alias = "mol")]
pub const mole: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (0, 1),
    kg: (0, 1),
    A: (0, 1),
    K: (0, 1),
    mol: (1, 1),
    cd: (0, 1),
};
/// Candela (cd)
#[doc(alias = "cd")]
pub const candela: SI = SI {
    scale: (1, 1),
    s: (0, 1),
    m: (0, 1),
    kg: (0, 1),
    A: (0, 1),
    K: (0, 1),
    mol: (0, 1),
    cd: (1, 1),
};

// Named derived units

/// Radian (rad) (effectively an alias for `DIMENSIONLESS`)
#[doc(alias = "rad")]
pub const radian: SI = meter.div(meter);
/// Steradian (sr) (effectively an alias for `DIMENSIONLESS`)
#[doc(alias = "sr")]
pub const steradian: SI = meter.powi(2).div(meter.powi(2));
/// Hertz (Hz)
#[doc(alias = "Hz")]
pub const hertz: SI = second.powi(-1);
/// Newton (N)
#[doc(alias = "N")]
pub const newton: SI = kilogram.mul(meter).div(second.powi(2));
/// Pascal (Pa)
#[doc(alias = "Pa")]
pub const pascal: SI = kilogram.div(meter).div(second.powi(2));
/// Joule (J)
#[doc(alias = "J")]
pub const joule: SI = newton.mul(meter);
/// Watt (W)
#[doc(alias = "W")]
pub const watt: SI = joule.div(second);
/// Coulomb (C)
#[doc(alias = "C")]
pub const coulomb: SI = second.mul(ampere);
/// Volt (V)
#[doc(alias = "V")]
pub const volt: SI = watt.div(ampere);
/// Farad (F)
#[doc(alias = "F")]
pub const farad: SI = coulomb.div(volt);
/// Ohm (Ω)
#[doc(alias = "omega")]
#[doc(alias = "Ω")]
pub const ohm: SI = volt.div(ampere);
/// Siemens (S)
#[doc(alias = "S")]
pub const siemens: SI = ohm.powi(-1);
/// Weber (Wb)
#[doc(alias = "Wb")]
pub const weber: SI = volt.mul(second);
/// Tesla (T)
#[doc(alias = "T")]
pub const tesla: SI = weber.div(meter.powi(2));
/// Henry (H)
#[doc(alias = "H")]
pub const henry: SI = weber.div(ampere);
/// Lumen (lm) (effectively an alias for `candela`)
#[doc(alias = "lm")]
pub const lumen: SI = candela.mul(steradian);
/// Lux (lx)
#[doc(alias = "lx")]
pub const lux: SI = candela.mul(steradian).div(meter.powi(2));
/// Bequerel (Bq) (alias for `hertz`)
#[doc(alias = "Bq")]
pub const bequerel: SI = hertz;
/// Gray (Gy)
#[doc(alias = "Gy")]
pub const gray: SI = joule.div(kilogram);
/// Sievert (Sv) (alias for `gray`)
#[doc(alias = "Sv")]
pub const sievert: SI = gray;
/// Katal (kat)
#[doc(alias = "kat")]
pub const katal: SI = mole.div(second);

/// Percent (%)
#[doc(alias = "%")]
pub const percent: SI = DIMENSIONLESS.scale_by((1, 100));
/// Degree (°)
#[doc(alias = "°")]
pub const degree: SI = DIMENSIONLESS.scale_by(PI).scale_by((1, 180));
/// Arcminute (′)
#[doc(alias = "′")]
pub const arcminute: SI = degree.scale_by((1, 60));
/// Arcsecond (″)
#[doc(alias = "″")]
pub const arcsecond: SI = arcminute.scale_by((1, 60));
/// Minute (min)
#[doc(alias = "min")]
pub const minute: SI = second.scale_by((60, 1));
/// Hour (h)
#[doc(alias = "h")]
pub const hour: SI = meter.scale_by((60, 1));
/// Day (d)
#[doc(alias = "d")]
pub const day: SI = hour.scale_by((24, 1));

/// Astronomical Unit (au)
pub const au: SI = meter.scale_by((149597870700, 1)); // I think using the `au` abbreviation is okay since `astronomical_unit` would be so long and usually people pronounce "a-u" when speaking
/// Hectare (ha)
#[doc(alias = "ha")]
pub const hectare: SI = meter.powi(2).scale_by((10000, 1));

/// Liter (l, L)
#[doc(alias = "l")]
pub const liter: SI = meter.powi(3).scale_by((1, 1000));

/// Metric ton (t)
#[doc(alias = "t")]
pub const ton: SI = kilogram.scale_by((1000, 1));
/// Dalton (Da)
#[doc(alias = "Da")]
pub const dalton: SI = kilogram.scale_by((1_660_539_040, 10_u128.pow(27 + 9)));

/// Electronvolt (eV)
#[doc(alias = "eV")]
pub const electronvolt: SI = joule.scale_by((1_602_176_634, 10_u128.pow(19 + 9)));

// Must be sorted for binary search
pub(crate) const UNITS_MAP: [(&str, SI); 42] = [
    ("%", percent),
    ("A", ampere),
    ("Bq", bequerel),
    ("C", coulomb),
    ("Da", dalton),
    ("F", farad),
    ("Gy", gray),
    ("H", henry),
    ("Hz", hertz),
    ("J", joule),
    ("K", kelvin),
    ("L", liter),
    ("N", newton),
    ("Pa", pascal),
    ("S", siemens),
    ("Sv", sievert),
    ("T", tesla),
    ("V", volt),
    ("W", watt),
    ("Wb", weber),
    ("au", au),
    ("cd", candela),
    ("d", day),
    ("eV", electronvolt),
    ("g", crate::prefixes::milli(kilogram)), // parsing dkg as deci-kilogram would be bad, we only want one prefix
    ("h", hour),
    ("ha", hectare),
    ("kat", katal),
    ("l", liter),
    ("lm", lumen),
    ("lx", lux),
    ("m", meter),
    ("min", minute),
    ("mol", mole),
    ("rad", radian),
    ("s", second),
    ("sr", steradian),
    ("t", ton),
    ("°", degree),
    ("Ω", ohm),
    ("′", arcminute),
    ("″", arcsecond),
];

#[cfg(test)]
mod tests {
    use crate::units::UNITS_MAP;

    #[test]
    fn is_sorted() {
        for i in 0..UNITS_MAP.len() - 1 {
            assert!(UNITS_MAP[i].0 < UNITS_MAP[i + 1].0);
        }
    }
}