From c3e0b8ce8942ad857ebd38ad35ff502536872c5a Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Tue, 27 Oct 2020 13:13:19 +0200 Subject: [PATCH 1/3] Issue #70 replace `snafu` with `thierror` Signed-off-by: Lachezar Lechev --- Cargo.toml | 4 +-- src/event/builder.rs | 18 ++++---------- src/message/error.rs | 58 ++++++++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9cccd06..66b1ec70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ chrono = { version = "^0.4", features = ["serde"] } delegate-attr = "^0.2" base64 = "^0.12" url = { version = "^2.1", features = ["serde"] } -snafu = "^0.6" +thiserror = "^1.0" [target."cfg(not(target_arch = \"wasm32\"))".dependencies] hostname = "^0.3" @@ -48,4 +48,4 @@ exclude = [ "example-projects/actix-web-example", "example-projects/reqwest-wasm-example", "example-projects/rdkafka-example", -] \ No newline at end of file +] diff --git a/src/event/builder.rs b/src/event/builder.rs index 2e5790d6..6e21780d 100644 --- a/src/event/builder.rs +++ b/src/event/builder.rs @@ -1,5 +1,5 @@ use super::Event; -use snafu::Snafu; +use thiserror::Error; /// Trait to implement a builder for [`Event`]: /// ``` @@ -30,24 +30,16 @@ where } /// Represents an error during build process -#[derive(Debug, Snafu, Clone)] +#[derive(Debug, Error, Clone)] pub enum Error { - #[snafu(display("Missing required attribute {}", attribute_name))] + #[error("Missing required attribute {attribute_name}")] MissingRequiredAttribute { attribute_name: &'static str }, - #[snafu(display( - "Error while setting attribute '{}' with timestamp type: {}", - attribute_name, - source - ))] + #[error("Error while setting attribute '{attribute_name}' with timestamp type: {source}")] ParseTimeError { attribute_name: &'static str, source: chrono::ParseError, }, - #[snafu(display( - "Error while setting attribute '{}' with uri/uriref type: {}", - attribute_name, - source - ))] + #[error("Error while setting attribute '{attribute_name}' with uri/uriref type: {source}")] ParseUrlError { attribute_name: &'static str, source: url::ParseError, diff --git a/src/message/error.rs b/src/message/error.rs index 900ae584..d77be5c9 100644 --- a/src/message/error.rs +++ b/src/message/error.rs @@ -1,38 +1,48 @@ -use snafu::Snafu; +use thiserror::Error; /// Represents an error during serialization/deserialization process -#[derive(Debug, Snafu)] +#[derive(Debug, Error)] pub enum Error { - #[snafu(display("Wrong encoding"))] + #[error("Wrong encoding")] WrongEncoding {}, - #[snafu(display("{}", source))] - #[snafu(context(false))] + #[error(transparent)] UnknownSpecVersion { + #[from] source: crate::event::UnknownSpecVersion, }, - #[snafu(display("Unknown attribute in this spec version: {}", name))] + #[error("Unknown attribute in this spec version: {name}")] UnknownAttribute { name: String }, - #[snafu(display("Error while building the final event: {}", source))] - #[snafu(context(false))] + #[error("Error while building the final event: {source}")] EventBuilderError { + #[from] source: crate::event::EventBuilderError, }, - #[snafu(display("Error while parsing a time string: {}", source))] - #[snafu(context(false))] - ParseTimeError { source: chrono::ParseError }, - #[snafu(display("Error while parsing a url: {}", source))] - #[snafu(context(false))] - ParseUrlError { source: url::ParseError }, - #[snafu(display("Error while decoding base64: {}", source))] - #[snafu(context(false))] - Base64DecodingError { source: base64::DecodeError }, - #[snafu(display("Error while serializing/deserializing to json: {}", source))] - #[snafu(context(false))] - SerdeJsonError { source: serde_json::Error }, - #[snafu(display("IO Error: {}", source))] - #[snafu(context(false))] - IOError { source: std::io::Error }, - #[snafu(display("Other error: {}", source))] + #[error("Error while parsing a time string: {source}")] + ParseTimeError { + #[from] + source: chrono::ParseError, + }, + #[error("Error while parsing a url: {source}")] + ParseUrlError { + #[from] + source: url::ParseError, + }, + #[error("Error while decoding base64: {source}")] + Base64DecodingError { + #[from] + source: base64::DecodeError, + }, + #[error("Error while serializing/deserializing to json: {source}")] + SerdeJsonError { + #[from] + source: serde_json::Error, + }, + #[error("IO Error: {source}")] + IOError { + #[from] + source: std::io::Error, + }, + #[error("Other error: {}", source)] Other { source: Box, }, From c1801187f527ae9f6af4294de420ab72846fb5b1 Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Tue, 27 Oct 2020 09:13:30 +0200 Subject: [PATCH 2/3] remove unnecessary `extern crate` from lib.rs Signed-off-by: Lachezar Lechev --- src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5ce52839..000e4e63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,11 +23,6 @@ //! * [cloudevents-sdk-reqwest](https://docs.rs/cloudevents-sdk-reqwest): Integration with [reqwest](https://github.com/seanmonstar/reqwest) //! -extern crate serde; -extern crate serde_json; -extern crate serde_value; -extern crate snafu; - /// Provides [`Event`] data structure, [`EventBuilder`] and other facilities to work with [`Event`] pub mod event; /// Provides facilities to implement Protocol Bindings From eba7ee9cb4c002034820cff9ee21bbdf14c43c0a Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Wed, 28 Oct 2020 14:32:01 +0200 Subject: [PATCH 3/3] Error - add `#[from]` attr for the source of `Error::Other` Signed-off-by: Lachezar Lechev --- src/message/error.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/message/error.rs b/src/message/error.rs index d77be5c9..76fc740b 100644 --- a/src/message/error.rs +++ b/src/message/error.rs @@ -44,6 +44,7 @@ pub enum Error { }, #[error("Other error: {}", source)] Other { + #[from] source: Box, }, }