u-numflow 0.14.0

Foundational mathematical primitives: statistics, probability distributions, random sampling, and collections.
Documentation
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
//! Units of measure: dimensional analysis and conversion between units of the
//! same dimension.
//!
//! A unit is a point in the space of physical dimensions — integer powers of
//! the seven SI base quantities — together with an affine map to the coherent
//! SI unit of that dimension: `value_in_si = value · scale + offset`. Most
//! units have offset 0 (a kilowatt hour is 3.6 × 10⁶ J); a temperature scale
//! such as the degree Celsius has an offset (0 °C = 273.15 K).
//!
//! Two conversions are distinct and both are offered:
//!
//! - [`Units::convert`] converts a **reading** — 20 °C is 293.15 K;
//! - [`Units::convert_difference`] converts an **amount of change** — a rise
//!   of 20 °C is a rise of 20 K. The offset cancels in a difference.
//!
//! Mixing the two is a silent error with an offset unit, so the caller picks.
//!
//! # Expressions
//!
//! Units are written as expressions in the case-sensitive syntax of UCUM
//! (the Unified Code for Units of Measure), restricted to what dimensional
//! conversion needs:
//!
//! - an atom (`m`, `s`, `J`), optionally with a decimal prefix (`km`, `mg`,
//!   `kW`, `uA` — `u` is micro);
//! - an integer exponent written after it (`m3`, `s-2`, `m+2`);
//! - products with `.` and quotients with `/`, evaluated left to right
//!   (`kW.h`, `m/s2`, `kg/m.s` = `(kg/m)·s`), a leading `/` for a reciprocal
//!   (`/s`);
//! - parentheses (`J/(kg.K)`), positive integers as factors (`1000.m`), `1`
//!   for unity;
//! - annotations in braces (`m3{gas}`), which UCUM defines to have no effect
//!   on the unit and which are read as such.
//!
//! An atom is resolved before it is split into prefix and atom (`cd` is the
//! candela, not a centi-day), and a whole token is tried as an atom before a
//! trailing exponent is split off — so an atom whose symbol ends in digits
//! (`Nm3`, defined by a caller) stays whole.
//!
//! A unit with an offset stands alone: a prefix, an exponent or a product
//! applied to it is refused, since none has a meaning for a reading.
//!
//! # Built-in units
//!
//! [`Units::si`] knows the SI: the base units `m`, `g` (with prefix: `kg`),
//! `s`, `A`, `K`, `mol`, `cd`; the 22 coherent derived units with special
//! names (`rad`, `sr`, `Hz`, `N`, `Pa`, `J`, `W`, `C`, `V`, `F`, `Ohm`, `S`,
//! `Wb`, `T`, `H`, `Cel`, `lm`, `lx`, `Bq`, `Gy`, `Sv`, `kat`); the units the SI
//! accepts for use with it that are exact multiples (`min`, `h`, `d`, `L`/`l`,
//! `t`); and the decimal prefixes `Q` … `q`. Everything else — calorie
//! variants, the tonne of oil equivalent, the normal cubic metre, imperial
//! units — is defined by the caller with [`Units::define`], so this crate
//! never chooses between same-named units of different size (the 15 °C,
//! international and thermochemical calories differ by up to 0.07 %).
//!
//! # Example
//!
//! ```
//! use u_numflow::units::Units;
//!
//! let mut units = Units::si();
//! units.define("cal_IT", 4.1868, "J", 0.0).unwrap();
//! assert!((units.convert(1.0, "kW.h", "J").unwrap() - 3.6e6).abs() < 1e-6);
//! assert!((units.convert(1.0, "kcal_IT", "J").unwrap() - 4186.8).abs() < 1e-9);
//! assert!((units.convert(20.0, "Cel", "K").unwrap() - 293.15).abs() < 1e-9);
//! assert_eq!(units.convert_difference(20.0, "Cel", "K").unwrap(), 20.0);
//! assert!(units.convert(1.0, "m3", "kg").is_err()); // volume is not mass
//! ```
//!
//! # References
//! - BIPM (2019), *The International System of Units (SI)*, 9th ed.
//! - Schadow & McDonald, *The Unified Code for Units of Measure*, §2–§5
//!   (syntax only; no UCUM table is embedded).

use std::collections::HashMap;
use std::fmt;

/// Symbols of the SI base units, in the order of a [`Dimension`]'s exponents.
pub const BASE_SYMBOLS: [&str; 7] = ["m", "kg", "s", "A", "K", "mol", "cd"];

/// Integer exponents of the seven SI base quantities — length, mass, time,
/// electric current, thermodynamic temperature, amount of substance,
/// luminous intensity, in that order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Dimension(pub [i32; 7]);

impl Dimension {
    /// The dimension of a pure number.
    pub const NONE: Dimension = Dimension([0; 7]);

