From 463bac13f984892f041a13184e1a651d00cf3af8 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 8 Apr 2020 22:42:51 -0400 Subject: untry: replace try! macro usage with the ? operator --- src/component.rs | 4 ++-- src/parser.rs | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/component.rs b/src/component.rs index 8330d18..f87e992 100644 --- a/src/component.rs +++ b/src/component.rs @@ -79,7 +79,7 @@ impl FromStr for Component { /// Parse exactly one component. Trailing data generates errors. pub fn parse_component(s: &str) -> Result { - let (rv, new_s) = try!(read_component(s)); + let (rv, new_s) = read_component(s)?; if !new_s.is_empty() { let s = format!("Trailing data: `{}`", new_s); return Err(VObjectErrorKind::ParserError(s)); @@ -91,7 +91,7 @@ pub fn parse_component(s: &str) -> Result { /// Parse one component and return the rest of the string. pub fn read_component(s: &str) -> Result<(Component, &str)> { let mut parser = Parser::new(s); - let rv = try!(parser.consume_component()); + let rv = parser.consume_component()?; let new_s = if parser.eof() { "" } else { diff --git a/src/parser.rs b/src/parser.rs index 808bc62..d46058a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -108,7 +108,7 @@ impl<'s> Parser<'s> { fn sloppy_terminate_line(&mut self) -> Result<()> { if !self.eof() { - try!(self.consume_eol()); + self.consume_eol()?; while let Ok(_) = self.consume_eol() {} }; @@ -152,13 +152,13 @@ impl<'s> Parser<'s> { pub fn consume_property(&mut self) -> Result { let group = self.consume_property_group().ok(); - let name = try!(self.consume_property_name()); + let name = self.consume_property_name()?; let params = self.consume_params(); - try!(self.assert_char(':')); + self.assert_char(':')?; self.consume_char(); - let value = try!(self.consume_property_value()); + let value = self.consume_property_value()?; Ok(Property { name: name, @@ -198,7 +198,7 @@ impl<'s> Parser<'s> { fn consume_property_value(&mut self) -> Result { let rv = self.consume_while(|x| x != '\r' && x != '\n'); - try!(self.sloppy_terminate_line()); + self.sloppy_terminate_line()?; Ok(rv) } @@ -218,7 +218,7 @@ impl<'s> Parser<'s> { if self.consume_only_char('"') { let rv = self.consume_while(qsafe); - try!(self.assert_char('"')); + self.assert_char('"')?; self.consume_char(); Ok(rv) } else { @@ -227,7 +227,7 @@ impl<'s> Parser<'s> { } fn consume_param(&mut self) -> Result<(String, String)> { - let name = try!(self.consume_param_name()); + let name = self.consume_param_name()?; let start_pos = self.pos; let value = if self.consume_only_char('=') { match self.consume_param_value() { @@ -254,7 +254,7 @@ impl<'s> Parser<'s> { pub fn consume_component(&mut self) -> Result { let start_pos = self.pos; - let mut property = try!(self.consume_property()); + let mut property = self.consume_property()?; if property.name != "BEGIN" { self.pos = start_pos; return Err(VObjectErrorKind::ParserError("Expected BEGIN tag.".to_owned())); @@ -265,10 +265,10 @@ impl<'s> Parser<'s> { loop { let previous_pos = self.pos; - property = try!(self.consume_property()); + property = self.consume_property()?; if property.name == "BEGIN" { self.pos = previous_pos; - component.subcomponents.push(try!(self.consume_component())); + component.subcomponents.push(self.consume_component()?); } else if property.name == "END" { if property.raw_value != component.name { self.pos = start_pos; -- cgit v1.2.3 From cd1c566c8a68b097235e57267b8106ceee150e2a Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 8 Apr 2020 22:44:04 -0400 Subject: error: remove the Result type alias Instead, make a local type alias that can be used within the crate, but avoid the need to export yet another symbol. --- src/component.rs | 6 +++--- src/error.rs | 2 +- src/icalendar.rs | 17 ++++++++--------- src/parser.rs | 22 +++++++++++----------- src/vcard.rs | 4 ++-- 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/component.rs b/src/component.rs index f87e992..a11d38d 100644 --- a/src/component.rs +++ b/src/component.rs @@ -72,13 +72,13 @@ impl FromStr for Component { type Err = VObjectErrorKind; /// Same as `vobject::parse_component` - fn from_str(s: &str) -> Result { + fn from_str(s: &str) -> VObjectResult { parse_component(s) } } /// Parse exactly one component. Trailing data generates errors. -pub fn parse_component(s: &str) -> Result { +pub fn parse_component(s: &str) -> VObjectResult { let (rv, new_s) = read_component(s)?; if !new_s.is_empty() { let s = format!("Trailing data: `{}`", new_s); @@ -89,7 +89,7 @@ pub fn parse_component(s: &str) -> Result { } /// Parse one component and return the rest of the string. -pub fn read_component(s: &str) -> Result<(Component, &str)> { +pub fn read_component(s: &str) -> VObjectResult<(Component, &str)> { let mut parser = Parser::new(s); let rv = parser.consume_component()?; let new_s = if parser.eof() { diff --git a/src/error.rs b/src/error.rs index b9c7b3a..28f39e0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,4 +14,4 @@ pub enum VObjectErrorKind { ChronoError(::chrono::format::ParseError), } -pub type Result = ::std::result::Result; +pub(crate) type VObjectResult = Result; diff --git a/src/icalendar.rs b/src/icalendar.rs index c49e193..4863029 100644 --- a/src/icalendar.rs +++ b/src/icalendar.rs @@ -1,4 +1,3 @@ -use std::result::Result as RResult; use std::collections::BTreeMap; use component::Component; @@ -23,7 +22,7 @@ impl ICalendar { /// Returns an error if the parsed text is not a ICalendar (that means that an error is /// returned also if this is a valid Vcard!) /// - pub fn build(s: &str) -> Result { + pub fn build(s: &str) -> VObjectResult { let c = parse_component(s)?; Self::from_component(c).map_err(|_| VObjectErrorKind::NotAnICalendar(s.to_owned())) } @@ -45,7 +44,7 @@ impl ICalendar { } /// Wrap a Component into a Vcard object, or don't do it if the Component is not a Vcard. - pub fn from_component(c: Component)-> RResult { + pub fn from_component(c: Component)-> Result { if c.name == "VCALENDAR" { Ok(ICalendar(c)) } else { @@ -99,7 +98,7 @@ impl<'a> EventIterator<'a> { } impl<'a> Iterator for EventIterator<'a> { - type Item = RResult, &'a Component>; + type Item = Result, &'a Component>; fn next(&mut self) -> Option { self.0.next().map(Event::from_component) @@ -111,7 +110,7 @@ impl<'a> Iterator for EventIterator<'a> { pub struct Event<'a>(&'a Component); impl<'a> Event<'a> { - fn from_component(c: &'a Component) -> RResult, &'a Component> { + fn from_component(c: &'a Component) -> Result, &'a Component> { if c.name == "VEVENT" { Ok(Event(c)) } else { @@ -160,13 +159,13 @@ pub enum Time { #[cfg(feature = "timeconversions")] pub trait AsDateTime { - fn as_datetime(&self) -> Result