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