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;
#[derive(Error)]
pub enum DeserializationError {
#[error("Deserialization failed due to a conversion error")]
ConversionError(ConversionError),
#[error("serde error")]
SerdeError(String),
#[error("incompatible target structure")]
Usage(String),
#[error("this exception should never be seen in practice")]
NotImplemented(&'static str),
#[error("the deserialization target misses a field for which data are provided")]
UnknownField(String),
#[error("cannot consume all existing rows")]
TrailingRows,
#[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),
}
}
}
pub type DeserializationResult<T> = Result<T, DeserializationError>;