Skip to main content

use_compound/
compound_formula.rs

1use std::fmt;
2
3use use_chemical_formula::ChemicalFormula;
4
5/// A compound-facing chemical formula wrapper.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct CompoundFormula(ChemicalFormula);
8
9impl CompoundFormula {
10    /// Creates a compound formula wrapper.
11    #[must_use]
12    pub const fn new(formula: ChemicalFormula) -> Self {
13        Self(formula)
14    }
15
16    /// Returns the wrapped formula.
17    #[must_use]
18    pub const fn as_formula(&self) -> &ChemicalFormula {
19        &self.0
20    }
21
22    /// Consumes the wrapper and returns the formula.
23    #[must_use]
24    pub fn into_formula(self) -> ChemicalFormula {
25        self.0
26    }
27}
28
29impl From<ChemicalFormula> for CompoundFormula {
30    fn from(value: ChemicalFormula) -> Self {
31        Self::new(value)
32    }
33}
34
35impl AsRef<ChemicalFormula> for CompoundFormula {
36    fn as_ref(&self) -> &ChemicalFormula {
37        self.as_formula()
38    }
39}
40
41impl fmt::Display for CompoundFormula {
42    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(formatter, "{}", self.0)
44    }
45}
46
47/// An empirical formula wrapper.
48#[derive(Clone, Debug, Eq, PartialEq)]
49pub struct EmpiricalFormula(ChemicalFormula);
50
51impl EmpiricalFormula {
52    /// Creates an empirical formula wrapper.
53    #[must_use]
54    pub const fn new(formula: ChemicalFormula) -> Self {
55        Self(formula)
56    }
57
58    /// Returns the wrapped formula.
59    #[must_use]
60    pub const fn as_formula(&self) -> &ChemicalFormula {
61        &self.0
62    }
63
64    /// Consumes the wrapper and returns the formula.
65    #[must_use]
66    pub fn into_formula(self) -> ChemicalFormula {
67        self.0
68    }
69}
70
71impl From<ChemicalFormula> for EmpiricalFormula {
72    fn from(value: ChemicalFormula) -> Self {
73        Self::new(value)
74    }
75}
76
77impl AsRef<ChemicalFormula> for EmpiricalFormula {
78    fn as_ref(&self) -> &ChemicalFormula {
79        self.as_formula()
80    }
81}
82
83impl fmt::Display for EmpiricalFormula {
84    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
85        write!(formatter, "{}", self.0)
86    }
87}
88
89/// A molecular formula wrapper.
90#[derive(Clone, Debug, Eq, PartialEq)]
91pub struct MolecularFormula(ChemicalFormula);
92
93impl MolecularFormula {
94    /// Creates a molecular formula wrapper.
95    #[must_use]
96    pub const fn new(formula: ChemicalFormula) -> Self {
97        Self(formula)
98    }
99
100    /// Returns the wrapped formula.
101    #[must_use]
102    pub const fn as_formula(&self) -> &ChemicalFormula {
103        &self.0
104    }
105
106    /// Consumes the wrapper and returns the formula.
107    #[must_use]
108    pub fn into_formula(self) -> ChemicalFormula {
109        self.0
110    }
111}
112
113impl From<ChemicalFormula> for MolecularFormula {
114    fn from(value: ChemicalFormula) -> Self {
115        Self::new(value)
116    }
117}
118
119impl AsRef<ChemicalFormula> for MolecularFormula {
120    fn as_ref(&self) -> &ChemicalFormula {
121        self.as_formula()
122    }
123}
124
125impl fmt::Display for MolecularFormula {
126    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
127        write!(formatter, "{}", self.0)
128    }
129}