Skip to main content

use_reaction/
error.rs

1use std::error::Error;
2use std::fmt;
3
4use use_stoichiometry::StoichiometryValidationError;
5
6/// Errors returned when constructing or validating reaction values.
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub enum ReactionValidationError {
9    /// A reaction has no reactants and no products.
10    EmptyReaction,
11    /// A reaction has no reactants.
12    MissingReactants,
13    /// A reaction has no products.
14    MissingProducts,
15    /// A catalyst label is empty.
16    EmptyCatalystLabel,
17    /// A solvent label is empty.
18    EmptySolventLabel,
19    /// A temperature label is empty.
20    EmptyTemperatureLabel,
21    /// A pressure label is empty.
22    EmptyPressureLabel,
23    /// A generic condition label is empty.
24    EmptyConditionLabel,
25    /// A generic condition value is empty.
26    EmptyConditionValue,
27    /// A stoichiometry primitive rejected the value.
28    InvalidStoichiometry(StoichiometryValidationError),
29}
30
31impl fmt::Display for ReactionValidationError {
32    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::EmptyReaction => {
35                formatter.write_str("reaction must contain reactants and products")
36            },
37            Self::MissingReactants => {
38                formatter.write_str("reaction must contain at least one reactant")
39            },
40            Self::MissingProducts => {
41                formatter.write_str("reaction must contain at least one product")
42            },
43            Self::EmptyCatalystLabel => formatter.write_str("catalyst label must not be empty"),
44            Self::EmptySolventLabel => formatter.write_str("solvent label must not be empty"),
45            Self::EmptyTemperatureLabel => {
46                formatter.write_str("temperature label must not be empty")
47            },
48            Self::EmptyPressureLabel => formatter.write_str("pressure label must not be empty"),
49            Self::EmptyConditionLabel => formatter.write_str("condition label must not be empty"),
50            Self::EmptyConditionValue => formatter.write_str("condition value must not be empty"),
51            Self::InvalidStoichiometry(error) => {
52                write!(formatter, "invalid stoichiometry: {error}")
53            },
54        }
55    }
56}
57
58impl Error for ReactionValidationError {
59    fn source(&self) -> Option<&(dyn Error + 'static)> {
60        match self {
61            Self::InvalidStoichiometry(error) => Some(error),
62            _ => None,
63        }
64    }
65}
66
67impl From<StoichiometryValidationError> for ReactionValidationError {
68    fn from(error: StoichiometryValidationError) -> Self {
69        Self::InvalidStoichiometry(error)
70    }
71}