aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Barnard2015-08-28 18:58:38 +0700
committerEdward Barnard2015-08-28 18:58:38 +0700
commitb5ad734bd68454fcb304a555bd2b1f0bb3274ec9 (patch)
tree1a49349cb59211402d15d4e2335faca2a4385dae /src
parent0c2ad6b0d12524b7042d45a92c1b20d8082c662c (diff)
downloadrust-plist-b5ad734bd68454fcb304a555bd2b1f0bb3274ec9.tar.bz2
Add StartPlist and EndPlist events
Diffstat (limited to 'src')
-rw-r--r--src/binary.rs9
-rw-r--r--src/lib.rs15
-rw-r--r--src/xml.rs7
3 files changed, 25 insertions, 6 deletions
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<R: Read+Seek> StreamingParser<R> {
}
fn read_next(&mut self) -> ParserResult<Option<PlistEvent>> {
- // 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<R: Read+Seek> StreamingParser<R> {
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<PlistEvent> = 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<u64>),
EndArray,
@@ -153,10 +156,15 @@ impl<T:Iterator<Item=PlistEvent>> Builder<T> {
pub fn build(mut self) -> BuilderResult<Plist> {
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<T:Iterator<Item=PlistEvent>> Builder<T> {
fn build_value(&mut self) -> BuilderResult<Plist> {
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<R: Read> Iterator for StreamingParser<R> {
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<R: Read> Iterator for StreamingParser<R> {
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<PlistEvent> = 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);