whippyunits-core 0.1.0

Core types and utilities for whippyunits
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
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
use crate::dimension_exponents::{DynDimensionExponents, TypeDimensionExponents};
use crate::prefix::SiPrefix;
use crate::scale_exponents::ScaleExponents;

/// Unit system classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum System {
    /// Metric system (SI and derived units)
    Metric,
    /// Imperial system (US customary units)
    Imperial,
    /// Astronomical system (For extremely )
    Astronomical,
}

impl System {
    /// Get the string identifier for this system.
    pub const fn as_str(&self) -> &'static str {
        match self {
            System::Metric => "Metric",
            System::Imperial => "Imperial",
            System::Astronomical => "Astronomical",
        }
    }
}

/// A unit.
///
/// Each unit is assigned a "storage unit". The storage unit is the unit
/// of a value stored with this unit. Storage units are always a well defined
/// multiple of an SI base unit.
///
/// The logarithmic scale encoding in the type system uses only powers of
/// 2, 3, 5, and pi.  This means that the storage unit must be a multiple of
/// an SI base unit and a product of powers of 2, 3, 5, and pi.  For example,
///
/// - "kilometer" has a scale factor of 10^3 = 2^3 * 5^3
/// - "degree" has a scale factor of π/180 = 2^-2 * 3^-2 * 5^-1 * pi^1
///
/// Units that differ from identity in their `conversion_factor` are "non-storage"
/// units.  Non-storage units are not stored in their native scale; upon declaration
/// they are converted to their "nearest neighbor" power-of-10 multiple of a SI
/// base unit.  For example
///
/// - "inch" is multiplied by 2.54 and stored as "centimeters"
/// - "yard" is multiplied by 0.9144 and stored as "meters"
/// - "mile" is multiplied by 1.609344 and stored as "kilometers"
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Unit<ExponentsType = DynDimensionExponents> {
    /// Name of the unit.
    pub name: &'static str,

    /// Symbols associated with the unit.
    ///
    /// Symbols are also used for lookup, so they must be unique within
    /// the unit system.
    ///
    /// Because SI has a systematic prefixing semantics, symbols must be kept
    /// from colliding not just with the SI base symbols, but with any legal
    /// prefixing thereof.  "SI base symbols" are defined as the first symbol
    /// of the first unit in each dimension, by declaration order.
    pub symbols: &'static [&'static str],

    /// Base unit per storage unit.
    ///
    /// To convert from a value in the storage unit to a SI base unit value,
    /// multiply it by `scale`.
    ///
    /// For example, a scale of `1000` in dimension length would store
    /// the value as kilometers. If we have the value `123` stored then that
    /// means in meters it is `123 * 1000 = 123,000 meters`.
    pub scale: ScaleExponents,

    /// Storage unit per this unit.
    ///
    /// Difference from identity canonically identifies a unit as a
    /// "non-storage" unit.  Non-storage units are not stored in their
    /// native scale, as arbitrary float scaling factors are not part
    /// of the logarithmic scale encoding in the type system.
    ///
    /// To convert from a value in this unit to a storage unit value,
    /// multiply it by `conversion_factor`.
    ///
    /// For example, the "inch" unit has a `conversion_factor` of `2.54`,
    /// which means that a value of `1` in inches is stored as `2.54`
    /// (accordingly, the `scale` is `10^-2`).
    ///
    /// Non-storage units are always given a storage scale of their
    /// "nearest neighbor" power-of-10 multiple of a SI base unit.
    /// For example,
    ///
    /// - "inch" is multiplied by 2.54 and stored as "centimeters"
    /// - "yard" is multiplied by 0.9144 and stored as "meters"
    /// - "mile" is multiplied by 1.609344 and stored as "kilometers"
    pub conversion_factor: f64,

    /// The "zero point offset" of this unit's measurement scale from
    /// the numerical zero of the storage unit.
    ///
    /// To convert from a value in the unit to the storage unit, add
    /// the affine offset to the value.
    ///
    /// For example, the "celsius" unit has an affine offset of `273.15`,
    /// which means that a value of `0` in celsius is stored as `273.15`
    /// in kelvin.
    pub affine_offset: f64,

    /// Dimensional exponent vector of the [dimension](crate::dimension_exponents::DimensionBasis)
    /// this unit belongs to.
    pub exponents: ExponentsType,

    /// Which "unit system" this unit belongs to.  This determines the name
    /// of the declarator trait in which this unit's nominal declarators will
    /// live (e.g. "ImperialLength" or "MetricMass").
    pub system: System,
}