    fn base(index: usize) -> Self {
        let mut e = [0; 7];
        e[index] = 1;
        Dimension(e)
    }

    fn times(self, other: Self) -> Self {
        let mut e = self.0;
        for (a, b) in e.iter_mut().zip(other.0) {
            *a += b;
        }
        Dimension(e)
    }

    fn pow(self, n: i32) -> Self {
        Dimension(self.0.map(|e| e * n))
    }

    /// Whether this is the dimension of a pure number.
    pub fn is_none(&self) -> bool {
        *self == Self::NONE
    }
}

/// Written as a product of SI base units in UCUM syntax, e.g. `kg.m2.s-2`
/// (or `1` for a pure number).
impl fmt::Display for Dimension {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Mass first reads as engineers write it: kg.m2.s-2.
        const ORDER: [usize; 7] = [1, 0, 2, 3, 4, 5, 6];
        let parts: Vec<String> = ORDER
            .iter()
            .filter(|&&i| self.0[i] != 0)
            .map(|&i| match self.0[i] {
                1 => BASE_SYMBOLS[i].to_string(),
                e => format!("{}{e}", BASE_SYMBOLS[i]),
            })
            .collect();
        if parts.is_empty() {
            f.write_str("1")
        } else {
            f.write_str(&parts.join("."))
        }
    }
}

/// A unit: its dimension and the affine map to the coherent SI unit of that
/// dimension, `value_in_si = value · scale + offset`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Unit {
    /// The physical dimension.
    pub dimension: Dimension,
    /// Size of one unit in coherent SI units (`> 0`).
    pub scale: f64,
    /// SI value of the unit's zero (0 except for scales such as `Cel`).
    pub offset: f64,
}

impl Unit {
    const ONE: Unit = Unit {
        dimension: Dimension::NONE,
        scale: 1.0,
        offset: 0.0,
    };

    /// A reading `value` in this unit, in `to`.
    ///
    /// # Errors
    /// [`UnitError::NotFinite`] for `value`, [`UnitError::DimensionMismatch`]
    /// when `to` measures something else.
    pub fn convert_to(&self, value: f64, to: &Unit) -> Result<f64, UnitError> {
        self.check(value, to)?;
        Ok((value * self.scale + self.offset - to.offset) / to.scale)
    }

    /// An amount of change `value` in this unit, in `to` (offsets cancel).
    ///
    /// # Errors
    /// As [`Unit::convert_to`].
    pub fn difference_to(&self, value: f64, to: &Unit) -> Result<f64, UnitError> {
        self.check(value, to)?;
        Ok(value * self.scale / to.scale)
    }

    fn check(&self, value: f64, to: &Unit) -> Result<(), UnitError> {
        if !value.is_finite() {
            return Err(UnitError::NotFinite { parameter: "value" });
        }
        if self.dimension != to.dimension {
            return Err(UnitError::DimensionMismatch {
                from: self.dimension,
                to: to.dimension,
            });
        }
        Ok(())
    }

    fn times(self, other: Unit) -> Unit {
        Unit {
            dimension: self.dimension.times(other.dimension),
            scale: self.scale * other.scale,
            offset: 0.0,
        }
    }

    fn pow(self, n: i32) -> Unit {
        Unit {
            dimension: self.dimension.pow(n),
            scale: self.scale.powi(n),
            offset: 0.0,
        }
    }
}

/// Why an expression, a definition or a conversion was refused.
#[derive(Debug, Clone, PartialEq)]
pub enum UnitError {
    /// The expression is not well formed (an empty term, an unbalanced
    /// parenthesis, a misplaced operator, an exponent too large).
    Malformed {
        /// The expression as given.
        expression: String,
        /// What is wrong, in words.
        reason: String,
    },
    /// A symbol that is neither a known atom nor a prefix with one.
    UnknownSymbol {
        /// The expression as given.
        expression: String,
        /// The symbol not recognised.
        symbol: String,
    },
    /// A unit with an offset given a prefix, an exponent, or a product.
    OffsetCombined {
        /// The expression as given.
        expression: String,
        /// The offset unit's symbol.
        symbol: String,
    },
    /// A conversion between different dimensions.
    DimensionMismatch {
        /// The dimension of the unit converted from.
        from: Dimension,
        /// The dimension of the unit converted to.
        to: Dimension,
    },
    /// A definition whose symbol is already an atom (built in or defined).
    DuplicateSymbol {
        /// The symbol.
        symbol: String,
    },
    /// A definition whose symbol is not usable as an atom.
    InvalidSymbol {
        /// The symbol.
        symbol: String,
        /// What is wrong, in words.
        reason: String,
    },
    /// A value, factor or offset that is NaN or infinite.
    NotFinite {
        /// What it is (`value`, `factor`, `offset`).
        parameter: &'static str,
    },
    /// A definition's factor that is not `> 0`.
    NonPositiveFactor {
        /// The factor given.
        got: f64,
    },
}

