Skip to main content

cfs_synapse/
errors.rs

1use std::{error::Error as StdError, fmt};
2
3/// Error type returned by the Synapse library facade.
4#[derive(Debug)]
5pub enum Error {
6    Io(std::io::Error),
7    Parse(Box<pest::error::Error<synapse_parser::synapse::Rule>>),
8    Codegen(synapse_codegen_cfs::CodegenError),
9    Import(String),
10    Mission(String),
11}
12
13impl fmt::Display for Error {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        match self {
16            Error::Io(e) => write!(f, "{e}"),
17            Error::Parse(e) => write!(f, "{e}"),
18            Error::Codegen(e) => write!(f, "{e}"),
19            Error::Import(e) => write!(f, "{e}"),
20            Error::Mission(e) => write!(f, "{e}"),
21        }
22    }
23}
24
25impl StdError for Error {
26    fn source(&self) -> Option<&(dyn StdError + 'static)> {
27        match self {
28            Error::Io(e) => Some(e),
29            Error::Parse(e) => Some(e),
30            Error::Codegen(e) => Some(e),
31            Error::Import(_) | Error::Mission(_) => None,
32        }
33    }
34}
35
36impl From<std::io::Error> for Error {
37    fn from(value: std::io::Error) -> Self {
38        Error::Io(value)
39    }
40}
41
42impl From<pest::error::Error<synapse_parser::synapse::Rule>> for Error {
43    fn from(value: pest::error::Error<synapse_parser::synapse::Rule>) -> Self {
44        Error::Parse(Box::new(value))
45    }
46}
47
48impl From<synapse_codegen_cfs::CodegenError> for Error {
49    fn from(value: synapse_codegen_cfs::CodegenError) -> Self {
50        Error::Codegen(value)
51    }
52}