1#[derive(Debug)]
3pub enum SerdeError {
4 Json(serde_json::Error),
6
7 #[cfg(feature = "xml")]
9 Xml(quick_xml::Error),
10
11 Io(std::io::Error),
13
14 Custom(String),
16}
17
18impl std::fmt::Display for SerdeError {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 match self {
21 SerdeError::Json(e) => write!(f, "JSON error: {}", e),
22 #[cfg(feature = "xml")]
23 SerdeError::Xml(e) => write!(f, "XML error: {}", e),
24 SerdeError::Io(e) => write!(f, "IO error: {}", e),
25 SerdeError::Custom(msg) => write!(f, "{}", msg),
26 }
27 }
28}
29
30impl std::error::Error for SerdeError {
31 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32 match self {
33 SerdeError::Json(e) => Some(e),
34 #[cfg(feature = "xml")]
35 SerdeError::Xml(e) => Some(e),
36 SerdeError::Io(e) => Some(e),
37 SerdeError::Custom(_) => None,
38 }
39 }
40}
41
42impl From<serde_json::Error> for SerdeError {
43 fn from(err: serde_json::Error) -> Self {
44 SerdeError::Json(err)
45 }
46}
47
48#[cfg(feature = "xml")]
49impl From<quick_xml::Error> for SerdeError {
50 fn from(err: quick_xml::Error) -> Self {
51 SerdeError::Xml(err)
52 }
53}
54
55impl From<std::io::Error> for SerdeError {
56 fn from(err: std::io::Error) -> Self {
57 SerdeError::Io(err)
58 }
59}
60
61impl From<String> for SerdeError {
62 fn from(msg: String) -> Self {
63 SerdeError::Custom(msg)
64 }
65}
66
67impl From<&str> for SerdeError {
68 fn from(msg: &str) -> Self {
69 SerdeError::Custom(msg.to_string())
70 }
71}
72
73impl serde::ser::Error for SerdeError {
75 fn custom<T: std::fmt::Display>(msg: T) -> Self {
76 SerdeError::Custom(msg.to_string())
77 }
78}
79
80impl serde::de::Error for SerdeError {
82 fn custom<T: std::fmt::Display>(msg: T) -> Self {
83 SerdeError::Custom(msg.to_string())
84 }
85}
86
87pub type Result<T> = std::result::Result<T, SerdeError>;