Skip to main content

readcon_core/
error.rs

1use std::fmt;
2use std::num::{ParseFloatError, ParseIntError};
3
4#[derive(Debug)]
5pub enum ParseError {
6    IncompleteHeader,
7    IncompleteFrame,
8    IncompleteVelocitySection,
9    InvalidVectorLength { expected: usize, found: usize },
10    InvalidNumberFormat(String),
11    MissingSpecVersion,
12    UnsupportedSpecVersion(u32),
13    InvalidMetadataJson(String),
14    IncompleteForceSection,
15    IncompleteEnergySection,
16    UnknownSection(String),
17    ValidationError(String),
18    /// An in-place builder mutation
19    /// (`ConFrameBuilder::set_atom_position` / `set_atom_velocity` /
20    /// `set_atom_force` / `set_atom_energy` / `set_atom_fixed` /
21    /// `set_atom_mass` / clear_*) was called with an atom index past
22    /// the current length. Surfaces as `IndexError` in PyO3 and as
23    /// `RKR_STATUS_INDEX_OUT_OF_BOUNDS` over the C ABI.
24    IndexOutOfBounds { index: usize, len: usize },
25}
26
27impl fmt::Display for ParseError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            ParseError::IncompleteHeader => {
31                write!(f, "file ended unexpectedly while parsing frame header")
32            }
33            ParseError::IncompleteFrame => {
34                write!(f, "file ended unexpectedly while reading atom data")
35            }
36            ParseError::IncompleteVelocitySection => {
37                write!(f, "file ended unexpectedly while reading velocity section")
38            }
39            ParseError::InvalidVectorLength { expected, found } => {
40                write!(f, "expected {expected} values on line, found {found}")
41            }
42            ParseError::InvalidNumberFormat(msg) => {
43                write!(f, "invalid number format: {msg}")
44            }
45            ParseError::MissingSpecVersion => {
46                write!(
47                    f,
48                    "line 1 must be a JSON object containing \"con_spec_version\""
49                )
50            }
51            ParseError::UnsupportedSpecVersion(v) => {
52                write!(f, "unsupported con_spec_version: {v}")
53            }
54            ParseError::InvalidMetadataJson(msg) => {
55                write!(f, "invalid JSON metadata on line 1: {msg}")
56            }
57            ParseError::IncompleteForceSection => {
58                write!(f, "file ended unexpectedly while reading force section")
59            }
60            ParseError::IncompleteEnergySection => {
61                write!(f, "file ended unexpectedly while reading energy section")
62            }
63            ParseError::UnknownSection(name) => {
64                write!(f, "unknown section type in metadata: {name}")
65            }
66            ParseError::ValidationError(msg) => {
67                write!(f, "CON validation failed: {msg}")
68            }
69            ParseError::IndexOutOfBounds { index, len } => {
70                write!(
71                    f,
72                    "atom index {index} is out of bounds (builder holds {len} atoms)"
73                )
74            }
75        }
76    }
77}
78
79impl std::error::Error for ParseError {}
80
81impl From<ParseFloatError> for ParseError {
82    fn from(e: ParseFloatError) -> Self {
83        ParseError::InvalidNumberFormat(e.to_string())
84    }
85}
86
87impl From<ParseIntError> for ParseError {
88    fn from(e: ParseIntError) -> Self {
89        ParseError::InvalidNumberFormat(e.to_string())
90    }
91}
92
93impl From<serde_json::Error> for ParseError {
94    fn from(e: serde_json::Error) -> Self {
95        ParseError::InvalidMetadataJson(e.to_string())
96    }
97}