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
use crate::dimension_exponents::{
    DimensionExponents, DynDimensionExponents, TypeDimensionExponents,
};
use crate::prefix::SiPrefix;
use crate::units::Unit;

/// A dimension and its associated units.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Dimension<ExponentsType: 'static = DynDimensionExponents> {
    /// Name of the dimension.
    pub name: &'static str,

    /// Symbol for the dimension.
    pub symbol: &'static str,

    /// Point in dimension space this dimension is for.
    pub exponents: ExponentsType,

    /// All supported units for this dimension.
    pub units: &'static [Unit<ExponentsType>],
    units_erased: &'static [Unit],
}

impl Dimension {
    /// Find a dimension by its name or symbol.
    pub fn find_dimension(name_or_symbol: &str) -> Option<&'static Self> {
        Self::ALL
            .iter()
            .find(|dim| dim.symbol == name_or_symbol || dim.name == name_or_symbol)
    }

    /// Find a unit by its symbol across all dimensions.
    pub fn find_unit_by_name(name: &str) -> Option<(&'static Unit, &'static Self)> {
        Self::ALL.iter().find_map(|dimension| {
            dimension
                .units
                .iter()
                .find(|unit| unit.name == name)
                .map(|unit| (unit, dimension))
        })
    }

    /// Find a unit by its symbol across all dimensions.
    pub fn find_unit_by_symbol(symbol: &str) -> Option<(&'static Unit, &'static Dimension)> {
        Self::ALL.iter().find_map(|dimension| {
            dimension
                .units
                .iter()
                .find(|unit| unit.symbols.contains(&symbol))
                .map(|unit| (unit, dimension))
        })
    }

    /// Find a unit by its symbol across all dimensions.
    pub fn find_si_unit_by_name(name: &str) -> Option<(&'static Unit, &'static Dimension)> {
        Self::ALL.iter().find_map(|dimension| {
            dimension
                .units
                .iter()
                .find(|unit| {
                    unit.name == name
                        && unit.exponents.as_basis().is_some()
                        && !unit.has_conversion()
                })
                .map(|unit| (unit, dimension))
        })
    }

    /// Find a unit by its symbol across all dimensions.
    pub fn find_si_unit_by_symbol(symbol: &str) -> Option<(&'static Unit, &'static Dimension)> {
        Self::ALL.iter().find_map(|dimension| {
            dimension
                .units
                .iter()
                .find(|unit| {
                    unit.symbols.contains(&symbol)
                        && unit.exponents.as_basis().is_some()
                        && !unit.has_conversion()
                })
                .map(|unit| (unit, dimension))
        })
    }

    /// Find a dimension by its exponents.
    pub fn find_dimension_by_exponents(
        exponents: DynDimensionExponents,
    ) -> Option<&'static Dimension> {
        Self::ALL.iter().find(|dim| dim.exponents == exponents)
    }

    /// Iterator over all unit names.
    pub fn names<'r>(
        dims: impl IntoIterator<Item = &'r Self>,
    ) -> impl Iterator<Item = &'static str> {
        dims.into_iter()
            .flat_map(|dimension| dimension.units.iter().map(move |unit| unit.name))
    }

    /// Iterator over all unit dymbols.
    pub fn symbols<'r>(
        dims: impl IntoIterator<Item = &'r Self>,
    ) -> impl Iterator<Item = &'static str> {
        dims.into_iter().flat_map(|dimension| {
            dimension
                .units
                .iter()
                .flat_map(move |unit| unit.symbols.iter().copied())
        })
    }

    /// Get all non SI base units.
    pub fn non_si_base_units() -> impl Iterator<Item = (&'static Unit, &'static Dimension)> {
        Self::ALL.iter().flat_map(|dimension| {
            dimension.units.iter().filter_map(move |unit| {
                if unit.exponents.as_basis().is_some() {
                    None
                } else {
                    Some((unit, dimension))
                }
            })
        })
    }

    /// Get all pure SI base units.
    pub fn si_base_units() -> impl Iterator<Item = (&'static Unit, &'static Dimension)> {
        Self::ALL.iter().flat_map(|dimension| {
            dimension.units.iter().filter_map(move |unit| {
                if unit.exponents.as_basis().is_some() {
                    Some((unit, dimension))
                } else {
                    None
                }
            })
        })
    }

    /// Find a unit by name or symbol across all dimensions.
    pub fn find_by_literal(
        literal: &str,
    ) -> Option<(&'static Unit, &'static Dimension, Option<&'static SiPrefix>)> {
        // First try the literal as-is
        if let Some((prefix, base)) = SiPrefix::strip_any_prefix_name(literal)
            && let Some((unit, dimension)) = Self::find_si_unit_by_name(base)
        {
            return Some((unit, dimension, Some(prefix)));
        }

        if let Some((prefix, base)) = SiPrefix::strip_any_prefix_symbol(literal)
            && let Some((unit, dimension)) = Self::find_si_unit_by_symbol(base)
        {
            return Some((unit, dimension, Some(prefix)));
        }

        if let Some((unit, dimension)) = Self::find_unit_by_name(literal) {
            return Some((unit, dimension, None));
        } else if let Some((unit, dimension)) = Self::find_unit_by_symbol(literal) {
            return Some((unit, dimension, None));
        }

        // If not found, try with plural "s" stripped (but only if the result is not empty)
        if literal.len() > 1 {
            let literal_singular = literal.trim_end_matches("s");
            if !literal_singular.is_empty() {
                if let Some((prefix, base)) = SiPrefix::strip_any_prefix_name(literal_singular)
                    && let Some((unit, dimension)) = Self::find_si_unit_by_name(base)
                {
                    return Some((unit, dimension, Some(prefix)));
                }

                if let Some((prefix, base)) = SiPrefix::strip_any_prefix_symbol(literal_singular)
                    && let Some((unit, dimension)) = Self::find_si_unit_by_symbol(base)
                {
                    return Some((unit, dimension, Some(prefix)));
                }

                if let Some((unit, dimension)) = Self::find_unit_by_name(literal_singular) {
                    return Some((unit, dimension, None));
                } else if let Some((unit, dimension)) = Self::find_unit_by_symbol(literal_singular)
                {
                    return Some((unit, dimension, None));
                }
            }
        }

        None
    }
}

