dcan_db/
errors.rs

1use chumsky::prelude::SimpleSpan;
2use std::{error, fmt};
3
4/// Top level error type
5#[derive(Debug, PartialEq, Eq, Default)]
6pub enum DbcError {
7    /// An error occurring while reading a file
8    Read(String),
9
10    /// An error occurring while writing a file
11    Write(String),
12
13    /// An error occured during parsing (with specific sub-type)
14    Parse(ParseError),
15
16    #[default]
17    Unknown,
18}
19
20impl fmt::Display for DbcError {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        match self {
23            Self::Read(val) => write!(f, "failed to read '{}'", &val),
24            Self::Write(val) => write!(f, "failed to write '{}'", &val),
25            Self::Parse(val) => write!(f, "parse error occurred: {}", &val),
26            Self::Unknown => write!(f, "an unknown error occurred"),
27        }
28    }
29}
30
31impl error::Error for DbcError {
32    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
33        match *self {
34            Self::Parse(ref e) => Some(e),
35            _ => None,
36        }
37    }
38}
39
40/// Representation of a parser error
41#[derive(Debug, PartialEq, Eq, Default)]
42pub enum ParseError {
43    /// A closing brace was not found
44    ExpectedClosingBrace(SimpleSpan),
45
46    /// An unknown/uncategorized error
47    #[default]
48    Unknown,
49}
50
51impl fmt::Display for ParseError {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        match *self {
54            ParseError::ExpectedClosingBrace(val) => {
55                write!(f, "expected a closing brace within '{}'", &val)
56            }
57            ParseError::Unknown => write!(f, "an unknown parsing error occurred"),
58        }
59    }
60}
61
62impl error::Error for ParseError {
63    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
64        match *self {
65            ParseError::ExpectedClosingBrace(_) => None,
66            _ => None,
67        }
68    }
69}