const IDENTITY: f64 = 1.0;
const NONE: f64 = 0.0;

impl<ExponentsType> Unit<ExponentsType> {
    /// Check if the unit has a conversion factor.
    pub fn has_conversion(&self) -> bool {
        self.conversion_factor != IDENTITY
    }

    /// Check if the unit has an affine offset.
    pub fn has_affine_offset(&self) -> bool {
        self.affine_offset != NONE
    }
}

impl<
    const MASS_EXP: i16,
    const LENGTH_EXP: i16,
    const TIME_EXP: i16,
    const CURRENT_EXP: i16,
    const TEMPERATURE_EXP: i16,
    const AMOUNT_EXP: i16,
    const LUMINOSITY_EXP: i16,
    const ANGLE_EXP: i16,
>
    Unit<
        crate::dimension_exponents!([
            MASS_EXP,
            LENGTH_EXP,
            TIME_EXP,
            CURRENT_EXP,
            TEMPERATURE_EXP,
            AMOUNT_EXP,
            LUMINOSITY_EXP,
            ANGLE_EXP,
        ]),
    >
{
    pub const fn erase(&self) -> Unit {
        Unit {
            name: self.name,
            symbols: self.symbols,
            scale: self.scale,
            conversion_factor: self.conversion_factor,
            affine_offset: self.affine_offset,
            exponents: self.exponents.value_const(),
            system: self.system,
        }
    }
}

/// Lists of units.
impl Unit {
    pub const BASES: [Self; 8] = [
        Unit::GRAM.erase(),
        Unit::METER.erase(),
        Unit::SECOND.erase(),
        Unit::AMPERE.erase(),
        Unit::KELVIN.erase(),
        Unit::MOLE.erase(),
        Unit::CANDELA.erase(),
        Unit::RADIAN.erase(),
    ];

    /// Convert multiple names to their base unit.
    ///
    /// This function takes a scale type name (like "Kilogram", "Millimeter") and returns
    /// the corresponding base unit (like "g", "m").
    pub fn multiple_to_base_unit(
        multiple: &str,
    ) -> Option<(&'static Self, Option<&'static SiPrefix>)> {
        let (prefix, name) = SiPrefix::strip_any_prefix_name(multiple)
            .map(|(prefix, name)| (Some(prefix), name))
            .unwrap_or((None, multiple));

        Self::BASES.iter().find_map(|unit| {
            if unit.name == name {
                Some((unit, prefix))
            } else {
                None
            }
        })
    }

    /// Find base unit by symbol.
    pub fn find_base(symbol: &str) -> Option<&'static Self> {
        Self::BASES
            .iter()
            .find(|unit| unit.symbols.contains(&symbol))
    }
}

