From 61b7b7075227f25f509e0ca51909e0f873ed310f Mon Sep 17 00:00:00 2001 From: Sam Scott Date: Sun, 21 May 2017 16:11:15 +0100 Subject: Refactor errors to be of a single type and export it. --- src/error.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/error.rs (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..b06d023 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,35 @@ +use data_encoding; +use serde::de; + +use std::fmt::Display; +use std::io; +use std::string; + +error_chain! { + errors { + Custom(msg: String) + Unsupported + } + + foreign_links { + Decoding(data_encoding::decode::Error); + Io(io::Error); + 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(msg: T) -> Self + where T: Display { + ErrorKind::Custom(msg.to_string()).into() + } +} \ No newline at end of file -- cgit v1.2.3