Skip to main content

use_molecule/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Errors returned when constructing molecule values.
5#[derive(Clone, Debug, Eq, PartialEq)]
6pub enum MoleculeValidationError {
7    /// A molecule name is empty.
8    EmptyName,
9    /// A builder was finalized without assigning a formula.
10    MissingFormula,
11    /// An atom label is empty.
12    EmptyAtomLabel,
13    /// An atom label does not match the supported element-symbol shape.
14    InvalidAtomLabel(String),
15    /// An atom identifier is empty.
16    EmptyAtomId,
17    /// A connection order was zero.
18    ZeroConnectionOrder,
19    /// A connection points from an atom to itself.
20    SelfConnection {
21        /// The self-connected atom index.
22        index: usize,
23    },
24    /// A connection references an atom index outside the explicit atom list.
25    InvalidConnectionIndex {
26        /// The invalid atom index.
27        index: usize,
28        /// The current explicit atom count.
29        atom_count: usize,
30    },
31}
32
33impl fmt::Display for MoleculeValidationError {
34    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            Self::EmptyName => formatter.write_str("molecule name must not be empty"),
37            Self::MissingFormula => formatter.write_str("molecule formula is required"),
38            Self::EmptyAtomLabel => formatter.write_str("atom label must not be empty"),
39            Self::InvalidAtomLabel(label) => write!(formatter, "invalid atom label: {label}"),
40            Self::EmptyAtomId => formatter.write_str("atom identifier must not be empty"),
41            Self::ZeroConnectionOrder => {
42                formatter.write_str("atom connection order must not be zero")
43            },
44            Self::SelfConnection { index } => {
45                write!(
46                    formatter,
47                    "atom connection cannot point index {index} to itself"
48                )
49            },
50            Self::InvalidConnectionIndex { index, atom_count } => write!(
51                formatter,
52                "atom connection index {index} is outside atom count {atom_count}"
53            ),
54        }
55    }
56}
57
58impl Error for MoleculeValidationError {}