impl Dimension {
    /// All dimensions and their units.
    pub const ALL: &[Self] = &Self::ALL_FIXED;

    /// Atomic dimensions.
    pub const BASIS: [Self; 8] = [
        Dimension::MASS.erase(),
        Dimension::LENGTH.erase(),
        Dimension::TIME.erase(),
        Dimension::CURRENT.erase(),
        Dimension::TEMPERATURE.erase(),
        Dimension::AMOUNT.erase(),
        Dimension::LUMINOSITY.erase(),
        Dimension::ANGLE.erase(),
    ];

    const ALL_FIXED: [Self; 28] = [
        Dimension::MASS.erase(),
        Dimension::LENGTH.erase(),
        Dimension::TIME.erase(),
        Dimension::CURRENT.erase(),
        Dimension::TEMPERATURE.erase(),
        Dimension::AMOUNT.erase(),
        Dimension::LUMINOSITY.erase(),
        Dimension::ANGLE.erase(),
        Dimension::AREA.erase(),
        Dimension::VOLUME.erase(),
        Dimension::FREQUENCY.erase(),
        Dimension::FORCE.erase(),
        Dimension::ENERGY.erase(),
        Dimension::POWER.erase(),
        Dimension::PRESSURE.erase(),
        Dimension::ELECTRIC_CHARGE.erase(),
        Dimension::ELECTRIC_POTENTIAL.erase(),
        Dimension::CAPACITANCE.erase(),
        Dimension::ELECTRIC_RESISTANCE.erase(),
        Dimension::ELECTRIC_CONDUCTANCE.erase(),
        Dimension::INDUCTANCE.erase(),
        Dimension::MAGNETIC_FIELD.erase(),
        Dimension::MAGNETIC_FLUX.erase(),
        Dimension::ILLUMINANCE.erase(),
        Dimension::VOLUME_MASS_DENSITY.erase(),
        Dimension::LINEAR_MASS_DENSITY.erase(),
        Dimension::DYNAMIC_VISCOSITY.erase(),
        Dimension::KINEMATIC_VISCOSITY.erase(),
    ];
}

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,
>
    Dimension<
        crate::dimension_exponents!([
            MASS_EXP,
            LENGTH_EXP,
            TIME_EXP,
            CURRENT_EXP,
            TEMPERATURE_EXP,
            AMOUNT_EXP,
            LUMINOSITY_EXP,
            ANGLE_EXP,
        ]),
    >
{
    pub const fn erase(&self) -> Dimension {
        Dimension {
            name: self.name,
            symbol: self.symbol,
            exponents: self.exponents.value_const(),
            units: self.units_erased,
            units_erased: self.units_erased,
        }
    }
}