impl fmt::Display for UnitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UnitError::Malformed { expression, reason } => {
                write!(f, "unit \"{expression}\": {reason}")
            }
            UnitError::UnknownSymbol { expression, symbol } => {
                write!(f, "unit \"{expression}\": unknown unit symbol \"{symbol}\"")
            }
            UnitError::OffsetCombined { expression, symbol } => write!(
                f,
                "unit \"{expression}\": \"{symbol}\" has an offset (a scale such as Cel) and \
                 must stand alone, without a prefix, an exponent or a product"
            ),
            UnitError::DimensionMismatch { from, to } => {
                write!(f, "cannot convert between dimensions {from} and {to}")
            }
            UnitError::DuplicateSymbol { symbol } => {
                write!(f, "\"{symbol}\" is already a unit")
            }
            UnitError::InvalidSymbol { symbol, reason } => {
                write!(f, "\"{symbol}\" cannot be a unit symbol: {reason}")
            }
            UnitError::NotFinite { parameter } => {
                write!(f, "{parameter} must be a finite number")
            }
            UnitError::NonPositiveFactor { got } => {
                write!(f, "factor must be > 0, got {got}")
            }
        }
    }
}

impl std::error::Error for UnitError {}

/// Decimal prefixes (SI 2022, Q … q), with `u` for micro as UCUM writes it.
const PREFIXES: [(&str, i32); 25] = [
    ("Q", 30),
    ("R", 27),
    ("Y", 24),
    ("Z", 21),
    ("E", 18),
    ("P", 15),
    ("T", 12),
    ("G", 9),
    ("M", 6),
    ("k", 3),
    ("h", 2),
    ("da", 1),
    ("d", -1),
    ("c", -2),
    ("m", -3),
    ("u", -6),
    ("n", -9),
    ("p", -12),
    ("f", -15),
    ("a", -18),
    ("z", -21),
    ("y", -24),
    ("r", -27),
    ("q", -30),
    ("μ", -6),
];

/// A known atom: the unit it stands for, and whether a prefix may go on it.
#[derive(Debug, Clone, Copy)]
struct Atom {
    unit: Unit,
    prefixable: bool,
}

/// A set of unit symbols: the SI, and whatever the caller defines.
///
/// Every operation reads expressions against this set; nothing is global.
#[derive(Debug, Clone)]
pub struct Units {
    atoms: HashMap<String, Atom>,
}

/// The characters that separate tokens in an expression.
fn is_operator(c: char) -> bool {
    matches!(c, '.' | '/' | '(' | ')' | '{' | '}') || c.is_whitespace()
}

impl Units {
    /// The SI units (see the module documentation).
    pub fn si() -> Self {
        let mut atoms = HashMap::new();
        let mut put = |symbol: &str, unit: Unit, prefixable: bool| {
            atoms.insert(symbol.to_string(), Atom { unit, prefixable });
        };
        let d = |e: [i32; 7]| Dimension(e);
        let coherent = |e: [i32; 7]| Unit {
            dimension: d(e),
            scale: 1.0,
            offset: 0.0,
        };
        let scaled = |e: [i32; 7], scale: f64| Unit {
            dimension: d(e),
            scale,
            offset: 0.0,
        };
        put(
            "m",
            Unit {
                dimension: Dimension::base(0),
                scale: 1.0,
                offset: 0.0,
            },
            true,
        );
        put(
            "g",
            Unit {
                dimension: Dimension::base(1),
                scale: 1e-3,
                offset: 0.0,
            },
            true,
        );
        put(
            "s",
            Unit {
                dimension: Dimension::base(2),
                scale: 1.0,
                offset: 0.0,
            },
            true,
        );
        put(
            "A",
            Unit {
                dimension: Dimension::base(3),
                scale: 1.0,
                offset: 0.0,
            },
            true,
        );
        put(
            "K",
            Unit {
                dimension: Dimension::base(4),
                scale: 1.0,
                offset: 0.0,
            },
            true,
        );
        put(
            "mol",
            Unit {
                dimension: Dimension::base(5),
                scale: 1.0,
                offset: 0.0,
            },
            true,
        );
        put(
            "cd",
            Unit {
                dimension: Dimension::base(6),
                scale: 1.0,
                offset: 0.0,
            },
            true,
        );
        put("rad", coherent([0, 0, 0, 0, 0, 0, 0]), true);
        put("sr", coherent([0, 0, 0, 0, 0, 0, 0]), true);
        put("Hz", coherent([0, 0, -1, 0, 0, 0, 0]), true);
        put("N", coherent([1, 1, -2, 0, 0, 0, 0]), true);
        put("Pa", coherent([-1, 1, -2, 0, 0, 0, 0]), true);
        put("J", coherent([2, 1, -2, 0, 0, 0, 0]), true);
        put("W", coherent([2, 1, -3, 0, 0, 0, 0]), true);
        put("C", coherent([0, 0, 1, 1, 0, 0, 0]), true);
        put("V", coherent([2, 1, -3, -1, 0, 0, 0]), true);
        put("F", coherent([-2, -1, 4, 2, 0, 0, 0]), true);
        put("Ohm", coherent([2, 1, -3, -2, 0, 0, 0]), true);
        put("S", coherent([-2, -1, 3, 2, 0, 0, 0]), true);
        put("Wb", coherent([2, 1, -2, -1, 0, 0, 0]), true);
        put("T", coherent([0, 1, -2, -1, 0, 0, 0]), true);
        put("H", coherent([2, 1, -2, -2, 0, 0, 0]), true);
        put("lm", coherent([0, 0, 0, 0, 0, 0, 1]), true);
        put("lx", coherent([-2, 0, 0, 0, 0, 0, 1]), true);
        put("Bq", coherent([0, 0, -1, 0, 0, 0, 0]), true);
        put("Gy", coherent([2, 0, -2, 0, 0, 0, 0]), true);
        put("Sv", coherent([2, 0, -2, 0, 0, 0, 0]), true);
        put("kat", coherent([0, 0, -1, 0, 0, 1, 0]), true);
        put(
            "Cel",
            Unit {
                dimension: Dimension::base(4),
                scale: 1.0,
                offset: 273.15,
            },
            false,
        );
        // Accepted for use with the SI (SI Brochure, Table 8) — exact multiples.
        put("min", scaled([0, 0, 1, 0, 0, 0, 0], 60.0), false);
        put("h", scaled([0, 0, 1, 0, 0, 0, 0], 3600.0), false);
        put("d", scaled([0, 0, 1, 0, 0, 0, 0], 86_400.0), false);
        put("L", scaled([3, 0, 0, 0, 0, 0, 0], 1e-3), true);
        put("l", scaled([3, 0, 0, 0, 0, 0, 0], 1e-3), true);
        put("t", scaled([0, 1, 0, 0, 0, 0, 0], 1e3), true);
        put("1", Unit::ONE, false);
        Units { atoms }
    }

