1pub type Result<T> = std::result::Result<T, Error>;
2
3pub struct Error {
4 err: Box<ErrorImpl>,
5}
6
7pub enum ErrorType {
8 ParseError,
9 ValidateError,
10}
11
12struct ErrorImpl {
13 message: Box<str>,
14 err_type: ErrorType,
15}
16
17impl Error {
18 pub fn new(message: &str, err_type: ErrorType) -> Self {
19 Error {
20 err: Box::new(ErrorImpl {
21 message: message.into(),
22 err_type,
23 }),
24 }
25 }
26 pub fn parse_err(message: &str) -> Self {
27 Error::new(message, ErrorType::ParseError)
28 }
29 pub fn validate_err(message: &str) -> Self {
30 Error::new(message, ErrorType::ValidateError)
31 }
32 pub fn err_type(&self) -> ErrorType {
33 self.err.err_type
34 }
35}
36
37impl std::fmt::Display for Error {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 write!(f, "{}: {}", self.err_type(), self.err.message)
40 }
41}
42
43impl std::fmt::Debug for Error {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 write!(f, "{}: {}", self.err_type(), self.err.message)
46 }
47}
48
49impl std::fmt::Display for ErrorType {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 match self {
52 ErrorType::ParseError => write!(f, "Parse Error"),
53 ErrorType::ValidateError => write!(f, "Validate Error"),
54 }
55 }
56}
57
58impl std::error::Error for Error {}
59
60impl std::fmt::Debug for ErrorType {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 match self {
63 ErrorType::ParseError => write!(f, "Parse Error"),
64 ErrorType::ValidateError => write!(f, "Validate Error"),
65 }
66 }
67}
68
69impl Clone for ErrorType {
70 fn clone(&self) -> Self {
71 match self {
72 ErrorType::ParseError => ErrorType::ParseError,
73 ErrorType::ValidateError => ErrorType::ValidateError,
74 }
75 }
76}
77
78impl Copy for ErrorType {}