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
use std::fmt;
use use_chemical_formula::ChemicalFormula;
use crate::{
ReactionSide, StoichiometricCoefficient, StoichiometricTerm, StoichiometryValidationError,
};
/// A stoichiometric entry on one side of a reaction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReactionEntry {
term: StoichiometricTerm,
side: ReactionSide,
}
impl ReactionEntry {
/// Creates a reaction entry.
///
/// # Errors
///
/// Returns [`StoichiometryValidationError::ZeroCoefficient`] if the coefficient is
/// structurally invalid.
pub fn new(
coefficient: StoichiometricCoefficient,
formula: ChemicalFormula,
side: ReactionSide,
) -> Result<Self, StoichiometryValidationError> {
Ok(Self {
term: StoichiometricTerm::new(coefficient, formula)?,
side,
})
}
/// Creates a reaction entry from a raw coefficient value.
///
/// # Errors
///
/// Returns [`StoichiometryValidationError::ZeroCoefficient`] when `coefficient` is zero.
pub fn from_value(
coefficient: u32,
formula: ChemicalFormula,
side: ReactionSide,
) -> Result<Self, StoichiometryValidationError> {
Self::new(StoichiometricCoefficient::new(coefficient)?, formula, side)
}
/// Returns the coefficient.
#[must_use]
pub const fn coefficient(&self) -> StoichiometricCoefficient {
self.term.coefficient()
}
/// Returns the formula.
#[must_use]
pub const fn formula(&self) -> &ChemicalFormula {
self.term.formula()
}
/// Returns the reaction side.
#[must_use]
pub const fn side(&self) -> ReactionSide {
self.side
}
/// Returns the stoichiometric term.
#[must_use]
pub const fn term(&self) -> &StoichiometricTerm {
&self.term
}
/// Consumes the entry and returns its parts.
#[must_use]
pub fn into_parts(self) -> (StoichiometricCoefficient, ChemicalFormula, ReactionSide) {
let (coefficient, formula) = self.term.into_parts();
(coefficient, formula, self.side)
}
}
impl fmt::Display for ReactionEntry {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.term)
}
}
/// A reactant-side reaction entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ReactantEntry(ReactionEntry);
impl ReactantEntry {
/// Creates a reactant entry.
///
/// # Errors
///
/// Returns [`StoichiometryValidationError::ZeroCoefficient`] if the coefficient is
/// structurally invalid.
pub fn new(
coefficient: StoichiometricCoefficient,
formula: ChemicalFormula,
) -> Result<Self, StoichiometryValidationError> {
Ok(Self(ReactionEntry::new(
coefficient,
formula,
ReactionSide::Reactant,
)?))
}
/// Creates a reactant wrapper from a reaction entry.
///
/// # Errors
///
/// Returns [`StoichiometryValidationError::ExpectedReactant`] when the entry is not a
/// reactant.
pub fn from_entry(entry: ReactionEntry) -> Result<Self, StoichiometryValidationError> {
if entry.side().is_reactant() {
Ok(Self(entry))
} else {
Err(StoichiometryValidationError::ExpectedReactant)
}
}
/// Returns the wrapped reaction entry.
#[must_use]
pub const fn as_entry(&self) -> &ReactionEntry {
&self.0
}
/// Consumes the wrapper and returns the reaction entry.
#[must_use]
pub fn into_entry(self) -> ReactionEntry {
self.0
}
}
impl fmt::Display for ReactantEntry {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}
/// A product-side reaction entry.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProductEntry(ReactionEntry);
impl ProductEntry {
/// Creates a product entry.
///
/// # Errors
///
/// Returns [`StoichiometryValidationError::ZeroCoefficient`] if the coefficient is
/// structurally invalid.
pub fn new(
coefficient: StoichiometricCoefficient,
formula: ChemicalFormula,
) -> Result<Self, StoichiometryValidationError> {
Ok(Self(ReactionEntry::new(
coefficient,
formula,
ReactionSide::Product,
)?))
}
/// Creates a product wrapper from a reaction entry.
///
/// # Errors
///
/// Returns [`StoichiometryValidationError::ExpectedProduct`] when the entry is not a
/// product.
pub fn from_entry(entry: ReactionEntry) -> Result<Self, StoichiometryValidationError> {
if entry.side().is_product() {
Ok(Self(entry))
} else {
Err(StoichiometryValidationError::ExpectedProduct)
}
}
/// Returns the wrapped reaction entry.
#[must_use]
pub const fn as_entry(&self) -> &ReactionEntry {
&self.0
}
/// Consumes the wrapper and returns the reaction entry.
#[must_use]
pub fn into_entry(self) -> ReactionEntry {
self.0
}
}
impl fmt::Display for ProductEntry {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}", self.0)
}
}