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}
19
20impl fmt::Display for ParseError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            ParseError::IncompleteHeader => {
24                write!(f, "file ended unexpectedly while parsing frame header")
25            }
26            ParseError::IncompleteFrame => {
27                write!(f, "file ended unexpectedly while reading atom data")
28            }
29            ParseError::IncompleteVelocitySection => {
30                write!(f, "file ended unexpectedly while reading velocity section")
31            }
32            ParseError::InvalidVectorLength { expected, found } => {
33                write!(f, "expected {expected} values on line, found {found}")
34            }
35            ParseError::InvalidNumberFormat(msg) => {
36                write!(f, "invalid number format: {msg}")
37            }
38            ParseError::MissingSpecVersion => {
39                write!(
40                    f,
41                    "line 1 must be a JSON object containing \"con_spec_version\""
42                )
43            }
44            ParseError::UnsupportedSpecVersion(v) => {
45                write!(f, "unsupported con_spec_version: {v}")
46            }
47            ParseError::InvalidMetadataJson(msg) => {
48                write!(f, "invalid JSON metadata on line 1: {msg}")
49            }
50            ParseError::IncompleteForceSection => {
51                write!(f, "file ended unexpectedly while reading force section")
52            }
53            ParseError::IncompleteEnergySection => {
54                write!(f, "file ended unexpectedly while reading energy section")
55            }
56            ParseError::UnknownSection(name) => {
57                write!(f, "unknown section type in metadata: {name}")
58            }
59            ParseError::ValidationError(msg) => {
60                write!(f, "CON validation failed: {msg}")
61            }
62        }
63    }
64}
65
66impl std::error::Error for ParseError {}
67
68impl From<ParseFloatError> for ParseError {
69    fn from(e: ParseFloatError) -> Self {
70        ParseError::InvalidNumberFormat(e.to_string())
71    }
72}
73
74impl From<ParseIntError> for ParseError {
75    fn from(e: ParseIntError) -> Self {
76        ParseError::InvalidNumberFormat(e.to_string())
77    }
78}
79
80impl From<serde_json::Error> for ParseError {
81    fn from(e: serde_json::Error) -> Self {
82        ParseError::InvalidMetadataJson(e.to_string())
83    }
84}