xml_serde/
error.rs

1#[derive(Debug)]
2pub enum Error {
3    XMLWError(xml::writer::Error),
4    XMLRError(xml::reader::Error),
5    Message(String),
6    ExpectedString,
7    ExpectedChar,
8    ExpectedBool,
9    ExpectedInt,
10    ExpectedElement,
11    Unsupported
12}
13
14pub type Result<T> = std::result::Result<T, Error>;
15
16impl serde::ser::Error for Error {
17    fn custom<T: std::fmt::Display>(msg: T) -> Self {
18        Error::Message(msg.to_string())
19    }
20}
21
22impl serde::de::Error for Error {
23    fn custom<T: std::fmt::Display>(msg: T) -> Self {
24        Error::Message(msg.to_string())
25    }
26}
27
28impl std::fmt::Display for Error {
29    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
30        match self {
31            Error::Message(msg) => formatter.write_str(msg),
32            Error::XMLWError(err) => formatter.write_str(&err.to_string()),
33            Error::XMLRError(err) => formatter.write_str(&err.to_string()),
34            Error::ExpectedString => formatter.write_str("expected a string"),
35            Error::ExpectedChar => formatter.write_str("expected a char"),
36            Error::ExpectedBool => formatter.write_str("expected a bool"),
37            Error::ExpectedInt => formatter.write_str("expected a number"),
38            Error::ExpectedElement => formatter.write_str("expected an element"),
39            Error::Unsupported => formatter.write_str("unsupported operation"),
40        }
41    }
42}
43
44impl std::error::Error for Error {}
45
46impl From<xml::writer::Error> for Error {
47    fn from(err: xml::writer::Error) -> Self {
48        Error::XMLWError(err)
49    }
50}
51
52impl From<xml::reader::Error> for Error {
53    fn from(err: xml::reader::Error) -> Self {
54        Error::XMLRError(err)
55    }
56}
57
58impl From<&xml::reader::Error> for Error {
59    fn from(err: &xml::reader::Error) -> Self {
60        Error::XMLRError(err.clone())
61    }
62}