serde_rmpv/
error.rs

1use std::fmt::{self, Display};
2
3use serde::{de, ser};
4
5pub type RResult<T, E = Error> = std::result::Result<T, E>;
6
7#[derive(thiserror::Error, Debug)]
8pub enum Error {
9    /// Type mismatch error
10    TypeError(String),
11    /// Data format error
12    Format(String),
13    /// Unsupported type
14    UnsupportedType,
15}
16
17impl ser::Error for Error {
18    fn custom<T: Display>(msg: T) -> Self {
19        Error::Format(msg.to_string())
20    }
21}
22
23impl de::Error for Error {
24    fn custom<T: Display>(msg: T) -> Self {
25        Error::Format(msg.to_string())
26    }
27}
28
29impl Display for Error {
30    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
31        match self {
32            Error::TypeError(msg) => write!(formatter, "invalid type: {}", msg),
33            Error::Format(msg) => write!(formatter, "{}", msg),
34            Error::UnsupportedType => write!(formatter, "unsupported type"),
35        }
36    }
37}