diff options
author | Josh Triplett <josh@joshtriplett.org> | 2020-08-27 10:45:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-27 13:45:51 -0400 |
commit | 933eb8ab38317d894b8b09b4de3cecd317221e06 (patch) | |
tree | 5e0d48dbf2043dad60a8900061ac921745133093 /src | |
parent | dd7596758f55fcbbfbd690a8da17d8a17c4ec1d8 (diff) |
Switch from error-chain to thiserror (#33)
thiserror provides a simpler interface than error-chain, directly
generating an error enum similar to what could be written by hand. It
also reduces dependencies; error-chain pulls in backtrace which
currently pulls in gimli, which is fairly heavyweight.
This changes the error type's interface, so it'll require a bump to
0.7.0.
Diffstat (limited to 'src')
-rw-r--r-- | src/error.rs | 61 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/ser.rs | 34 |
3 files changed, 55 insertions, 42 deletions
diff --git a/src/error.rs b/src/error.rs index d6b7023..6aac25f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,37 +6,50 @@ use std::num; use std::str; use std::string; -error_chain! { - errors { - Custom(msg: String) { - description("miscellaneous failure") - display("failed with reason: {}", msg) - } - Parse(msg: String, pos: usize) { - description("parsing failure") - display("parsing failed with error: '{}' at position: {}", msg, pos) - } - Unsupported - } +/// Error type for `serde_qs`. +#[derive(thiserror::Error, Debug)] +pub enum Error { + /// Custom string-based error + #[error("failed with reason: {0}")] + Custom(String), - foreign_links { - Decoding(data_encoding::DecodeError); - FromUtf8(string::FromUtf8Error); - Io(io::Error); - ParseInt(num::ParseIntError); - Utf8(str::Utf8Error); - } + /// Parse error at a specified position in the query string + #[error("parsing failed with error: '{0}' at position: {1}")] + Parse(String, usize), + + /// Unsupported type that `serde_qs` can't serialize into a query string + #[error("unsupported type for serialization")] + Unsupported, + + /// Error decoding `BASE64URL` data + #[error(transparent)] + Decoding(#[from] data_encoding::DecodeError), + + /// Error proessing UTF-8 for a `String` + #[error(transparent)] + FromUtf8(#[from] string::FromUtf8Error), + + /// I/O error + #[error(transparent)] + Io(#[from] io::Error), + + /// Error parsing a number + #[error(transparent)] + ParseInt(#[from] num::ParseIntError), + + /// Error processing UTF-8 for a `str` + #[error(transparent)] + Utf8(#[from] str::Utf8Error), } impl Error { /// Generate error to show top-level type cannot be deserialized. pub fn top_level(object: &'static str) -> Self { - ErrorKind::Custom(format!( + Self::Custom(format!( "cannot deserialize {} at the top level.\ Try deserializing into a struct.", object )) - .into() } /// Generate a parsing error message with position. @@ -44,7 +57,7 @@ impl Error { where T: Display, { - ErrorKind::Parse(msg.to_string(), position).into() + Self::Parse(msg.to_string(), position) } } @@ -53,6 +66,8 @@ impl de::Error for Error { where T: Display, { - ErrorKind::Custom(msg.to_string()).into() + Self::Custom(msg.to_string()) } } + +pub type Result<T, E = Error> = core::result::Result<T, E>; @@ -203,8 +203,6 @@ )] #[macro_use] -extern crate error_chain; -#[macro_use] extern crate serde; #[cfg(feature = "actix")] @@ -169,7 +169,7 @@ impl<'a, W: 'a + Write> QsSerializer<'a, W> { impl Error { fn no_key() -> Self { let msg = "tried to serialize a value before serializing key"; - msg.into() + Self::Custom(msg.into()) } } @@ -267,7 +267,7 @@ impl<'a, W: Write> ser::Serializer for &'a mut QsSerializer<'a, W> { } fn serialize_some<T: ?Sized + ser::Serialize>(self, value: &T) -> Result<Self::Ok> { - // Err(ErrorKind::Unsupported.into()) + // Err(Error::Unsupported) value.serialize(self) } @@ -326,7 +326,7 @@ impl ser::Error for Error { where T: Display, { - ErrorKind::Custom(msg.to_string()).into() + Error::Custom(msg.to_string()) } } @@ -516,12 +516,12 @@ impl ser::Serializer for StringSerializer { /// Returns an error. fn serialize_unit(self) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. @@ -531,7 +531,7 @@ impl ser::Serializer for StringSerializer { _variant_index: u32, _variant: &'static str, ) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. @@ -540,7 +540,7 @@ impl ser::Serializer for StringSerializer { _name: &'static str, _value: &T, ) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. @@ -551,26 +551,26 @@ impl ser::Serializer for StringSerializer { _variant: &'static str, _value: &T, ) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. fn serialize_none(self) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. fn serialize_some<T: ?Sized + ser::Serialize>(self, _value: &T) -> Result<Self::Ok> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } /// Returns an error. @@ -579,7 +579,7 @@ impl ser::Serializer for StringSerializer { _name: &'static str, _len: usize, ) -> Result<Self::SerializeTupleStruct> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } fn serialize_tuple_variant( @@ -589,15 +589,15 @@ impl ser::Serializer for StringSerializer { _variant: &'static str, _len: usize, ) -> Result<Self::SerializeTupleVariant> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } fn serialize_struct_variant( @@ -607,6 +607,6 @@ impl ser::Serializer for StringSerializer { _variant: &'static str, _len: usize, ) -> Result<Self::SerializeStructVariant> { - Err(ErrorKind::Unsupported.into()) + Err(Error::Unsupported) } } |