From b5ad734bd68454fcb304a555bd2b1f0bb3274ec9 Mon Sep 17 00:00:00 2001 From: Edward Barnard Date: Fri, 28 Aug 2015 18:58:38 +0700 Subject: Add StartPlist and EndPlist events --- src/binary.rs | 9 ++++++--- src/lib.rs | 15 ++++++++++++++- src/xml.rs | 7 +++++-- 3 files changed, 25 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/binary.rs b/src/binary.rs index 826ba0b..c189c1a 100644 --- a/src/binary.rs +++ b/src/binary.rs @@ -119,9 +119,10 @@ impl StreamingParser { } fn read_next(&mut self) -> ParserResult> { - // Initialise here rather than in new if self.ref_size == 0 { + // Initialise here rather than in new try!(self.read_trailer()); + return Ok(Some(PlistEvent::StartPlist)) } let object_ref = match self.stack.last_mut() { @@ -140,7 +141,7 @@ impl StreamingParser { match item.ty { StackType::Array => return Ok(Some(PlistEvent::EndArray)), StackType::Dict => return Ok(Some(PlistEvent::EndDictionary)), - StackType::Root => return Ok(None) // Reached the end of the plist + StackType::Root => return Ok(Some(PlistEvent::EndPlist)) } } } @@ -251,6 +252,7 @@ mod tests { let events: Vec = streaming_parser.collect(); let comparison = &[ + StartPlist, StartDictionary(Some(5)), StringValue("Lines".to_owned()), StartArray(Some(2)), @@ -265,7 +267,8 @@ mod tests { StringValue("William Shakespeare".to_owned()), StringValue("Data".to_owned()), DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), - EndDictionary + EndDictionary, + EndPlist ]; assert_eq!(events, comparison); diff --git a/src/lib.rs b/src/lib.rs index 6168295..8de9efa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,9 @@ pub enum Plist { #[derive(Debug, PartialEq)] pub enum PlistEvent { + StartPlist, + EndPlist, + StartArray(Option), EndArray, @@ -153,10 +156,15 @@ impl> Builder { pub fn build(mut self) -> BuilderResult { self.bump(); + if let Some(PlistEvent::StartPlist) = self.token { + self.bump(); + } + let plist = try!(self.build_value()); self.bump(); match self.token { None => (), + Some(PlistEvent::EndPlist) => self.bump(), // The stream should have finished _ => return Err(BuilderError::InvalidEvent) }; @@ -169,6 +177,9 @@ impl> Builder { fn build_value(&mut self) -> BuilderResult { match self.token.take() { + Some(PlistEvent::StartPlist) => Err(BuilderError::InvalidEvent), + Some(PlistEvent::EndPlist) => Err(BuilderError::InvalidEvent), + Some(PlistEvent::StartArray(len)) => Ok(Plist::Array(try!(self.build_array(len)))), Some(PlistEvent::StartDictionary(len)) => Ok(Plist::Dictionary(try!(self.build_dict(len)))), @@ -240,6 +251,7 @@ mod tests { // Input let events = vec![ + StartPlist, StartDictionary(None), StringValue("Author".to_owned()), StringValue("William Shakespeare".to_owned()), @@ -252,7 +264,8 @@ mod tests { IntegerValue(1564), StringValue("Height".to_owned()), RealValue(1.60), - EndDictionary + EndDictionary, + EndPlist, ]; let builder = Builder::from_event_stream(events.into_iter()); diff --git a/src/xml.rs b/src/xml.rs index b6f8928..27eb3ae 100644 --- a/src/xml.rs +++ b/src/xml.rs @@ -46,7 +46,7 @@ impl Iterator for StreamingParser { self.element_stack.push(name.local_name.clone()); match &name.local_name[..] { - "plist" => (), + "plist" => return Some(PlistEvent::StartPlist), "array" => return Some(PlistEvent::StartArray(None)), "dict" => return Some(PlistEvent::StartDictionary(None)), "key" => return Some(self.read_content(|s| PlistEvent::StringValue(s))), @@ -86,6 +86,7 @@ impl Iterator for StreamingParser { match &name.local_name[..] { "array" => return Some(PlistEvent::EndArray), "dict" => return Some(PlistEvent::EndDictionary), + "plist" => return Some(PlistEvent::EndPlist), _ => () } }, @@ -118,6 +119,7 @@ mod tests { let events: Vec = streaming_parser.collect(); let comparison = &[ + StartPlist, StartDictionary(None), StringValue("Author".to_owned()), StringValue("William Shakespeare".to_owned()), @@ -132,7 +134,8 @@ mod tests { RealValue(1.60), StringValue("Data".to_owned()), DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), - EndDictionary + EndDictionary, + EndPlist ]; assert_eq!(events, comparison); -- cgit v1.2.3