From caa9d74e3551d31433cc34e94441cdd0dc120416 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Thu, 11 Jan 2018 20:36:35 +0100 Subject: add read_component --- src/component.rs | 23 +++++++++++++++++------ src/lib.rs | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/component.rs b/src/component.rs index 2c96a4a..897afa5 100644 --- a/src/component.rs +++ b/src/component.rs @@ -78,15 +78,26 @@ 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)); + if !new_s.is_empty() { + let s = format!("Trailing data: `{}`", new_s); + let kind = VObjectErrorKind::ParserError(s); + return Err(VObjectError::from_kind(kind)); + } + + Ok(rv) +} + +/// 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()); - if !parser.eof() { - let s = format!("Trailing data: `{}`", &parser.input[parser.pos..]); - let kind = VObjectErrorKind::ParserError(s); - Err(VObjectError::from_kind(kind)) + let new_s = if parser.eof() { + "" } else { - Ok(rv) - } + &parser.input[parser.pos..] + }; + Ok((rv, new_s)) } /// Write a component to a String. diff --git a/src/lib.rs b/src/lib.rs index 38ea59a..737a41f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ pub mod icalendar; pub use component::Component; pub use component::parse_component; +pub use component::read_component; pub use component::write_component; pub use property::Property; pub use property::escape_chars; -- cgit v1.2.3