Skip to main content

use_reaction/
reactant.rs

1use std::fmt;
2
3use use_chemical_formula::ChemicalFormula;
4use use_stoichiometry::StoichiometricCoefficient;
5
6use crate::ReactionTerm;
7
8/// A reactant-side reaction term.
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct Reactant(ReactionTerm);
11
12impl Reactant {
13    /// Creates a reactant wrapper.
14    #[must_use]
15    pub const fn new(term: ReactionTerm) -> Self {
16        Self(term)
17    }
18
19    /// Returns the wrapped reaction term.
20    #[must_use]
21    pub const fn as_term(&self) -> &ReactionTerm {
22        &self.0
23    }
24
25    /// Consumes this value and returns the wrapped reaction term.
26    #[must_use]
27    pub fn into_term(self) -> ReactionTerm {
28        self.0
29    }
30
31    /// Returns the stoichiometric coefficient.
32    #[must_use]
33    pub const fn coefficient(&self) -> StoichiometricCoefficient {
34        self.0.coefficient()
35    }
36
37    /// Returns the chemical formula.
38    #[must_use]
39    pub const fn formula(&self) -> &ChemicalFormula {
40        self.0.formula()
41    }
42}
43
44impl From<ReactionTerm> for Reactant {
45    fn from(term: ReactionTerm) -> Self {
46        Self::new(term)
47    }
48}
49
50impl fmt::Display for Reactant {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(formatter, "{}", self.0)
53    }
54}