    /// Defines `symbol` as `factor` × `expression` (+ `offset` SI units at
    /// its zero, for a scale): `define("cal_IT", 4.1868, "J", 0.0)`,
    /// `define("toe", 1e7, "kcal_IT", 0.0)`,
    /// `define("degF", 5.0 / 9.0, "K", 459.67 * 5.0 / 9.0)`.
    ///
    /// `expression` may use the built-in units and every symbol defined
    /// before. A defined unit takes decimal prefixes unless it has an offset.
    /// A symbol that reads as a prefix on an atom (`ft`, the femtotonne) is
    /// free to define: atoms are read before prefixes, so the definition wins.
    ///
    /// # Errors
    /// [`UnitError::InvalidSymbol`] for a symbol that is empty, starts with a
    /// digit or a sign, or holds an operator character (`.` `/` `(` `)` `{`
    /// `}` or a space); [`UnitError::DuplicateSymbol`] for one already
    /// defined (built-in or not); [`UnitError::NotFinite`] /
    /// [`UnitError::NonPositiveFactor`] for `factor` and `offset`; and the
    /// errors of [`Units::parse`] for `expression`.
    pub fn define(
        &mut self,
        symbol: &str,
        factor: f64,
        expression: &str,
        offset: f64,
    ) -> Result<(), UnitError> {
        let invalid = |reason: &str| UnitError::InvalidSymbol {
            symbol: symbol.to_string(),
            reason: reason.to_string(),
        };
        let first = symbol
            .chars()
            .next()
            .ok_or_else(|| invalid("it is empty"))?;
        if first.is_ascii_digit() || first == '+' || first == '-' {
            return Err(invalid("it starts with a digit or a sign"));
        }
        if symbol.chars().any(is_operator) {
            return Err(invalid(
                "it holds an operator character (. / ( ) { } or a space)",
            ));
        }
        // A prefix on an atom (`ft` = femtotonne) may be taken over: atoms
        // are read first, and `ft` as the foot is what a caller means.
        if self.atoms.contains_key(symbol) {
            return Err(UnitError::DuplicateSymbol {
                symbol: symbol.to_string(),
            });
        }
        if !factor.is_finite() {
            return Err(UnitError::NotFinite {
                parameter: "factor",
            });
        }
        if factor <= 0.0 {
            return Err(UnitError::NonPositiveFactor { got: factor });
        }
        if !offset.is_finite() {
            return Err(UnitError::NotFinite {
                parameter: "offset",
            });
        }
        let base = self.parse(expression)?;
        let unit = Unit {
            dimension: base.dimension,
            scale: factor * base.scale,
            offset: base.offset + offset,
        };
        self.atoms.insert(
            symbol.to_string(),
            Atom {
                unit,
                prefixable: unit.offset == 0.0,
            },
        );
        Ok(())
    }

