acf_parser/
errors.rs

1use chumsky::prelude::SimpleSpan;
2use std::error;
3use std::fmt;
4
5/// Top level error type
6#[derive(Debug, PartialEq, Eq, Default)]
7pub enum AcfError {
8    /// An error occurred reading a file
9    Read(String),
10
11    /// An error occurring during parsing (with specific sub-type)
12    Parse(ParseError),
13
14    /// An unknown/uncategorized error
15    #[default]
16    Unknown,
17}
18
19impl fmt::Display for AcfError {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        match self {
22            AcfError::Read(val) => write!(f, "failed to read '{}'", &val),
23            AcfError::Parse(..) => write!(f, "the provided input could not be parsed"),
24            AcfError::Unknown => write!(f, "an unknown error occurred"),
25        }
26    }
27}
28
29impl error::Error for AcfError {
30    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
31        match *self {
32            AcfError::Read(..) => None,
33            AcfError::Parse(ref e) => Some(e),
34            AcfError::Unknown => None,
35        }
36    }
37}
38
39/// Representation of a filesystem error
40#[derive(Debug, PartialEq, Eq, Default)]
41pub enum IOError {
42    /// An unknown/uncategorized error
43    #[default]
44    Unknown,
45}
46
47impl fmt::Display for IOError {
48    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49        match *self {
50            IOError::Unknown => write!(f, "an unknown I/O error occurred"),
51        }
52    }
53}
54
55impl error::Error for IOError {
56    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
57        match *self {
58            IOError::Unknown => None,
59        }
60    }
61}
62
63/// Representation of a parser error
64#[derive(Debug, PartialEq, Eq, Default)]
65pub enum ParseError {
66    /// A closing brace was not found
67    ExpectedClosingBrace(SimpleSpan),
68
69    /// An unknown/uncategorized error
70    #[default]
71    Unknown,
72}
73
74impl fmt::Display for ParseError {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        match *self {
77            ParseError::ExpectedClosingBrace(val) => {
78                write!(f, "expected a closing brace within '{}'", &val)
79            }
80            ParseError::Unknown => write!(f, "an unknown parsing error occurred"),
81        }
82    }
83}
84
85impl error::Error for ParseError {
86    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
87        match *self {
88            ParseError::ExpectedClosingBrace(_) => None,
89            ParseError::Unknown => None,
90        }
91    }
92}