nettext/src/serde/error.rs
2022-11-18 14:15:30 +01:00

52 lines
1 KiB
Rust

use std;
use std::fmt::{self, Display};
use serde::{de, ser};
use crate::{dec, enc};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Message(String),
Encode(enc::Error),
Type(dec::TypeError),
}
impl From<enc::Error> for Error {
fn from(e: enc::Error) -> Self {
Error::Encode(e)
}
}
impl From<dec::TypeError> for Error {
fn from(e: dec::TypeError) -> Self {
Error::Type(e)
}
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Message(msg) => formatter.write_str(msg),
Error::Encode(err) => write!(formatter, "Encode: {}", err),
Error::Type(err) => write!(formatter, "Type: {}", err),
}
}
}
impl std::error::Error for Error {}