From 2163caa2e1cf409d3bc8d0b4c010bc1bdedf1998 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 28 Oct 2018 20:51:04 +0100 Subject: Update dependency: error-chain (#26) * Update dependency: error-chain * Replace dependency: error_chain -> failure * fixup! Replace dependency: error_chain -> failure * Use enum for errors (in return types) * fixup! Use enum for errors (in return types) --- Cargo.toml | 2 +- src/component.rs | 6 +++--- src/error.rs | 38 ++++++++++++-------------------------- src/icalendar.rs | 12 ++++-------- src/lib.rs | 2 +- src/parser.rs | 35 +++++++++++++++-------------------- src/vcard.rs | 6 +----- 7 files changed, 37 insertions(+), 64 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b4beb64..d0b79b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ readme = "README.md" keywords = ["vobject", "icalendar", "calendar", "contacts"] [dependencies] -error-chain = "0.11" chrono = { version = "0.4", optional = true } +failure = "0.1" [features] default = [] diff --git a/src/component.rs b/src/component.rs index 897afa5..8330d18 100644 --- a/src/component.rs +++ b/src/component.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use property::Property; use parser::Parser; + use error::*; #[derive(Clone, Debug)] @@ -68,7 +69,7 @@ impl Component { } impl FromStr for Component { - type Err = VObjectError; + type Err = VObjectErrorKind; /// Same as `vobject::parse_component` fn from_str(s: &str) -> Result { @@ -81,8 +82,7 @@ pub fn parse_component(s: &str) -> Result { let (rv, new_s) = try!(read_component(s)); if !new_s.is_empty() { let s = format!("Trailing data: `{}`", new_s); - let kind = VObjectErrorKind::ParserError(s); - return Err(VObjectError::from_kind(kind)); + return Err(VObjectErrorKind::ParserError(s)); } Ok(rv) diff --git a/src/error.rs b/src/error.rs index 22e64a8..20654df 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,30 +1,16 @@ +#[derive(Debug, Clone, Eq, PartialEq, Fail)] +pub enum VObjectErrorKind { + #[fail(display = "Parser error: {}", _0)] + ParserError(String), -error_chain! { - - types { - VObjectError, VObjectErrorKind, ResultExt, Result; - } - - foreign_links { - ChronoParseError(::chrono::format::ParseError) #[cfg(feature = "timeconversions")]; - } - - errors { - ParserError(desc: String) { - description("Parser error") - display("{}", desc) - } - - NotAVCard { - description("Input is not a valid VCard") - display("Passed content string is not a VCard") - } - - NotAnICalendar(content: String) { - description("Input is not a valid ICalendar") - display("Not an ICalendar: '{}'", content) - } - } + #[fail(display = "Not a Vcard")] + NotAVCard, + #[fail(display = "Not a Icalendar: {}", _0)] + NotAnICalendar(String), + #[fail(display = "{}", _0)] + ChronoError(::chrono::format::ParseError), } + +pub type Result = ::std::result::Result; diff --git a/src/icalendar.rs b/src/icalendar.rs index ba2c3cf..c49e193 100644 --- a/src/icalendar.rs +++ b/src/icalendar.rs @@ -25,11 +25,7 @@ impl ICalendar { /// pub fn build(s: &str) -> Result { let c = parse_component(s)?; - Self::from_component(c) - .map_err(|_| { - let kind = VObjectErrorKind::NotAnICalendar(s.to_owned()); - VObjectError::from_kind(kind) - }) + Self::from_component(c).map_err(|_| VObjectErrorKind::NotAnICalendar(s.to_owned())) } pub fn empty() -> ICalendar { @@ -175,7 +171,7 @@ impl AsDateTime for Dtend { Ok(dt) => Ok(Time::DateTime(dt)), Err(_) => NaiveDate::parse_from_str(&self.0, DATE_FMT) .map(Time::Date) - .map_err(From::from), + .map_err(VObjectErrorKind::ChronoError), } } @@ -189,7 +185,7 @@ impl AsDateTime for Dtstart { Ok(dt) => Ok(Time::DateTime(dt)), Err(_) => NaiveDate::parse_from_str(&self.0, DATE_FMT) .map(Time::Date) - .map_err(From::from), + .map_err(VObjectErrorKind::ChronoError), } } @@ -203,7 +199,7 @@ impl AsDateTime for Dtstamp { Ok(dt) => Ok(Time::DateTime(dt)), Err(_) => NaiveDate::parse_from_str(&self.0, DATE_FMT) .map(Time::Date) - .map_err(From::from), + .map_err(VObjectErrorKind::ChronoError), } } diff --git a/src/lib.rs b/src/lib.rs index 90e3205..1aadb8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #[macro_use] -extern crate error_chain; +extern crate failure; #[cfg(feature = "timeconversions")] extern crate chrono; diff --git a/src/parser.rs b/src/parser.rs index 4943654..808bc62 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -59,14 +59,12 @@ impl<'s> Parser<'s> { let real_c = match self.peek() { Some((x, _)) => x, None => { - let kind = VObjectErrorKind::ParserError(format!("Expected {}, found EOL", c)); - return Err(VObjectError::from_kind(kind)) + return Err(VObjectErrorKind::ParserError(format!("Expected {}, found EOL", c))) } }; if real_c != c { - let kind = VObjectErrorKind::ParserError(format!("Expected {}, found {}", c, real_c)); - return Err(VObjectError::from_kind(kind)) + return Err(VObjectErrorKind::ParserError(format!("Expected {}, found {}", c, real_c))) }; Ok(()) @@ -104,8 +102,7 @@ impl<'s> Parser<'s> { Ok(()) } else { self.pos = start_pos; - let kind = VObjectErrorKind::ParserError("Expected EOL.".to_owned()); - Err(VObjectError::from_kind(kind)) + return Err(VObjectErrorKind::ParserError("Expected EOL.".to_owned())) } } @@ -174,8 +171,7 @@ impl<'s> Parser<'s> { fn consume_property_name(&mut self) -> Result { let rv = self.consume_while(|x| x == '-' || x.is_alphanumeric()); if rv.is_empty() { - let kind = VObjectErrorKind::ParserError("No property name found.".to_owned()); - Err(VObjectError::from_kind(kind)) + Err(VObjectErrorKind::ParserError("No property name found.".to_owned())) } else { Ok(rv) } @@ -207,13 +203,8 @@ impl<'s> Parser<'s> { } fn consume_param_name(&mut self) -> Result { - match self.consume_property_name() { - Ok(x) => Ok(x), - Err(e) => { - let kind = VObjectErrorKind::ParserError(format!("No param name found: {}", e)); - Err(VObjectError::from_kind(kind)) - } - } + self.consume_property_name() + .map_err(|e| VObjectErrorKind::ParserError(format!("No param name found: {}", e))) } fn consume_param_value(&mut self) -> Result { @@ -266,8 +257,7 @@ impl<'s> Parser<'s> { let mut property = try!(self.consume_property()); if property.name != "BEGIN" { self.pos = start_pos; - let kind = VObjectErrorKind::ParserError("Expected BEGIN tag.".to_owned()); - return Err(VObjectError::from_kind(kind)); + return Err(VObjectErrorKind::ParserError("Expected BEGIN tag.".to_owned())); }; // Create a component with the name of the BEGIN tag's value @@ -285,8 +275,7 @@ impl<'s> Parser<'s> { let s = format!("Mismatched tags: BEGIN:{} vs END:{}", component.name, property.raw_value); - let kind = VObjectErrorKind::ParserError(s); - return Err(VObjectError::from_kind(kind)); + return Err(VObjectErrorKind::ParserError(s)); } break; @@ -359,6 +348,7 @@ mod tests { // Test for infinite loops as well use std::sync::mpsc::{channel, RecvTimeoutError}; use std::time::Duration; + use error::VObjectErrorKind; let mut p = Parser {input: "BEGIN:a\nBEGIN:b\nEND:a", pos: 0}; let (tx, rx) = channel(); @@ -366,7 +356,12 @@ mod tests { match rx.recv_timeout(Duration::from_millis(50)) { Err(RecvTimeoutError::Timeout) => assert!(false), - Ok(Err(VObjectError(VObjectErrorKind::ParserError{..}, _ ))) => assert!(true), + Ok(Err(e)) => { + match e { + VObjectErrorKind::ParserError { .. } => assert!(true), + _ => assert!(false), + } + }, _ => assert!(false), } } diff --git a/src/vcard.rs b/src/vcard.rs index a989d19..5a4d980 100644 --- a/src/vcard.rs +++ b/src/vcard.rs @@ -25,11 +25,7 @@ impl Vcard { pub fn build(s: &str) -> Result { parse_component(s) .and_then(|c| { - Self::from_component(c) - .map_err(|_| { - let kind = VObjectErrorKind::NotAVCard; - VObjectError::from_kind(kind) - }) + Self::from_component(c).map_err(|_| VObjectErrorKind::NotAVCard) }) } -- cgit v1.2.3