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}
12
13impl fmt::Display for ParseError {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        match self {
16            ParseError::IncompleteHeader => {
17                write!(f, "file ended unexpectedly while parsing frame header")
18            }
19            ParseError::IncompleteFrame => {
20                write!(f, "file ended unexpectedly while reading atom data")
21            }
22            ParseError::IncompleteVelocitySection => {
23                write!(f, "file ended unexpectedly while reading velocity section")
24            }
25            ParseError::InvalidVectorLength { expected, found } => {
26                write!(f, "expected {expected} values on line, found {found}")
27            }
28            ParseError::InvalidNumberFormat(msg) => {
29                write!(f, "invalid number format: {msg}")
30            }
31        }
32    }
33}
34
35impl std::error::Error for ParseError {}
36
37impl From<ParseFloatError> for ParseError {
38    fn from(e: ParseFloatError) -> Self {
39        ParseError::InvalidNumberFormat(e.to_string())
40    }
41}
42
43impl From<ParseIntError> for ParseError {
44    fn from(e: ParseIntError) -> Self {
45        ParseError::InvalidNumberFormat(e.to_string())
46    }
47}