1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::table;

#[derive(Debug, PartialEq)]
pub enum Error {
	Parse(ParseError),
	IO(std::io::ErrorKind),
	Utf8(std::str::Utf8Error),
}

impl std::fmt::Display for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		match self {
			Error::Parse(err) => write!(f, "{}", err),
			Error::IO(err) => write!(f, "{:?}", err),
			Error::Utf8(err) => write!(f, "{}", err),
		}
	}
}

impl std::error::Error for Error {}

impl From<ParseError> for Error {
	fn from(err: ParseError) -> Self {
		Error::Parse(err)
	}
}

impl From<std::io::Error> for Error {
	fn from(err: std::io::Error) -> Self {
		Error::IO(err.kind())
	}
}

impl From<std::str::Utf8Error> for Error {
	fn from(err: std::str::Utf8Error) -> Self {
		Error::Utf8(err)
	}
}

#[derive(Debug, PartialEq)]
pub struct ParseError {
	pub state: table::State,
	pub byte: u8,
}

impl std::fmt::Display for ParseError {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		write!(
			f,
			"parse error with byte {:X} in state {:?}",
			self.byte, self.state
		)
	}
}

impl std::error::Error for ParseError {}