Skip to main content

use_ion/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Errors returned when constructing ion values.
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub enum IonValidationError {
7    /// An ion name is empty.
8    EmptyName,
9    /// A charge magnitude is zero.
10    ZeroChargeMagnitude,
11    /// An oxidation-state label is empty.
12    EmptyOxidationStateLabel,
13    /// A positive ion was required.
14    ExpectedCation,
15    /// A negative ion was required.
16    ExpectedAnion,
17    /// A monatomic formula was required.
18    ExpectedMonatomicFormula,
19    /// A polyatomic formula was required.
20    ExpectedPolyatomicFormula,
21}
22
23impl fmt::Display for IonValidationError {
24    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::EmptyName => formatter.write_str("ion name must not be empty"),
27            Self::ZeroChargeMagnitude => {
28                formatter.write_str("ion charge magnitude must be greater than zero")
29            },
30            Self::EmptyOxidationStateLabel => {
31                formatter.write_str("oxidation-state label must not be empty")
32            },
33            Self::ExpectedCation => formatter.write_str("ion must have a positive charge"),
34            Self::ExpectedAnion => formatter.write_str("ion must have a negative charge"),
35            Self::ExpectedMonatomicFormula => {
36                formatter.write_str("ion formula must contain exactly one atom")
37            },
38            Self::ExpectedPolyatomicFormula => {
39                formatter.write_str("ion formula must contain more than one atom")
40            },
41        }
42    }
43}
44
45impl Error for IonValidationError {}