1use serde::de::Error as DeError;
2use serde::ser::Error as SerError;
3use std::convert::Infallible;
4use std::fmt::Display;
5
6#[derive(Debug)]
7pub enum Error {
8 UnexpectedToken {
9 token: String,
10 found: String,
11 },
12 Custom {
13 field: String,
14 },
15 UnsupportedOperation {
16 operation: String,
17 },
18 Io {
19 source: ::std::io::Error,
20 },
21 FromUtf8Error {
22 source: ::std::string::FromUtf8Error,
23 },
24
25 ParseIntError {
26 source: ::std::num::ParseIntError,
27 },
28
29 ParseFloatError {
30 source: ::std::num::ParseFloatError,
31 },
32
33 ParseBoolError {
34 source: ::std::str::ParseBoolError,
35 },
36 }
44
45pub type Result<T> = std::result::Result<T, Error>;
46
47impl std::error::Error for Error {}
48
49#[rustfmt::skip]
50impl Display for Error {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 match self {
53 Error::UnexpectedToken { token, found } => write!(f, "Expected token {token}, found {found}"),
54 Error::Custom { field } => write!(f, "Custom: {field}"),
55 Error::UnsupportedOperation { operation } => write!(f, "UnsupportedOperation: {operation}"),
56 Error::Io { source } => write!(f, "IO error: {source}"),
57 Error::FromUtf8Error { source } => write!(f, "FromUtf8Error: {source}"),
58 Error::ParseIntError { source } => write!(f, "ParseIntError: {source}"),
59 Error::ParseFloatError { source } => write!(f, "ParseFloatError: {source}"),
60 Error::ParseBoolError { source } => write!(f, "ParseBoolError: {source}"),
61 }
62 }
63}
64
65impl DeError for Error {
66 fn custom<T: Display>(msg: T) -> Self {
67 Error::Custom {
68 field: msg.to_string(),
69 }
70 }
71}
72
73impl SerError for Error {
74 fn custom<T: Display>(msg: T) -> Self {
75 Error::Custom {
76 field: msg.to_string(),
77 }
78 }
79}
80
81impl From<::std::io::Error> for Error {
82 fn from(source: ::std::io::Error) -> Self {
83 Error::Io { source }
84 }
85}
86
87impl From<::std::string::FromUtf8Error> for Error {
88 fn from(source: ::std::string::FromUtf8Error) -> Self {
89 Error::FromUtf8Error { source }
90 }
91}
92
93impl From<::std::num::ParseIntError> for Error {
94 fn from(source: ::std::num::ParseIntError) -> Self {
95 Error::ParseIntError { source }
96 }
97}
98
99impl From<::std::num::ParseFloatError> for Error {
100 fn from(source: ::std::num::ParseFloatError) -> Self {
101 Error::ParseFloatError { source }
102 }
103}
104
105impl From<::std::str::ParseBoolError> for Error {
106 fn from(source: ::std::str::ParseBoolError) -> Self {
107 Error::ParseBoolError { source }
108 }
109}
110
111impl From<Infallible> for Error {
112 fn from(err: Infallible) -> Self {
113 return Error::Custom {
114 field: err.to_string(),
115 };
116 }
117}
118
119impl From<serde_xml_rs::Error> for Error {
120 fn from(err: serde_xml_rs::Error) -> Self {
121 match err {
122 serde_xml_rs::Error::UnexpectedToken { token, found } => {
123 Error::UnexpectedToken { token, found }
124 }
125 serde_xml_rs::Error::Custom { field } => Error::Custom { field },
126 serde_xml_rs::Error::UnsupportedOperation { operation } => {
127 Error::UnsupportedOperation { operation }
128 }
129 serde_xml_rs::Error::Io { source } => Error::Io { source },
130 serde_xml_rs::Error::FromUtf8Error { source } => Error::FromUtf8Error { source },
131 serde_xml_rs::Error::ParseIntError { source } => Error::ParseIntError { source },
132 serde_xml_rs::Error::ParseFloatError { source } => Error::ParseFloatError { source },
133 serde_xml_rs::Error::ParseBoolError { source } => Error::ParseBoolError { source },
134 serde_xml_rs::Error::Syntax { source } => Error::Custom {
135 field: format!("Syntax error: {source}"),
136 },
137 serde_xml_rs::Error::Writer { source } => Error::Custom {
138 field: format!("Writer error: {source}"),
139 },
140 }
141 }
142}