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}
21
22impl std::fmt::Display for MolParseError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Self::InvalidHeader { line, detail } => {
26                write!(f, "invalid header at line {line}: {detail}")
27            }
28            Self::InvalidCountLine { line, detail } => {
29                write!(f, "invalid counts line at line {line}: {detail}")
30            }
31            Self::InvalidAtomLine { line, detail } => {
32                write!(f, "invalid atom line at line {line}: {detail}")
33            }
34            Self::InvalidBondLine { line, detail } => {
35                write!(f, "invalid bond line at line {line}: {detail}")
36            }
37            Self::UnknownElement { symbol, line } => {
38                write!(f, "unknown element symbol '{symbol}' at line {line}")
39            }
40            Self::UnexpectedEnd => {
41                write!(f, "unexpected end of input")
42            }
43            Self::V3000ParseError { line, msg } => {
44                write!(f, "V3000 parse error at line {line}: {msg}")
45            }
46        }
47    }
48}
49
50impl std::error::Error for MolParseError {}