Skip to main content

use_molar_mass/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Errors returned while constructing or calculating molar mass values.
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub enum MolarMassValidationError {
7    /// A molar mass value was not finite.
8    NonFiniteMolarMass,
9    /// A molar mass value was zero or negative.
10    NonPositiveMolarMass,
11    /// An element symbol does not match the supported chemical-formula shape.
12    InvalidElementSymbol(String),
13    /// An atomic mass value was not finite.
14    NonFiniteAtomicMass { symbol: String },
15    /// An atomic mass value was zero or negative.
16    NonPositiveAtomicMass { symbol: String },
17    /// An expanded formula count was zero.
18    ZeroElementCount { symbol: String },
19    /// An expanded formula count cannot fit the contribution count type.
20    FormulaCountTooLarge { symbol: String, count: u64 },
21    /// No atomic mass entry exists for a required element symbol.
22    MissingAtomicMass { symbol: String },
23}
24
25impl fmt::Display for MolarMassValidationError {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::NonFiniteMolarMass => formatter.write_str("molar mass must be finite"),
29            Self::NonPositiveMolarMass => {
30                formatter.write_str("molar mass must be greater than zero")
31            },
32            Self::InvalidElementSymbol(symbol) => {
33                write!(formatter, "invalid element symbol: {symbol}")
34            },
35            Self::NonFiniteAtomicMass { symbol } => {
36                write!(formatter, "atomic mass for {symbol} must be finite")
37            },
38            Self::NonPositiveAtomicMass { symbol } => {
39                write!(
40                    formatter,
41                    "atomic mass for {symbol} must be greater than zero"
42                )
43            },
44            Self::ZeroElementCount { symbol } => {
45                write!(
46                    formatter,
47                    "element count for {symbol} must be greater than zero"
48                )
49            },
50            Self::FormulaCountTooLarge { symbol, count } => {
51                write!(
52                    formatter,
53                    "element count for {symbol} is too large: {count}"
54                )
55            },
56            Self::MissingAtomicMass { symbol } => {
57                write!(
58                    formatter,
59                    "missing atomic mass for element symbol: {symbol}"
60                )
61            },
62        }
63    }
64}
65
66impl Error for MolarMassValidationError {}