impl Dimension<crate::dimension_exponents!([1, 0, 0, 0, 0, 0, 0, 0])> {
    pub const MASS: Self = __dim!(Self {
        name: "Mass",
        symbol: "M",
        units: &[
            Unit::GRAM,
            Unit::GRAIN,
            Unit::CARAT,
            Unit::OUNCE,
            Unit::TROY_OUNCE,
            Unit::TROY_POUND,
            Unit::POUND,
            Unit::STONE,
            Unit::TON
        ],
    });
}

impl Dimension<crate::dimension_exponents!([0, 1, 0, 0, 0, 0, 0, 0])> {
    pub const LENGTH: Self = __dim!(Self {
        name: "Length",
        symbol: "L",
        units: &[
            Unit::METER,
            Unit::INCH,
            Unit::FOOT,
            Unit::YARD,
            Unit::MILE,
            Unit::NAUTICAL_MILE,
            Unit::ASTRONOMICAL_UNIT,
            Unit::LIGHT_YEAR,
            Unit::PARSEC
        ],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 1, 0, 0, 0, 0, 0])> {
    pub const TIME: Self = __dim!(Self {
        name: "Time",
        symbol: "T",
        units: &[
            Unit::SECOND,
            Unit::MINUTE,
            Unit::HOUR,
            Unit::DAY,
            Unit::WEEK,
            Unit::MONTH,
            Unit::YEAR
        ],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 0, 1, 0, 0, 0, 0])> {
    pub const CURRENT: Self = __dim!(Self {
        name: "Current",
        symbol: "I",
        units: &[Unit::AMPERE],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 0, 0, 1, 0, 0, 0])> {
    pub const TEMPERATURE: Self = __dim!(Self {
        name: "Temperature",
        symbol: "θ",
        units: &[Unit::KELVIN, Unit::CELSIUS, Unit::RANKINE, Unit::FAHRENHEIT],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 0, 0, 0, 1, 0, 0])> {
    pub const AMOUNT: Self = __dim!(Self {
        name: "Amount",
        symbol: "N",
        units: &[Unit::MOLE],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 0, 0, 0, 0, 1, 0])> {
    pub const LUMINOSITY: Self = __dim!(Self {
        name: "Luminosity",
        symbol: "Cd",
        units: &[Unit::CANDELA],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 0, 0, 0, 0, 0, 1])> {
    pub const ANGLE: Self = __dim!(Self {
        name: "Angle",
        symbol: "A",
        units: &[
            Unit::RADIAN,
            Unit::DEGREE,
            Unit::GRADIAN,
            Unit::TURN,
            Unit::ARCMINUTE,
            Unit::ARCSECOND,
        ]
    });
}

impl Dimension<crate::dimension_exponents!([0, 2, 0, 0, 0, 0, 0, 0])> {
    pub const AREA: Self = __dim!(Self {
        name: "Area",
        symbol: "",
        units: &[Unit::HECTARE, Unit::ACRE,],
    });
}

impl Dimension<crate::dimension_exponents!([0, 3, 0, 0, 0, 0, 0, 0])> {
    pub const VOLUME: Self = __dim!(Self {
        name: "Volume",
        symbol: "",
        units: &[
            // Metric volume units
            Unit::LITER,
            // Imperial volume units
            Unit::GALLON_US,
            Unit::QUART_US,
            Unit::PINT_US,
            Unit::CUP_US,
            Unit::FLUID_OUNCE_US,
            Unit::TABLESPOON_US,
            Unit::TEASPOON_US,
            // UK Imperial volume units
            Unit::GALLON_UK,
            Unit::QUART_UK,
            Unit::PINT_UK,
            Unit::CUP_UK,
            Unit::FLUID_OUNCE_UK,
            Unit::TABLESPOON_UK,
            Unit::TEASPOON_UK,
            Unit::BUSHEL
        ],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, -1, 0, 0, 0, 0, 0])> {
    pub const FREQUENCY: Self = __dim!(Self {
        name: "Frequency",
        symbol: "T⁻¹",
        units: &[Unit::HERTZ],
    });
}

impl Dimension<crate::dimension_exponents!([1, 1, -2, 0, 0, 0, 0, 0])> {
    pub const FORCE: Self = __dim!(Self {
        name: "Force",
        symbol: "MLT⁻²",
        units: &[Unit::NEWTON],
    });
}

impl Dimension<crate::dimension_exponents!([1, 2, -2, 0, 0, 0, 0, 0])> {
    pub const ENERGY: Self = __dim!(Self {
        name: "Energy",
        symbol: "ML²T⁻²",
        units: &[
            Unit::JOULE,
            Unit::ELECTRONVOLT,
            Unit::ERG,
            Unit::NEWTON_METER,
            Unit::CALORIE,
            Unit::FOOT_POUND,
            Unit::KILOWATT_HOUR,
            Unit::THERM
        ],
    });
}

impl Dimension<crate::dimension_exponents!([1, 2, -3, 0, 0, 0, 0, 0])> {
    pub const POWER: Self = __dim!(Self {
        name: "Power",
        symbol: "ML²T⁻³",
        units: &[Unit::WATT, Unit::HORSEPOWER],
    });
}

impl Dimension<crate::dimension_exponents!([1, -1, -2, 0, 0, 0, 0, 0])> {
    pub const PRESSURE: Self = __dim!(Self {
        name: "Pressure",
        symbol: "ML⁻¹T⁻²",
        units: &[
            Unit::PASCAL,
            Unit::TORR,
            Unit::PSI,
            Unit::BAR,
            Unit::ATMOSPHERE
        ],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 1, 1, 0, 0, 0, 0])> {
    pub const ELECTRIC_CHARGE: Self = __dim!(Self {
        name: "Electric Charge",
        symbol: "TI",
        units: &[Unit::COULOMB],
    });
}

impl Dimension<crate::dimension_exponents!([1, 2, -3, -1, 0, 0, 0, 0])> {
    pub const ELECTRIC_POTENTIAL: Self = __dim!(Self {
        name: "Electric Potential",
        symbol: "ML²T⁻³I⁻¹",
        units: &[Unit::VOLT],
    });
}

impl Dimension<crate::dimension_exponents!([-1, -2, 4, 2, 0, 0, 0, 0])> {
    pub const CAPACITANCE: Self = __dim!(Self {
        name: "Capacitance",
        symbol: "M⁻¹L⁻²T⁴I²",
        units: &[Unit::FARAD],
    });
}

impl Dimension<crate::dimension_exponents!([1, 2, -3, -2, 0, 0, 0, 0])> {
    pub const ELECTRIC_RESISTANCE: Self = __dim!(Self {
        name: "Electric Resistance",
        symbol: "ML²T⁻³I⁻²",
        units: &[Unit::OHM],
    });
}

impl Dimension<crate::dimension_exponents!([-1, -2, 3, 2, 0, 0, 0, 0])> {
    pub const ELECTRIC_CONDUCTANCE: Self = __dim!(Self {
        name: "Electric Conductance",
        symbol: "M⁻¹L⁻²T³I²",
        units: &[Unit::SIEMENS],
    });
}

impl Dimension<crate::dimension_exponents!([1, 2, -2, -2, 0, 0, 0, 0])> {
    pub const INDUCTANCE: Self = __dim!(Self {
        name: "Inductance",
        symbol: "ML²T⁻²I⁻²",
        units: &[Unit::HENRY],
    });
}

impl Dimension<crate::dimension_exponents!([1, 0, -2, -1, 0, 0, 0, 0])> {
    pub const MAGNETIC_FIELD: Self = __dim!(Self {
        name: "Magnetic Field",
        symbol: "MT⁻²I⁻¹",
        units: &[Unit::TESLA, Unit::GAUSS],
    });
}

impl Dimension<crate::dimension_exponents!([1, 2, -2, -1, 0, 0, 0, 0])> {
    pub const MAGNETIC_FLUX: Self = __dim!(Self {
        name: "Magnetic Flux",
        symbol: "ML²T⁻²I⁻¹",
        units: &[Unit::WEBER],
    });
}

impl Dimension<crate::dimension_exponents!([0, -2, 0, 0, 0, 0, 1, 0])> {
    pub const ILLUMINANCE: Self = __dim!(Self {
        name: "Illuminance",
        symbol: "L⁻²Cd",
        units: &[Unit::LUX, Unit::LUMEN],
    });
}

impl Dimension<crate::dimension_exponents!([1, -3, 0, 0, 0, 0, 0, 0])> {
    pub const VOLUME_MASS_DENSITY: Self = __dim!(Self {
        name: "Volume Mass Density",
        symbol: "ML⁻³",
        units: &[
            // No atomic units for volume mass density - it's a derived dimension
        ],
    });
}

impl Dimension<crate::dimension_exponents!([1, -1, 0, 0, 0, 0, 0, 0])> {
    pub const LINEAR_MASS_DENSITY: Self = __dim!(Self {
        name: "Linear Mass Density",
        symbol: "ML⁻¹",
        units: &[
            // No atomic units for linear mass density - it's a derived dimension
        ],
    });
}

impl Dimension<crate::dimension_exponents!([1, -1, 1, 0, 0, 0, 0, 0])> {
    pub const DYNAMIC_VISCOSITY: Self = __dim!(Self {
        name: "Dynamic Viscosity",
        symbol: "ML⁻¹T⁻¹",
        units: &[
            // No atomic units for dynamic viscosity - it's a derived dimension
        ],
    });
}

impl Dimension<crate::dimension_exponents!([0, 2, -1, 0, 0, 0, 0, 0])> {
    pub const KINEMATIC_VISCOSITY: Self = __dim!(Self {
        name: "Kinematic Viscosity",
        symbol: "L²T⁻¹",
        units: &[Unit::STOKES],
    });
}

impl Dimension<crate::dimension_exponents!([0, 0, 0, 0, 0, 0, 0, 0])> {
    pub const NONE: Self = __dim!(Self {
        name: "dimensionless",
        symbol: "1",
        units: &[Unit::DIMENSIONLESS],
    });
}

pub struct DimensionExponentsFmt(DynDimensionExponents);

impl core::fmt::Display for DimensionExponentsFmt {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut first = true;
        for (d, x) in Dimension::BASIS.iter().zip(self.0.0) {
            if x == 0 {
                continue;
            }

            if first {
                first = false;
            } else {
                write!(f, " * ")?;
            }

            write!(f, "{}^{x}", d.units[0].symbols[0])?;
        }

        if first {
            write!(f, "1")?;
        }

        Ok(())
    }
}

macro_rules! __dim {
    (
        Self {
            name: $name:expr,
            symbol: $symbol:expr,
            units: &[$($unit:path),* $(,)?] $(,)?
        }
    ) => {
        Self {
            name: $name,
            symbol: $symbol,
            exponents: TypeDimensionExponents::new(),
            units: &[$($unit),*],
            units_erased: &[$($unit.erase()),*],
        }
    }
}
use __dim;

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

    #[test]
    fn can_find_dimension_by_literal_string() {
        assert_eq!(
            Dimension::find_by_literal("rad").unwrap(),
            (&Unit::RADIAN.erase(), &Dimension::ANGLE.erase(), None,)
        );

        assert_eq!(
            Dimension::find_by_literal("mrad").unwrap(),
            (
                &Unit::RADIAN.erase(),
                &Dimension::ANGLE.erase(),
                Some(&SiPrefix::MILLI),
            )
        );

        assert_eq!(
            Dimension::find_by_literal("megameter").unwrap(),
            (
                &Unit::METER.erase(),
                &Dimension::LENGTH.erase(),
                Some(&SiPrefix::MEGA),
            )
        );

        assert_eq!(
            Dimension::find_by_literal("Megameter").unwrap(),
            (
                &Unit::METER.erase(),
                &Dimension::LENGTH.erase(),
                Some(&SiPrefix::MEGA),
            )
        );

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

        assert_eq!(
            Dimension::find_by_literal("grams").unwrap(),
            (&Unit::GRAM.erase(), &Dimension::MASS.erase(), None,)
        );

        // Test that single-letter units like "s" (second) work correctly
        assert_eq!(
            Dimension::find_by_literal("s").unwrap(),
            (&Unit::SECOND.erase(), &Dimension::TIME.erase(), None,)
        );

        assert_eq!(Dimension::find_by_literal("millipound"), None,);

        assert_eq!(Dimension::find_by_literal("abc"), None,);
    }

    #[test]
    fn can_format_dimension_exponents() {
        assert_eq!(
            format!(
                "{}",
                DimensionExponentsFmt(Dimension::ENERGY.exponents.value())
            ),
            "g^1 * m^2 * s^-2"
        );

        assert_eq!(
            format!(
                "{}",
                DimensionExponentsFmt(Dimension::NONE.exponents.value())
            ),
            "1"
        );
    }
}