quake_util/error/
mod.rs

1extern crate std;
2
3use std::{error, fmt, io, num::NonZeroU64, string::String};
4
5#[derive(Debug)]
6pub enum BinParse {
7    Io(io::Error),
8    Parse(String),
9}
10
11impl From<io::Error> for BinParse {
12    fn from(err: io::Error) -> BinParse {
13        BinParse::Io(err)
14    }
15}
16
17impl fmt::Display for BinParse {
18    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
19        match self {
20            Self::Io(e) => {
21                fmt.write_fmt(format_args!("IO Error: {e}"))?;
22            }
23            Self::Parse(s) => {
24                fmt.write_fmt(format_args!("Binary Parse Error: {s}"))?;
25            }
26        }
27
28        Ok(())
29    }
30}
31
32impl std::error::Error for BinParse {}
33
34#[derive(Debug, Clone)]
35pub struct Line {
36    pub message: String,
37    pub line_number: Option<NonZeroU64>,
38}
39
40impl fmt::Display for Line {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        match self.line_number {
43            Some(ln) => write!(f, "Line {}: {}", ln, self.message),
44            None => write!(f, "{}", self.message),
45        }
46    }
47}
48
49impl error::Error for Line {}
50
51#[derive(Debug)]
52pub enum TextParse {
53    Io(io::Error),
54    Lexer(Line),
55    Parser(Line),
56}
57
58impl TextParse {
59    pub fn from_lexer(message: String, line_number: NonZeroU64) -> TextParse {
60        TextParse::Lexer(Line {
61            message,
62            line_number: Some(line_number),
63        })
64    }
65
66    pub fn from_parser(message: String, line_number: NonZeroU64) -> TextParse {
67        TextParse::Parser(Line {
68            message,
69            line_number: Some(line_number),
70        })
71    }
72
73    pub fn eof() -> TextParse {
74        TextParse::Parser(Line {
75            message: String::from("Unexpected end-of-file"),
76            line_number: None,
77        })
78    }
79}
80
81impl From<io::Error> for TextParse {
82    fn from(err: io::Error) -> Self {
83        TextParse::Io(err)
84    }
85}
86
87impl fmt::Display for TextParse {
88    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89        match self {
90            Self::Io(msg) => write!(f, "{}", msg),
91            Self::Lexer(err) => write!(f, "{}", err),
92            Self::Parser(err) => write!(f, "{}", err),
93        }
94    }
95}
96
97impl std::error::Error for TextParse {}
98
99#[derive(Debug)]
100pub enum Write {
101    Validation(String),
102    Io(std::io::Error),
103}
104
105impl From<io::Error> for Write {
106    fn from(err: io::Error) -> Self {
107        Write::Io(err)
108    }
109}
110
111impl fmt::Display for Write {
112    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113        match self {
114            Self::Validation(msg) => write!(f, "{}", msg),
115            Self::Io(err) => write!(f, "{}", err),
116        }
117    }
118}
119
120impl std::error::Error for Write {}