serde_hashkey/
error.rs

1//! Errors raised during serialization/deserialization.
2use serde::{de, ser};
3use std::{error, fmt, result};
4
5/// Errors that can occur during serialization and deserialization of a
6/// [Key](crate::Key).
7#[derive(Debug, PartialEq)]
8pub enum Error {
9    /// Unexpected type encountered.
10    Unexpected(&'static str),
11    /// Type is not supported for serialization.
12    UnsupportedType(&'static str),
13    /// Unsupported deserialization variant.
14    UnexpectedVariant(&'static str),
15    /// A custom error.
16    Custom(String),
17    /// Value is missing during deserialization.
18    MissingValue,
19    /// Array has invalid length.
20    InvalidLength,
21}
22
23/// Helper alias for a Result which already represents our local [Error] type.
24pub type Result<T, E = Error> = result::Result<T, E>;
25
26impl fmt::Display for Error {
27    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
28        use self::Error::*;
29
30        match self {
31            Unexpected(expected) => write!(fmt, "unexpected type, expected: {}", expected),
32            UnsupportedType(ty) => write!(fmt, "unsupported type: {}", ty),
33            UnexpectedVariant(variant) => write!(fmt, "unexpectec variant: {}", variant),
34            Custom(e) => write!(fmt, "{}", e),
35            MissingValue => write!(fmt, "missing value duration deserialization"),
36            InvalidLength => write!(fmt, "array with invalid length"),
37        }
38    }
39}
40
41impl error::Error for Error {}
42
43impl ser::Error for Error {
44    fn custom<T>(msg: T) -> Self
45    where
46        T: fmt::Display,
47    {
48        Error::Custom(msg.to_string())
49    }
50}
51
52impl de::Error for Error {
53    fn custom<T>(msg: T) -> Self
54    where
55        T: fmt::Display,
56    {
57        Error::Custom(msg.to_string())
58    }
59}