    /// The unit `expression` stands for.
    ///
    /// # Errors
    /// [`UnitError::Malformed`], [`UnitError::UnknownSymbol`],
    /// [`UnitError::OffsetCombined`].
    pub fn parse(&self, expression: &str) -> Result<Unit, UnitError> {
        let mut parser = Parser {
            units: self,
            expression,
            chars: expression.char_indices().peekable(),
        };
        let (unit, offset_symbol) = parser.term()?;
        if let Some(&(_, c)) = parser.chars.peek() {
            return Err(parser.malformed(format!("unexpected \"{c}\"")));
        }
        if let OffsetUse::Combined(symbol) = offset_symbol {
            return Err(UnitError::OffsetCombined {
                expression: expression.to_string(),
                symbol,
            });
        }
        Ok(unit)
    }

    /// `value` read in `from`, in `to`: a reading, so a scale's offset
    /// applies (20 `Cel` is 293.15 `K`).
    ///
    /// # Errors
    /// [`UnitError::NotFinite`] for `value`; the errors of [`Units::parse`];
    /// [`UnitError::DimensionMismatch`] when the units measure different
    /// things.
    pub fn convert(&self, value: f64, from: &str, to: &str) -> Result<f64, UnitError> {
        self.parse(from)?.convert_to(value, &self.parse(to)?)
    }

    /// An amount of change `value` in `from`, in `to`: the offset cancels
    /// (a rise of 20 `Cel` is a rise of 20 `K`).
    ///
    /// # Errors
    /// As [`Units::convert`].
    pub fn convert_difference(&self, value: f64, from: &str, to: &str) -> Result<f64, UnitError> {
        self.parse(from)?.difference_to(value, &self.parse(to)?)
    }

    /// The atom `token` names, alone or after a prefix; `None` if neither.
    fn atom<'t>(&self, token: &'t str) -> Option<(Unit, Option<&'t str>)> {
        if let Some(a) = self.atoms.get(token) {
            return Some((a.unit, None));
        }
        PREFIXES.iter().find_map(|&(prefix, exp)| {
            let rest = token.strip_prefix(prefix)?;
            let a = self.atoms.get(rest)?;
            if !a.prefixable && a.unit.offset == 0.0 {
                return None;
            }
            Some((
                Unit {
                    dimension: a.unit.dimension,
                    scale: a.unit.scale * 10f64.powi(exp),
                    offset: a.unit.offset,
                },
                Some(rest),
            ))
        })
    }
}

/// Whether a parsed part involves an offset unit, and how.
#[derive(Debug, Clone, PartialEq)]
enum OffsetUse {
    None,
    /// The whole part is the offset unit, unmodified.
    Alone(String),
    /// An offset unit with a prefix, exponent, or product.
    Combined(String),
}

struct Parser<'a> {
    units: &'a Units,
    expression: &'a str,
    chars: std::iter::Peekable<std::str::CharIndices<'a>>,
}

