diff options
author | Markus Unterwaditzer <markus@unterwaditzer.net> | 2018-01-11 20:36:35 +0100 |
---|---|---|
committer | Markus Unterwaditzer <markus@unterwaditzer.net> | 2018-01-11 20:36:46 +0100 |
commit | caa9d74e3551d31433cc34e94441cdd0dc120416 (patch) | |
tree | 661436901b315943ac7479ce2861ed3755e82b0e | |
parent | 4cf79bbe7ac4ef74dfc0e6ee2a5770a8b00870a7 (diff) |
add read_component
-rw-r--r-- | src/component.rs | 23 | ||||
-rw-r--r-- | 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<Component> { + 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. @@ -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; |