Skip to main content

use_molar_mass/
molar_mass_unit.rs

1use std::fmt;
2
3/// A display unit for molar mass values.
4#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
5pub enum MolarMassUnit {
6    /// Grams per mole.
7    #[default]
8    GramsPerMole,
9    /// Kilograms per mole.
10    KilogramsPerMole,
11}
12
13impl MolarMassUnit {
14    /// Returns the abbreviated unit label.
15    #[must_use]
16    pub const fn as_str(self) -> &'static str {
17        match self {
18            Self::GramsPerMole => "g/mol",
19            Self::KilogramsPerMole => "kg/mol",
20        }
21    }
22}
23
24impl fmt::Display for MolarMassUnit {
25    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26        formatter.write_str(self.as_str())
27    }
28}