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
use data_encoding;
use serde::de;

use std::fmt::Display;
use std::io;
use std::num;
use std::string;

error_chain! {
    errors { 
        Custom(msg: String)
        Unsupported
    }

    foreign_links {
        Decoding(data_encoding::decode::Error);
        Io(io::Error);
        Parse(num::ParseIntError);
        Utf8(string::FromUtf8Error);
    }
}

impl Error {
    /// Generate error to show top-level type cannot be deserialized.
    pub fn top_level(object: &'static str) -> Error {
        ErrorKind::Custom(format!("cannot deserialize {} at the top level.\
                           Try deserializing into a struct.", object)).into()

    }
}

impl de::Error for Error {
    fn custom<T>(msg: T) -> Self 
        where T: Display {
            ErrorKind::Custom(msg.to_string()).into()
    }
}