use serde::de::Error as DeError;
use serde::ser::Error as SerError;
use std::convert::Infallible;
use std::fmt::Display;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Expected token {token}, found {found}")]
UnexpectedToken { token: String, found: String },
#[error("Custom: {field}")]
Custom { field: String },
#[error("UnsupportedOperation: {operation}")]
UnsupportedOperation { operation: String },
#[error("IO error: {0}")]
Io(#[from] ::std::io::Error),
#[error("FromUtf8Error: {0}")]
FromUtf8Error(#[from] ::std::string::FromUtf8Error),
#[error("ParseIntError: {0}")]
ParseIntError(#[from] ::std::num::ParseIntError),
#[error("ParseFloatError: {0}")]
ParseFloatError(#[from] ::std::num::ParseFloatError),
#[error("ParseBoolError: {0}")]
ParseBoolError(#[from] ::std::str::ParseBoolError),
}
pub type Result<T> = std::result::Result<T, Error>;
impl DeError for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Custom {
field: msg.to_string(),
}
}
}
impl SerError for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Custom {
field: msg.to_string(),
}
}
}
impl From<Infallible> for Error {
fn from(err: Infallible) -> Self {
return Error::Custom {
field: err.to_string(),
};
}
}
impl From<serde_xml_rs::Error> for Error {
fn from(err: serde_xml_rs::Error) -> Self {
match err {
serde_xml_rs::Error::UnexpectedToken { token, found } => {
Error::UnexpectedToken { token, found }
}
serde_xml_rs::Error::Custom { field } => Error::Custom { field },
serde_xml_rs::Error::UnsupportedOperation { operation } => {
Error::UnsupportedOperation { operation }
}
serde_xml_rs::Error::Io { source } => Error::Io(source),
serde_xml_rs::Error::FromUtf8Error { source } => Error::FromUtf8Error(source),
serde_xml_rs::Error::ParseIntError { source } => Error::ParseIntError(source),
serde_xml_rs::Error::ParseFloatError { source } => Error::ParseFloatError(source),
serde_xml_rs::Error::ParseBoolError { source } => Error::ParseBoolError(source),
serde_xml_rs::Error::Syntax { source } => Error::Custom {
field: format!("Syntax error: {source}"),
},
serde_xml_rs::Error::Writer { source } => Error::Custom {
field: format!("Writer error: {source}"),
},
}
}
}