1 | use std::fmt; |
2 | use std::num::ParseFloatError; |
3 | use std::num::ParseIntError; |
4 | use std::str::ParseBoolError; |
5 | |
6 | #[derive (Debug)] |
7 | pub enum Error { |
8 | Xml(roxmltree::Error), |
9 | NoFontconfig, |
10 | InvalidFormat(String), |
11 | IoError(std::io::Error), |
12 | ParseEnumError(&'static str, String), |
13 | ParseIntError(ParseIntError), |
14 | ParseFloatError(ParseFloatError), |
15 | ParseBoolError(ParseBoolError), |
16 | } |
17 | |
18 | impl From<std::io::Error> for Error { |
19 | fn from(e: std::io::Error) -> Self { |
20 | Self::IoError(e) |
21 | } |
22 | } |
23 | |
24 | impl From<roxmltree::Error> for Error { |
25 | fn from(e: roxmltree::Error) -> Self { |
26 | Self::Xml(e) |
27 | } |
28 | } |
29 | |
30 | impl From<ParseIntError> for Error { |
31 | fn from(e: ParseIntError) -> Self { |
32 | Self::ParseIntError(e) |
33 | } |
34 | } |
35 | |
36 | impl From<ParseFloatError> for Error { |
37 | fn from(e: ParseFloatError) -> Self { |
38 | Self::ParseFloatError(e) |
39 | } |
40 | } |
41 | |
42 | impl From<ParseBoolError> for Error { |
43 | fn from(e: ParseBoolError) -> Self { |
44 | Self::ParseBoolError(e) |
45 | } |
46 | } |
47 | |
48 | impl fmt::Display for Error { |
49 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
50 | match self { |
51 | Error::Xml(e: &Error) => e.fmt(f), |
52 | Error::NoFontconfig => write!(f, "Can't find fontconfig element" ), |
53 | Error::InvalidFormat(msg: &String) => write!(f, "Config format is invalid: {}" , msg), |
54 | Error::IoError(e: &Error) => write!(f, "IO error: {}" , e), |
55 | Error::ParseEnumError(ty: &&'static str, s: &String) => write!(f, "Unknown variant for {}: {}" , ty, s), |
56 | Error::ParseIntError(e: &ParseIntError) => e.fmt(f), |
57 | Error::ParseFloatError(e: &ParseFloatError) => e.fmt(f), |
58 | Error::ParseBoolError(e: &ParseBoolError) => e.fmt(f), |
59 | } |
60 | } |
61 | } |
62 | |
63 | impl std::error::Error for Error {} |
64 | |