diff options
author | Sam Scott <sam.scott89@gmail.com> | 2017-05-21 16:11:15 +0100 |
---|---|---|
committer | Sam Scott <sam.scott89@gmail.com> | 2017-05-21 16:11:56 +0100 |
commit | 61b7b7075227f25f509e0ca51909e0f873ed310f (patch) | |
tree | d533ffece20e8dc833cb6f32a0932bf2fbede511 /src | |
parent | 6429b975c6cbcf5d7e0c2b1d02026f8d46b5c50f (diff) |
Refactor errors to be of a single type and export it.
Diffstat (limited to 'src')
-rw-r--r-- | src/de/mod.rs | 34 | ||||
-rw-r--r-- | src/error.rs | 35 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/ser.rs | 9 |
4 files changed, 44 insertions, 37 deletions
diff --git a/src/de/mod.rs b/src/de/mod.rs index cf2768b..228a12b 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -41,8 +41,8 @@ mod parse; pub use de::parse::Config; +use error::*; -use data_encoding; use data_encoding::base64url as base64; use serde::de; @@ -51,33 +51,7 @@ use serde::de::IntoDeserializer; use url::percent_encoding; use std::collections::btree_map::{BTreeMap, Entry, IntoIter}; -use std::io::{self,Read}; -use std::fmt::Display; -use std::string; - -error_chain! { - errors { Custom(msg: String) } - foreign_links { - Decoding(data_encoding::decode::Error); - Io(io::Error); - Utf8(string::FromUtf8Error); - } -} - -impl Error { - fn top_level(object: &'static str) -> Self { - 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() - } -} +use std::io::Read; /// Deserializes a querystring from a `&[u8]`. /// @@ -316,7 +290,7 @@ impl<'de> de::MapAccess<'de> for QsDeserializer { } impl<'de> de::EnumAccess<'de> for QsDeserializer { - type Error = Error; + type Error = Error; type Variant = Self; fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant)> @@ -379,7 +353,7 @@ impl<'de> de::VariantAccess<'de> for QsDeserializer { } impl<'de> de::EnumAccess<'de> for LevelDeserializer { - type Error = Error; + type Error = Error; type Variant = Self; fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)> 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<T>(msg: T) -> Self + where T: Display { + ErrorKind::Custom(msg.to_string()).into() + } +}
\ No newline at end of file @@ -125,8 +125,11 @@ extern crate serde; extern crate url; mod de; +mod error; mod ser; +pub use error::Error; + #[doc(inline)] pub use de::{QsDeserializer, from_bytes, from_reader, from_str}; #[doc(inline)] @@ -10,6 +10,8 @@ use std::fmt::Display; use std::borrow::Cow; use std::str; +use error::*; + /// Serializes a value into a querystring. /// /// ``` @@ -76,13 +78,6 @@ impl<'a, Target: 'a + UrlEncodedTarget> QsSerializer<'a, Target> { } } -error_chain!{ - errors { - Custom(msg: String) - Unsupported - } -} - impl Error { fn no_key() -> Self { let msg = "tried to serialize a value before serializing key"; |