impl Parser<'_> {
    fn malformed(&self, reason: String) -> UnitError {
        UnitError::Malformed {
            expression: self.expression.to_string(),
            reason,
        }
    }

    /// term := ['/'] component (('.' | '/') component)*
    fn term(&mut self) -> Result<(Unit, OffsetUse), UnitError> {
        let mut unit = Unit::ONE;
        let mut parts: Vec<OffsetUse> = Vec::new();
        let mut divide = false;
        if let Some(&(_, '/')) = self.chars.peek() {
            self.chars.next();
            divide = true;
        }
        let mut divided = divide;
        loop {
            let (next, offset) = self.component()?;
            unit = if parts.is_empty() && !divide {
                next
            } else {
                unit.times(if divide { next.pow(-1) } else { next })
            };
            parts.push(offset);
            match self.chars.peek() {
                Some(&(_, '.')) => divide = false,
                Some(&(_, '/')) => divide = true,
                _ => break,
            }
            divided |= divide;
            self.chars.next();
        }
        // An offset unit keeps its meaning only as the whole term.
        let offset = match parts.as_slice() {
            [OffsetUse::Alone(s)] if !divided => OffsetUse::Alone(s.clone()),
            _ => parts
                .into_iter()
                .find_map(|p| match p {
                    OffsetUse::Alone(s) | OffsetUse::Combined(s) => Some(OffsetUse::Combined(s)),
                    OffsetUse::None => None,
                })
                .unwrap_or(OffsetUse::None),
        };
        if !matches!(offset, OffsetUse::Alone(_)) {
            unit.offset = 0.0;
        }
        Ok((unit, offset))
    }

    /// component := '(' term ')' annotation? | token annotation? | annotation
    fn component(&mut self) -> Result<(Unit, OffsetUse), UnitError> {
        match self.chars.peek().copied() {
            None => Err(self.malformed("a unit is missing at the end".to_string())),
            Some((_, '(')) => {
                self.chars.next();
                let (unit, offset) = self.term()?;
                match self.chars.next() {
                    Some((_, ')')) => {}
                    _ => return Err(self.malformed("\"(\" is not closed".to_string())),
                }
                self.annotation()?;
                Ok((unit, offset))
            }
            Some((_, '{')) => {
                self.annotation()?;
                Ok((Unit::ONE, OffsetUse::None))
            }
            Some((_, c)) if is_operator(c) => {
                Err(self.malformed(format!("a unit is missing before \"{c}\"")))
            }
            Some((start, _)) => {
                let mut end = start;
                while let Some(&(i, c)) = self.chars.peek() {
                    if is_operator(c) {
                        break;
                    }
                    end = i + c.len_utf8();
                    self.chars.next();
                }
                let token = &self.expression[start..end];
                let result = self.token(token)?;
                self.annotation()?;
                Ok(result)
            }
        }
    }

    /// An optional `{…}` annotation, which has no effect on the unit.
    fn annotation(&mut self) -> Result<(), UnitError> {
        if let Some(&(_, '{')) = self.chars.peek() {
            self.chars.next();
            loop {
                match self.chars.next() {
                    Some((_, '}')) => return Ok(()),
                    Some((_, '{')) => {
                        return Err(self.malformed("an annotation holds \"{\"".to_string()))
                    }
                    Some(_) => {}
                    None => return Err(self.malformed("\"{\" is not closed".to_string())),
                }
            }
        }
        Ok(())
    }

    /// token := integer | atom exponent? — the whole token is tried as an
    /// atom before a trailing exponent is split off.
    fn token(&self, token: &str) -> Result<(Unit, OffsetUse), UnitError> {
        if token.bytes().all(|b| b.is_ascii_digit()) {
            let n: f64 = token
                .parse()
                .map_err(|_| self.malformed(format!("\"{token}\" is not a number")))?;
            if n <= 0.0 {
                return Err(self.malformed("a factor must be a positive integer".to_string()));
            }
            return Ok((
                Unit {
                    scale: n,
                    ..Unit::ONE
                },
                OffsetUse::None,
            ));
        }
        if let Some(found) = self.lookup(token) {
            return Ok(found);
        }
        let digits = token
            .char_indices()
            .rev()
            .take_while(|(_, c)| c.is_ascii_digit())
            .last()
            .map(|(i, _)| i);
        let Some(digits_at) = digits else {
            return Err(self.unknown(token));
        };
        let (symbol_and_sign, digits) = token.split_at(digits_at);
        let (symbol, sign) = match symbol_and_sign.strip_suffix(['+', '-']) {
            Some(s) => (s, &symbol_and_sign[s.len()..]),
            None => (symbol_and_sign, ""),
        };
        if symbol.is_empty() {
            return Err(self.malformed(format!("\"{token}\" has an exponent but no unit")));
        }
        let n: i32 = format!("{sign}{digits}")
            .parse()
            .ok()
            .filter(|n: &i32| n.unsigned_abs() <= 64)
            .ok_or_else(|| self.malformed(format!("exponent \"{sign}{digits}\" is too large")))?;
        let (unit, offset) = self.lookup(symbol).ok_or_else(|| self.unknown(symbol))?;
        let offset = match offset {
            OffsetUse::Alone(s) if n != 1 => OffsetUse::Combined(s),
            other => other,
        };
        Ok((unit.pow(n), offset))
    }

    fn lookup(&self, symbol: &str) -> Option<(Unit, OffsetUse)> {
        let (unit, prefixed) = self.units.atom(symbol)?;
        let offset = match (unit.offset != 0.0, prefixed) {
            (false, _) => OffsetUse::None,
            (true, None) => OffsetUse::Alone(symbol.to_string()),
            (true, Some(atom)) => OffsetUse::Combined(atom.to_string()),
        };
        Some((unit, offset))
    }

    fn unknown(&self, symbol: &str) -> UnitError {
        UnitError::UnknownSymbol {
            expression: self.expression.to_string(),
            symbol: symbol.to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn close(a: f64, b: f64) -> bool {
        (a - b).abs() <= 1e-12 * b.abs().max(1.0)
    }

    /// The units a district-heating or energy report needs, defined as data.
    fn energy() -> Units {
        let mut u = Units::si();
        u.define("bar", 1e5, "Pa", 0.0).unwrap();
        u.define("cal_IT", 4.1868, "J", 0.0).unwrap();
        u.define("cal_th", 4.184, "J", 0.0).unwrap();
        u.define("toe", 1e7, "kcal_IT", 0.0).unwrap();
        u.define("Nm3", 1.0, "m3", 0.0).unwrap();
        u
    }

    // ── the behaviours the request lists ──

    #[test]
    fn one_bar_is_one_hundred_kilopascal() {
        assert!(close(energy().convert(1.0, "bar", "kPa").unwrap(), 100.0));
    }

    #[test]
    fn a_celsius_reading_converts_with_the_offset_and_a_difference_without() {
        let u = Units::si();
        assert!(close(u.convert(20.0, "Cel", "K").unwrap(), 293.15));
        assert!(close(u.convert_difference(20.0, "Cel", "K").unwrap(), 20.0));
        assert!(close(u.convert(300.0, "K", "Cel").unwrap(), 26.85));
    }

    #[test]
    fn a_kilowatt_hour_is_three_point_six_megajoule() {
        assert!(close(Units::si().convert(1.0, "kW.h", "J").unwrap(), 3.6e6));
        assert!(close(Units::si().convert(1.0, "kW.h", "MJ").unwrap(), 3.6));
    }

    #[test]
    fn the_calorie_is_the_one_the_caller_defined() {
        let u = energy();
        assert!(close(u.convert(1.0, "kcal_IT", "J").unwrap(), 4186.8));
        assert!(close(u.convert(1.0, "kcal_th", "J").unwrap(), 4184.0));
        assert!(close(u.convert(1.0, "Gcal_IT", "GJ").unwrap(), 4.1868));
        assert!(close(u.convert(1.0, "toe", "GJ").unwrap(), 41.868));
    }

    #[test]
    fn volume_is_not_mass() {
        assert_eq!(
            energy().convert(1.0, "m3", "kg"),
            Err(UnitError::DimensionMismatch {
                from: Dimension([3, 0, 0, 0, 0, 0, 0]),
                to: Dimension([0, 1, 0, 0, 0, 0, 0]),
            })
        );
    }

    // ── syntax ──

    #[test]
    fn products_and_quotients_read_left_to_right() {
        let u = Units::si();
        assert!(close(
            u.convert(1.0, "km/h", "m/s").unwrap(),
            1000.0 / 3600.0
        ));
        // kg/m.s = (kg/m)·s, as UCUM reads it.
        assert_eq!(
            u.parse("kg/m.s").unwrap().dimension,
            Dimension([-1, 1, 1, 0, 0, 0, 0])
        );
        assert_eq!(
            u.parse("kg/(m.s)").unwrap().dimension,
            Dimension([-1, 1, -1, 0, 0, 0, 0])
        );
        assert_eq!(
            u.parse("/s").unwrap().dimension,
            u.parse("Hz").unwrap().dimension
        );
        assert!(close(u.convert(1.0, "m/s2", "N/kg").unwrap(), 1.0));
        assert!(close(u.convert(1.0, "J/(kg.K)", "m2/(s2.K)").unwrap(), 1.0));
    }

    #[test]
    fn exponents_are_signed_integers() {
        let u = Units::si();
        assert!(close(u.convert(1.0, "m3", "L").unwrap(), 1000.0));
        assert!(close(u.convert(1.0, "cm3", "mL").unwrap(), 1.0));
        assert!(close(u.convert(2.0, "s-1", "Hz").unwrap(), 2.0));
        assert!(close(u.convert(1.0, "m+2", "dm2").unwrap(), 100.0));
    }

    #[test]
    fn integers_are_factors_and_one_is_unity() {
        let u = Units::si();
        assert!(close(u.convert(1.0, "1000.m", "km").unwrap(), 1.0));
        assert!(u.parse("1").unwrap().dimension.is_none());
        assert!(close(u.convert(1.0, "rad", "1").unwrap(), 1.0));
    }

    #[test]
    fn annotations_have_no_effect() {
        let u = energy();
        assert!(close(u.convert(5.0, "m3{gas}", "m3").unwrap(), 5.0));
        assert!(close(u.convert(5.0, "{count}/min", "/h").unwrap(), 300.0));
    }

    #[test]
    fn an_atom_wins_over_a_prefix_split_and_a_symbol_ending_in_digits_stays_whole() {
        let u = energy();
        assert_eq!(u.parse("cd").unwrap().dimension, Dimension::base(6)); // candela
        assert!(close(u.convert(1.0, "Nm3", "m3").unwrap(), 1.0));
        assert!(close(u.convert(1.0, "Nm3", "Nm3").unwrap(), 1.0));
        assert!(close(u.convert(1.0, "min", "s").unwrap(), 60.0)); // not milli-inch
        assert!(close(u.convert(1.0, "mmol", "mol").unwrap(), 1e-3));
        assert!(close(u.convert(1.0, "kg", "g").unwrap(), 1e3));
        assert!(close(u.convert(1.0, "dam", "m").unwrap(), 10.0));
        assert!(close(u.convert(1.0, "um", "m").unwrap(), 1e-6));
        assert!(close(u.convert(1.0, "μm", "m").unwrap(), 1e-6));
    }

    #[test]
    fn hours_and_days_take_no_prefix() {
        // "kh" would be a kilohour; the SI does not prefix the hour.
        assert!(matches!(
            Units::si().parse("kh"),
            Err(UnitError::UnknownSymbol { .. })
        ));
    }

    // ── refusals ──

    #[test]
    fn an_offset_unit_stands_alone() {
        let u = Units::si();
        for e in ["Cel2", "kCel", "Cel/s", "m.Cel", "(Cel)", "/Cel"] {
            let r = u.parse(e);
            if e == "(Cel)" {
                // A parenthesized lone unit is still the unit alone.
                assert!(r.is_ok(), "{e}: {r:?}");
                continue;
            }
            assert!(
                matches!(r, Err(UnitError::OffsetCombined { ref symbol, .. }) if symbol == "Cel"),
                "{e}: {r:?}"
            );
        }
    }

    #[test]
    fn a_malformed_expression_is_refused() {
        let u = Units::si();
        for e in ["", "m.", "m//s", "(m", "m)", "m{x", ".m", "m s", "m99999"] {
            assert!(
                matches!(u.parse(e), Err(UnitError::Malformed { .. })),
                "{e:?}: {:?}",
                u.parse(e)
            );
        }
    }

    #[test]
    fn an_unknown_symbol_is_named() {
        assert_eq!(
            Units::si().parse("kW.hr"),
            Err(UnitError::UnknownSymbol {
                expression: "kW.hr".into(),
                symbol: "hr".into()
            })
        );
        // The exponent is split off before the symbol is named.
        assert!(matches!(
            Units::si().parse("yd2"),
            Err(UnitError::UnknownSymbol { ref symbol, .. }) if symbol == "yd"
        ));
        // A prefix on an atom is a unit even where a reader expects another:
        // `ft` is the femtotonne — imperial units are the caller's to define.
        assert!(Units::si().parse("ft").is_ok());
    }

    #[test]
    fn a_definition_is_checked() {
        let mut u = Units::si();
        assert_eq!(
            u.define("J", 1.0, "J", 0.0),
            Err(UnitError::DuplicateSymbol { symbol: "J".into() })
        );
        // A prefixed atom is not an atom: the foot takes over the femtotonne.
        u.define("ft", 0.3048, "m", 0.0).unwrap();
        assert!(close(u.convert(1.0, "ft2", "m2").unwrap(), 0.3048 * 0.3048));
        for bad in ["", "2x", "-x", "a.b", "a b", "a/b", "{a}"] {
            assert!(
                matches!(
                    u.define(bad, 1.0, "m", 0.0),
                    Err(UnitError::InvalidSymbol { .. })
                ),
                "{bad:?}"
            );
        }
        assert_eq!(
            u.define("x", 0.0, "m", 0.0),
            Err(UnitError::NonPositiveFactor { got: 0.0 })
        );
        assert_eq!(
            u.define("x", f64::NAN, "m", 0.0),
            Err(UnitError::NotFinite {
                parameter: "factor"
            })
        );
        assert!(matches!(
            u.define("x", 1.0, "later", 0.0),
            Err(UnitError::UnknownSymbol { .. })
        ));
    }

    #[test]
    fn a_defined_scale_converts_readings() {
        let mut u = Units::si();
        u.define("degF", 5.0 / 9.0, "K", 459.67 * 5.0 / 9.0)
            .unwrap();
        assert!(close(u.convert(32.0, "degF", "Cel").unwrap(), 0.0));
        assert!(close(u.convert(212.0, "degF", "Cel").unwrap(), 100.0));
        assert!(close(u.convert_difference(9.0, "degF", "K").unwrap(), 5.0));
        assert!(matches!(
            u.parse("kdegF"),
            Err(UnitError::OffsetCombined { .. })
        ));
    }

    #[test]
    fn a_value_must_be_finite() {
        assert_eq!(
            Units::si().convert(f64::INFINITY, "m", "m"),
            Err(UnitError::NotFinite { parameter: "value" })
        );
    }

    #[test]
    fn a_dimension_is_written_in_base_units() {
        let u = Units::si();
        assert_eq!(u.parse("J").unwrap().dimension.to_string(), "kg.m2.s-2");
        assert_eq!(u.parse("rad").unwrap().dimension.to_string(), "1");
        assert_eq!(
            u.parse("Ohm").unwrap().dimension.to_string(),
            "kg.m2.s-3.A-2"
        );
    }

    #[test]
    fn round_trips_are_exact_to_rounding() {
        let u = energy();
        for (a, b) in [("kW.h", "GJ"), ("Cel", "K"), ("bar", "Pa"), ("toe", "MW.h")] {
            let x = 123.456;
            let back = u.convert(u.convert(x, a, b).unwrap(), b, a).unwrap();
            assert!(close(back, x), "{a} ↔ {b}: {back}");
        }
    }
}