1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::de::ConversionError;
use thiserror::Error;

/// The errors that can arise while deserializing with `serde_db::de`.
#[derive(Error)]
pub enum DeserializationError {
    /// Deserialization failed due to a conversion error.
    #[error("Deserialization failed due to a conversion error")]
    ConversionError(ConversionError),

    /// Error reported from serde framework.
    #[error("serde error")]
    SerdeError(String),

    /// The structure of the target object does not fit to the structure of the object being
    /// deserialized.
    #[error("incompatible target structure")]
    Usage(String),

    /// Thrown by functions in the Deserializer interface that are not implemented.
    /// This exception should never be seen in practice.
    #[error("this exception should never be seen in practice")]
    NotImplemented(&'static str),

    /// The target structure misses a field for which data are provided.
    #[error("the deserialization target misses a field for which data are provided")]
    UnknownField(String),

    /// The deserialization cannot consume all existing rows.
    #[error("cannot consume all existing rows")]
    TrailingRows,

    /// The deserialization cannot consume all existing columns.
    #[error("cannot consume all existing columns")]
    TrailingCols,
}

impl From<ConversionError> for DeserializationError {
    fn from(error: ConversionError) -> DeserializationError {
        DeserializationError::ConversionError(error)
    }
}

impl serde::de::Error for DeserializationError {
    fn custom<T: std::fmt::Display>(msg: T) -> Self {
        DeserializationError::SerdeError(msg.to_string())
    }
}

impl std::fmt::Debug for DeserializationError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            Self::ConversionError(ref e) => write!(formatter, "{:?}", e),
            Self::NotImplemented(ref s) => write!(formatter, "{}: {}", self, s),
            Self::SerdeError(ref s) | Self::UnknownField(ref s) | Self::Usage(ref s) => {
                write!(formatter, "{}: {}", self, s)
            }
            Self::TrailingRows | Self::TrailingCols => write!(formatter, "{}", self),
        }
    }
}

/// A specialized Result type for deserialization.
pub type DeserializationResult<T> = Result<T, DeserializationError>;