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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
pub use Dimension;
pub use UcumError;
pub use UnitExpr;
pub use Quantity;
use ;
/// Case-sensitivity mode for parsing and lookup.
///
/// UCUM defines a case-sensitive (`c/s`) and a case-insensitive (`c/i`) form.
/// `c/s` is the default for data interchange and the one the free functions
/// ([`parse`], [`analyze`], …) use. Use [`Ucum::case_insensitive`] to opt into
/// `c/i`, where codes are matched against the upper-case `CODE` column.
/// The result of analyzing a unit expression: its dimension and the linear
/// (and affine) relationship of its magnitude to the canonical UCUM base units.
///
/// For an ordinary unit, a magnitude `v` in this unit equals `factor · v` in
/// base units. For an affine unit (e.g. `Cel`), it equals `factor · v + offset`.
/// For a logarithmic or arbitrary special unit, `is_special` is `true` and the
/// linear fields are informational only; see [`Ucum::convert`] for the
/// conversion rules.
/// A configured UCUM facade carrying a [`Case`] mode.
///
/// The crate-level free functions are shorthands for the case-sensitive
/// instance. Construct a [`Ucum`] when you need case-insensitive (`c/i`)
/// handling:
///
/// ```
/// use ucum::Ucum;
/// let ci = Ucum::case_insensitive();
/// assert!(ci.validate("MOL").is_ok()); // mole, case-insensitively
/// assert!(ci.validate("L").is_ok());
/// ```
// --------------------------------------------------------------------------
// Case-sensitive free-function shorthands
// --------------------------------------------------------------------------
/// Parse a UCUM expression into an abstract syntax tree (case-sensitive).
///
/// This is a *total* function: it never panics and never hangs. Syntactically
/// malformed input yields a [`UcumError::Parse`] carrying the byte offset.
/// Atoms are **not** checked for existence here; use [`validate`] or
/// [`analyze`] for that.
///
/// ```
/// use ucum::UcumError;
/// assert!(ucum::parse("kg.m/s2").is_ok());
/// assert!(ucum::parse("/s").is_ok()); // leading-slash reciprocal
/// assert!(ucum::parse("(m/s)").is_ok());
/// // A malformed expression reports *where* it went wrong.
/// assert!(matches!(ucum::parse("m/"), Err(UcumError::Parse { pos: 2, .. })));
/// ```
/// Validate that an expression is well-formed UCUM *and* references only known
/// atoms (case-sensitive). Total.
///
/// ```
/// use ucum::UcumError;
/// assert!(ucum::validate("mg/dL").is_ok());
/// assert!(ucum::validate("[ft_i]").is_ok()); // bracketed customary unit
/// assert!(ucum::validate("1").is_ok()); // the dimensionless unity
/// // The failure tells you which atom is unknown.
/// assert!(matches!(
/// ucum::validate("flurble"),
/// Err(UcumError::UnknownAtom { code }) if code == "flurble"
/// ));
/// ```
/// Analyze an expression into its dimension and conversion factor/offset
/// (case-sensitive). Total.
///
/// ```
/// let a = ucum::analyze("m2").unwrap();
/// assert_eq!(a.dimension, ucum::Dimension([2, 0, 0, 0, 0, 0, 0]));
///
/// let unity = ucum::analyze("1").unwrap();
/// assert!(unity.is_dimensionless);
/// ```
/// Return `true` when two expressions share the same dimension and neither is
/// an arbitrary unit (case-sensitive). Total.
///
/// ```
/// assert!(ucum::is_comparable("km", "[ft_i]").unwrap()); // both length
/// assert!(!ucum::is_comparable("kg", "m").unwrap()); // mass vs length
/// ```
/// Convert a magnitude between two commensurable units (case-sensitive),
/// handling affine offsets (temperature) and logarithmic units. Total.
///
/// Returns [`UcumError::NotComparable`] if the units have different dimensions,
/// or [`UcumError::UnsupportedSpecial`] if either side is an arbitrary unit or a
/// special unit used inside a compound term.
///
/// ```
/// use ucum::{convert, UcumError};
/// assert!((convert(1.0, "[ft_i]", "m").unwrap() - 0.3048).abs() < 1e-12);
/// assert!((convert(1.0, "bar", "Pa").unwrap() - 1e5).abs() < 1e-3);
/// assert!((convert(0.0, "Cel", "K").unwrap() - 273.15).abs() < 1e-9);
/// assert!((convert(2.0, "B", "1").unwrap() - 100.0).abs() < 1e-9); // bels: log
///
/// // Units of different dimensions cannot be converted.
/// assert!(matches!(
/// convert(1.0, "kg", "m"),
/// Err(UcumError::NotComparable { .. })
/// ));
/// ```
/// Return a normalized UCUM string for display (case-sensitive). Total.
///
/// The result is the input re-serialized from its parse tree: redundant
/// parentheses are removed (`((m))` → `m`), while parentheses required to
/// preserve UCUM's left-associative grouping are kept (`kg/(m.s)`). Exponent
/// and number formatting are normalized.
///
/// This is a *syntactic* normalization, **not** a full algebraic canonical
/// form: it does not reorder commutative factors (`m.s` and `s.m` stay
/// distinct) nor reduce units to base dimensions. It also does **not** check
/// that atoms are known, only that the expression parses, so a well-formed
/// but unknown unit still round-trips. Use [`validate`] or [`analyze`] for
/// semantic checks, and [`analyze`] when you need dimensional equivalence.
///
/// ```
/// assert_eq!(ucum::canonical("kg.m/s2").unwrap(), "kg.m/s2");
/// assert_eq!(ucum::canonical("((m))").unwrap(), "m"); // redundant parens dropped
/// assert_eq!(ucum::canonical("kg/(m.s)").unwrap(), "kg/(m.s)"); // necessary parens kept
/// assert_eq!(ucum::canonical("flurble").unwrap(), "flurble"); // not validated!
/// ```
/// Generate a human-readable display name (case-sensitive). Total.
///
/// ```
/// assert_eq!(ucum::display_name("mm").unwrap(), "(millimeter)");
/// assert_eq!(ucum::display_name("rad2").unwrap(), "(radian ^ 2)");
/// ```