Skip to main content

use_ion/
ion_formula.rs

1use std::fmt;
2
3use use_chemical_formula::ChemicalFormula;
4
5/// An ion-facing chemical formula wrapper.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub struct IonFormula(ChemicalFormula);
8
9impl IonFormula {
10    /// Creates an ion 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 IonFormula {
30    fn from(value: ChemicalFormula) -> Self {
31        Self::new(value)
32    }
33}
34
35impl AsRef<ChemicalFormula> for IonFormula {
36    fn as_ref(&self) -> &ChemicalFormula {
37        self.as_formula()
38    }
39}
40
41impl fmt::Display for IonFormula {
42    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(formatter, "{}", self.0)
44    }
45}