preserves/implementations/rust/preserves-serde/src/error.rs

49 lines
1.1 KiB
Rust

use std::convert::From;
use std::io;
#[derive(Debug)]
pub enum Error {
Preserves(preserves::Error),
Message(String),
InvalidUnicodeScalar(u32),
CannotDeserializeAny,
ExpectedOption,
ExpectedUnicodeScalar,
}
impl From<preserves::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Preserves(e)
}
}
impl From<Error> for io::Error {
fn from(e: Error) -> Self {
match e {
Error::Preserves(p) => p.into(),
Error::Message(str) => io::Error::new(io::ErrorKind::Other, str),
_ => io::Error::new(io::ErrorKind::Other, e),
}
}
}
impl serde::ser::Error for Error {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
Self::Message(msg.to_string())
}
}
impl serde::de::Error for Error {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
Self::Message(msg.to_string())
}
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}