/// Mass
impl Unit<crate::dimension_exponents!([1, 0, 0, 0, 0, 0, 0, 0])> {
    pub const GRAM: Self = Self {
        name: "gram",
        symbols: &["g"],
        scale: ScaleExponents::_10(-3),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const GRAIN: Self = Self {
        name: "grain",
        symbols: &["gr"],
        scale: ScaleExponents::_10(-4),
        conversion_factor: 0.6479891,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const CARAT: Self = Self {
        name: "carat",
        symbols: &["ct"],
        scale: ScaleExponents::_10(-4).mul(ScaleExponents::_2(1)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const OUNCE: Self = Self {
        name: "ounce",
        symbols: &["oz"],
        scale: ScaleExponents::_10(-2),
        conversion_factor: 2.8349523125,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const TROY_OUNCE: Self = Self {
        name: "troy_ounce",
        symbols: &["ozt"],
        scale: ScaleExponents::_10(-2),
        conversion_factor: 3.11034768,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
    pub const TROY_POUND: Self = Self {
        name: "troy_pound",
        symbols: &["lbt"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: 0.3732417216,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const POUND: Self = Self {
        name: "pound",
        symbols: &["lb"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: 0.45359237,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const STONE: Self = Self {
        name: "stone",
        symbols: &["st"],
        scale: ScaleExponents::_10(1),
        conversion_factor: 0.635029318,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const SLUG: Self = Self {
        name: "slug",
        symbols: &["slg"],
        scale: ScaleExponents::_10(1),
        conversion_factor: 1.4593902937206365,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const TON: Self = Self {
        name: "ton",
        symbols: &["t"],
        scale: ScaleExponents::_10(3),
        conversion_factor: 1.0160469088,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
    // To add: Earth mass, Jupiter mass, Sol mass
}

/// Length
impl Unit<crate::dimension_exponents!([0, 1, 0, 0, 0, 0, 0, 0])> {
    pub const METER: Self = Self {
        name: "meter",
        symbols: &["m"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const INCH: Self = Self {
        name: "inch",
        symbols: &["in"],
        scale: ScaleExponents::_10(-2),
        conversion_factor: 2.54,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const FOOT: Self = Self {
        name: "foot",
        symbols: &["ft"],
        scale: ScaleExponents::_10(-1),
        conversion_factor: 3.048,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const YARD: Self = Self {
        name: "yard",
        symbols: &["yd"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: 0.9144,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const FATHOM: Self = Self {
        name: "fathom",
        symbols: &["ftm"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: 1.8288,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const FURLONG: Self = Self {
        name: "furlong",
        symbols: &["fur"],
        scale: ScaleExponents::_10(2),
        conversion_factor: 2.01168,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const MILE: Self = Self {
        name: "mile",
        symbols: &["mi"],
        scale: ScaleExponents::_10(3),
        conversion_factor: 1.609344,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const NAUTICAL_MILE: Self = Self {
        name: "nautical_mile",
        symbols: &["nmi"],
        scale: ScaleExponents::_10(3),
        conversion_factor: 1.852,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const ASTRONOMICAL_UNIT: Self = Self {
        name: "astronomical_unit",
        symbols: &["AU"],
        scale: ScaleExponents::_10(11),
        conversion_factor: 1.495978707,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Astronomical,
    };

    pub const LIGHT_YEAR: Self = Self {
        name: "light_year",
        symbols: &["ly"],
        scale: ScaleExponents::_10(16),
        conversion_factor: 0.94607304725808,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Astronomical,
    };

    pub const PARSEC: Self = Self {
        name: "parsec",
        symbols: &["pc"],
        scale: ScaleExponents::_10(16),
        conversion_factor: 3.08567758128,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Astronomical,
    };
}

/// Time
impl Unit<crate::dimension_exponents!([0, 0, 1, 0, 0, 0, 0, 0])> {
    pub const SECOND: Self = Self {
        name: "second",
        symbols: &["s"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const MINUTE: Self = Self {
        name: "minute",
        symbols: &["min"],
        scale: ScaleExponents::_10(1).mul(ScaleExponents::_6(1)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const HOUR: Self = Self {
        name: "hour",
        symbols: &["h", "hr"],
        scale: ScaleExponents::_10(2).mul(ScaleExponents::_6(2)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const DAY: Self = Self {
        name: "day",
        symbols: &["d"],
        scale: ScaleExponents::_10(2)
            .mul(ScaleExponents::_6(3))
            .mul(ScaleExponents::_2(2)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const WEEK: Self = Self {
        name: "week",
        symbols: &["wk"],
        scale: ScaleExponents::_10(3)
            .mul(ScaleExponents::_6(3))
            .mul(ScaleExponents::_2(2)),
        conversion_factor: 0.7,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const MONTH: Self = Self {
        // 30 days
        name: "month",
        symbols: &["mo"],
        scale: ScaleExponents::_10(3)
            .mul(ScaleExponents::_6(4))
            .mul(ScaleExponents::_2(1)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const YEAR: Self = Self {
        // solar year, not calendar year
        name: "year",
        symbols: &["yr"],
        scale: ScaleExponents::_10(7),
        conversion_factor: 3.1556926,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Current
impl Unit<crate::dimension_exponents!([0, 0, 0, 1, 0, 0, 0, 0])> {
    pub const AMPERE: Self = Self {
        name: "ampere",
        symbols: &["A"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Temperature
impl Unit<crate::dimension_exponents!([0, 0, 0, 0, 1, 0, 0, 0])> {
    pub const KELVIN: Self = Self {
        name: "kelvin",
        symbols: &["K"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const CELSIUS: Self = Self {
        name: "celsius",
        symbols: &["degC"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: 273.15,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const RANKINE: Self = Self {
        name: "rankine",
        symbols: &["degR"],
        scale: ScaleExponents([0, -2, 1, 0]),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const FAHRENHEIT: Self = Self {
        name: "fahrenheit",
        symbols: &["degF"],
        scale: ScaleExponents([0, -2, 1, 0]),
        conversion_factor: IDENTITY,
        affine_offset: 459.7,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
}

/// Amount
impl Unit<crate::dimension_exponents!([0, 0, 0, 0, 0, 1, 0, 0])> {
    pub const MOLE: Self = Self {
        name: "mole",
        symbols: &["mol"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Angle
impl Unit<crate::dimension_exponents!([0, 0, 0, 0, 0, 0, 0, 1])> {
    pub const RADIAN: Self = Self {
        name: "radian",
        symbols: &["rad"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const DEGREE: Self = Self {
        name: "degree",
        symbols: &["deg"],
        scale: ScaleExponents([-2, -2, -1, 1]),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const GRADIAN: Self = Self {
        name: "gradian",
        symbols: &["grad"],
        scale: ScaleExponents([-3, -1, -1, 1]),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const TURN: Self = Self {
        name: "turn",
        symbols: &["rot", "turn"],
        scale: ScaleExponents([1, 0, 0, 1]),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const ARCMINUTE: Self = Self {
        name: "arcminute",
        symbols: &["arcmin"],
        scale: ScaleExponents([-4, -2, -2, 1]),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const ARCSECOND: Self = Self {
        name: "arcsecond",
        symbols: &["arcsec"],
        scale: ScaleExponents([-6, -2, -2, 1]),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Frequency
impl Unit<crate::dimension_exponents!([0, 0, -1, 0, 0, 0, 0, 0])> {
    pub const HERTZ: Self = Self {
        name: "hertz",
        symbols: &["Hz"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Force
impl Unit<crate::dimension_exponents!([1, 1, -2, 0, 0, 0, 0, 0])> {
    pub const NEWTON: Self = Self {
        name: "newton",
        symbols: &["N"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Energy and work
impl Unit<crate::dimension_exponents!([1, 2, -2, 0, 0, 0, 0, 0])> {
    pub const JOULE: Self = Self {
        name: "joule",
        symbols: &["J"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const NEWTON_METER: Self = Self {
        name: "newton_meter",
        symbols: &["Nm"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const ELECTRONVOLT: Self = Self {
        name: "electron_volt",
        symbols: &["eV"],
        scale: ScaleExponents::_10(-19),
        conversion_factor: 1.602176634,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const ERG: Self = Self {
        name: "erg",
        symbols: &["erg"],
        scale: ScaleExponents::_10(-7),
        conversion_factor: 1.0,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const CALORIE: Self = Self {
        name: "calorie",
        symbols: &["cal"],
        scale: ScaleExponents::_10(1),
        conversion_factor: 0.4184,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const FOOT_POUND: Self = Self {
        name: "foot_pound",
        symbols: &["ft_lb"],
        scale: ScaleExponents::_10(1),
        conversion_factor: 1.3558179483314004,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const KILOWATT_HOUR: Self = Self {
        name: "kilowatt_hour",
        symbols: &["kWh"],
        scale: ScaleExponents::_10(5).mul(ScaleExponents::_6(2)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const THERM: Self = Self {
        name: "therm",
        symbols: &["thm"],
        scale: ScaleExponents::_10(8),
        conversion_factor: 1.05505585262,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
}

/// Power
impl Unit<crate::dimension_exponents!([1, 2, -3, 0, 0, 0, 0, 0])> {
    pub const WATT: Self = Self {
        name: "watt",
        symbols: &["W"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const HORSEPOWER: Self = Self {
        name: "horsepower",
        symbols: &["hp"],
        scale: ScaleExponents::_10(3),
        conversion_factor: 0.7456998715822702,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
}

/// Pressure
impl Unit<crate::dimension_exponents!([1, -1, -2, 0, 0, 0, 0, 0])> {
    pub const PASCAL: Self = Self {
        name: "pascal",
        symbols: &["Pa"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const TORR: Self = Self {
        name: "torr",
        symbols: &["Torr"],
        scale: ScaleExponents::_10(2),
        conversion_factor: 1.3332236842105263,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const PSI: Self = Self {
        name: "psi",
        symbols: &["psi"],
        scale: ScaleExponents::_10(4),
        conversion_factor: 0.6894757293168361,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const BAR: Self = Self {
        name: "bar",
        symbols: &["bar"],
        scale: ScaleExponents::_10(5),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const ATMOSPHERE: Self = Self {
        name: "atmosphere",
        symbols: &["atm"],
        scale: ScaleExponents::_10(5),
        conversion_factor: 1.01325,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Electric charge
impl Unit<crate::dimension_exponents!([0, 0, 1, 1, 0, 0, 0, 0])> {
    pub const COULOMB: Self = Self {
        name: "coulomb",
        symbols: &["C"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Electric potential
impl Unit<crate::dimension_exponents!([1, 2, -3, -1, 0, 0, 0, 0])> {
    pub const VOLT: Self = Self {
        name: "volt",
        symbols: &["V"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Capacitance
impl Unit<crate::dimension_exponents!([-1, -2, 4, 2, 0, 0, 0, 0])> {
    pub const FARAD: Self = Self {
        name: "farad",
        symbols: &["F"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

// Electric resistance
impl Unit<crate::dimension_exponents!([1, 2, -3, -2, 0, 0, 0, 0])> {
    pub const OHM: Self = Self {
        name: "ohm",
        symbols: &["Ω"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Electric conductance
impl Unit<crate::dimension_exponents!([-1, -2, 3, 2, 0, 0, 0, 0])> {
    pub const SIEMENS: Self = Self {
        name: "siemens",
        symbols: &["S"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Inductance
impl Unit<crate::dimension_exponents!([1, 2, -2, -2, 0, 0, 0, 0])> {
    pub const HENRY: Self = Self {
        name: "henry",
        symbols: &["H"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Magnetic field
impl Unit<crate::dimension_exponents!([1, 0, -2, -1, 0, 0, 0, 0])> {
    pub const TESLA: Self = Self {
        name: "tesla",
        symbols: &["T"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const GAUSS: Self = Self {
        name: "gauss",
        symbols: &["G"],
        scale: ScaleExponents::_10(-4),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Magnetic flex
impl Unit<crate::dimension_exponents!([1, 2, -2, -1, 0, 0, 0, 0])> {
    pub const WEBER: Self = Self {
        name: "weber",
        symbols: &["Wb"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Illuminance
impl Unit<crate::dimension_exponents!([0, -2, 0, 0, 0, 0, 1, 0])> {
    pub const LUX: Self = Self {
        name: "lux",
        symbols: &["lx"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const LUMEN: Self = Self {
        name: "lumen",
        symbols: &["lm"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Luminous intensity
impl Unit<crate::dimension_exponents!([0, 0, 0, 0, 0, 0, 1, 0])> {
    pub const CANDELA: Self = Self {
        name: "candela",
        symbols: &["cd"],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Kinematic viscosity
impl Unit<crate::dimension_exponents!([0, 2, -1, 0, 0, 0, 0, 0])> {
    pub const STOKES: Self = Self {
        name: "stokes",
        symbols: &["St"],
        scale: ScaleExponents::_10(-4),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

/// Area
impl Unit<crate::dimension_exponents!([0, 2, 0, 0, 0, 0, 0, 0])> {
    pub const HECTARE: Self = Self {
        name: "hectare",
        symbols: &["hect"],
        scale: ScaleExponents::_10(4),
        conversion_factor: IDENTITY, // 1 hectare = 10,000 m²
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const ACRE: Self = Self {
        name: "acre",
        symbols: &["acre"],
        scale: ScaleExponents::_10(3),
        conversion_factor: 0.40468564224, // 1 acre = 4046.8564224 m²
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
}

/// Volume
impl Unit<crate::dimension_exponents!([0, 3, 0, 0, 0, 0, 0, 0])> {
    // Metric volume units
    pub const LITER: Self = Self {
        name: "liter",
        symbols: &["L", "l"],
        scale: ScaleExponents::_10(-3),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const GALLON_US: Self = Self {
        name: "gallon",
        symbols: &["gal", "gallon"],
        scale: ScaleExponents::_10(-2),
        conversion_factor: 0.3785411784, // 1 US gallon = 3.785411784 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const GALLON_UK: Self = Self {
        name: "uk_gallon",
        symbols: &["uk_gal"],
        scale: ScaleExponents::_10(-2),
        conversion_factor: 0.454609, // 1 UK gallon = 4.54609 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const QUART_US: Self = Self {
        name: "quart",
        symbols: &["qrt"],
        scale: ScaleExponents::_10(-3),
        conversion_factor: 0.946352946, // 1 US quart = 0.946352946 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const QUART_UK: Self = Self {
        name: "uk_quart",
        symbols: &["uk_qrt"],
        scale: ScaleExponents::_10(-3),
        conversion_factor: 1.1365225, // 1 UK quart = 1.1365225 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const PINT_US: Self = Self {
        name: "pint",
        symbols: &["pnt"],
        scale: ScaleExponents::_10(-3),
        conversion_factor: 0.473176473, // 1 US pint = 0.473176473 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const PINT_UK: Self = Self {
        name: "uk_pint",
        symbols: &["uk_pnt"],
        scale: ScaleExponents::_10(-3),
        conversion_factor: 0.56826125, // 1 UK pint = 0.56826125 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const CUP_US: Self = Self {
        name: "cup",
        symbols: &["cup"],
        scale: ScaleExponents::_10(-4),
        conversion_factor: 2.365882365, // 1 US cup = 0.2365882365 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const CUP_UK: Self = Self {
        name: "uk_cup",
        symbols: &["uk_cup"],
        scale: ScaleExponents::_10(-4),
        conversion_factor: 2.84130625, // 1 UK cup = 0.284130625 L
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const FLUID_OUNCE_US: Self = Self {
        name: "fluid_ounce",
        symbols: &["fl_oz"],
        scale: ScaleExponents::_10(-5),
        conversion_factor: 2.95735295625,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const FLUID_OUNCE_UK: Self = Self {
        name: "uk_fluid_ounce",
        symbols: &["uk_fl_oz"],
        scale: ScaleExponents::_10(-5),
        conversion_factor: 2.84130625,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const METRIC_FLUID_OUNCE: Self = Self {
        name: "metric_fluid_ounce",
        symbols: &["metric_fl_oz"],
        scale: ScaleExponents::_10(-5).mul(ScaleExponents::_3(1)),
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };

    pub const TABLESPOON_US: Self = Self {
        name: "tablespoon",
        symbols: &["tbsp"],
        scale: ScaleExponents::_10(-5),
        conversion_factor: 1.478676478125,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const TABLESPOON_UK: Self = Self {
        name: "uk_tablespoon",
        symbols: &["uk_tbsp"],
        scale: ScaleExponents::_10(-5),
        conversion_factor: 1.77581640625,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const TEASPOON_US: Self = Self {
        name: "teaspoon",
        symbols: &["tsp"],
        scale: ScaleExponents::_10(-5),
        conversion_factor: 0.492892159375,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const TEASPOON_UK: Self = Self {
        name: "uk_teaspoon",
        symbols: &["uk_tsp"],
        scale: ScaleExponents::_10(-5),
        conversion_factor: 0.59193880208333,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };

    pub const BUSHEL: Self = Self {
        name: "bushel",
        symbols: &["bu"],
        scale: ScaleExponents::_10(-1),
        conversion_factor: 0.3523907016688,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Imperial,
    };
}

/// Dimensionless
impl Unit<crate::dimension_exponents!([0, 0, 0, 0, 0, 0, 0, 0])> {
    pub const DIMENSIONLESS: Self = Self {
        name: "dimensionless",
        symbols: &[],
        scale: ScaleExponents::IDENTITY,
        conversion_factor: IDENTITY,
        affine_offset: NONE,
        exponents: TypeDimensionExponents::new(),
        system: System::Metric,
    };
}

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

    #[test]
    fn can_get_prefix_and_unit_from_multiple_string() {
        assert_eq!(
            Unit::multiple_to_base_unit("kilometer").unwrap(),
            (&Unit::METER.erase(), Some(&SiPrefix::KILO),)
        );

        assert_eq!(
            Unit::multiple_to_base_unit("millisecond").unwrap(),
            (&Unit::SECOND.erase(), Some(&SiPrefix::MILLI),)
        );

        assert_eq!(
            Unit::multiple_to_base_unit("gram").unwrap(),
            (&Unit::GRAM.erase(), None,)
        );

        assert_eq!(Unit::multiple_to_base_unit("abc"), None,);
    }
}