Skip to main content

use_oxidation_state/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Errors returned when constructing oxidation-state values.
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub enum OxidationStateValidationError {
7    /// A signed oxidation state used zero magnitude.
8    ZeroSignedMagnitude,
9    /// A zero oxidation state used nonzero magnitude.
10    NonZeroZeroMagnitude,
11    /// A magnitude is above the supported range.
12    MagnitudeAboveMaximum { magnitude: u8, maximum: u8 },
13    /// A generic assignment label is empty.
14    EmptyAssignmentLabel,
15    /// An element symbol is empty.
16    EmptyElementSymbol,
17    /// An element symbol does not match the supported shape.
18    InvalidElementSymbol(String),
19    /// A formula or formula-context label is empty.
20    EmptyFormulaLabel,
21}
22
23impl fmt::Display for OxidationStateValidationError {
24    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::ZeroSignedMagnitude => {
27                formatter.write_str("signed oxidation-state magnitude must be greater than zero")
28            },
29            Self::NonZeroZeroMagnitude => {
30                formatter.write_str("zero oxidation state must have zero magnitude")
31            },
32            Self::MagnitudeAboveMaximum { magnitude, maximum } => write!(
33                formatter,
34                "oxidation-state magnitude {magnitude} is greater than maximum {maximum}"
35            ),
36            Self::EmptyAssignmentLabel => {
37                formatter.write_str("oxidation-state assignment label must not be empty")
38            },
39            Self::EmptyElementSymbol => formatter.write_str("element symbol must not be empty"),
40            Self::InvalidElementSymbol(symbol) => {
41                write!(formatter, "invalid element symbol: {symbol}")
42            },
43            Self::EmptyFormulaLabel => {
44                formatter.write_str("formula oxidation-state label must not be empty")
45            },
46        }
47    }
48}
49
50impl Error for OxidationStateValidationError {}