Skip to main content

chematic_mol/
error.rs

1//! Error types for MOL/SDF parsing.
2
3/// Errors that can occur while parsing a MOL V2000 or SDF file.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum MolParseError {
6    /// The header block (lines 1–3) could not be parsed.
7    InvalidHeader { line: usize, detail: String },
8    /// The counts line (line 4) is missing or malformed.
9    InvalidCountLine { line: usize, detail: String },
10    /// An atom-block line could not be parsed.
11    InvalidAtomLine { line: usize, detail: String },
12    /// A bond-block line could not be parsed.
13    InvalidBondLine { line: usize, detail: String },
14    /// The element symbol on an atom line is not recognised.
15    UnknownElement { symbol: String, line: usize },
16    /// The input ended before the molecule was complete.
17    UnexpectedEnd,
18    /// A V3000 (Extended Ctab) structural error.
19    V3000ParseError { line: usize, msg: String },
20    /// An IO error occurred while reading the input stream.
21    Io(String),
22}
23
24impl std::fmt::Display for MolParseError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Self::InvalidHeader { line, detail } => {
28                write!(f, "invalid header at line {line}: {detail}")
29            }
30            Self::InvalidCountLine { line, detail } => {
31                write!(f, "invalid counts line at line {line}: {detail}")
32            }
33            Self::InvalidAtomLine { line, detail } => {
34                write!(f, "invalid atom line at line {line}: {detail}")
35            }
36            Self::InvalidBondLine { line, detail } => {
37                write!(f, "invalid bond line at line {line}: {detail}")
38            }
39            Self::UnknownElement { symbol, line } => {
40                write!(f, "unknown element symbol '{symbol}' at line {line}")
41            }
42            Self::UnexpectedEnd => {
43                write!(f, "unexpected end of input")
44            }
45            Self::V3000ParseError { line, msg } => {
46                write!(f, "V3000 parse error at line {line}: {msg}")
47            }
48            Self::Io(msg) => write!(f, "IO error: {msg}"),
49        }
50    }
51}
52
53impl std::error::Error for MolParseError {}