collectd_plugin/de/
errors.rs

1use serde::de;
2use std::error;
3use std::fmt::{self, Display};
4
5#[derive(Clone, Debug)]
6pub enum DeError {
7    NoMoreValuesLeft,
8    SerdeError(String),
9    ExpectSingleValue,
10    ExpectString,
11    ExpectChar(String),
12    ExpectBoolean,
13    ExpectNumber,
14    ExpectStruct,
15    ExpectObject,
16    DataTypeNotSupported,
17}
18
19// Since the failure crate can't automatically implement serde::de::Error (see issue
20// <https://github.com/withoutboats/failure/issues/108>) we define a thin wrapper around our actual
21// error type.
22#[derive(Debug)]
23pub struct Error(pub DeError);
24
25impl de::Error for Error {
26    fn custom<T: Display>(msg: T) -> Self {
27        Error(DeError::SerdeError(msg.to_string()))
28    }
29}
30
31impl error::Error for Error {
32    fn description(&self) -> &str {
33        "an with deserialization error"
34    }
35}
36
37impl Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self.0 {
40            DeError::NoMoreValuesLeft => write!(f, "no more values left, this should never happen"),
41            DeError::SerdeError(ref s) => write!(f, "error from deserialization: {}", s),
42            DeError::ExpectSingleValue => write!(f, "expecting values to contain a single entry"),
43            DeError::ExpectString => write!(f, "expecting string"),
44            DeError::ExpectChar(ref s) => {
45                write!(f, "expecting string of length one, received `{}`", s)
46            }
47            DeError::ExpectBoolean => write!(f, "expecting boolean"),
48            DeError::ExpectNumber => write!(f, "expecting number"),
49            DeError::ExpectStruct => write!(f, "expecting struct"),
50            DeError::ExpectObject => write!(f, "needs an object to deserialize a struct"),
51            DeError::DataTypeNotSupported => {
52                write!(f, "could not deserialize as datatype not supported")
53            }
54        }
55    }
56}