diff options
| author | Edward Barnard | 2015-12-25 20:45:06 +0000 |
|---|---|---|
| committer | Edward Barnard | 2015-12-26 19:36:19 +0000 |
| commit | 0ec05856dab205db2a874dc45340f8b522bd7634 (patch) | |
| tree | 708e1a76b5148ffc7813286432ff642ddd8fc3b0 | |
| parent | 261510a0c61da1afbbb4c5876023e2210fc60901 (diff) | |
| download | rust-plist-0ec05856dab205db2a874dc45340f8b522bd7634.tar.bz2 | |
Kill StartPlist and EndPlist
| -rw-r--r-- | src/binary/reader.rs | 12 | ||||
| -rw-r--r-- | src/builder.rs | 37 | ||||
| -rw-r--r-- | src/de.rs | 3 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/xml/reader.rs | 10 | ||||
| -rw-r--r-- | src/xml/writer.rs | 97 |
6 files changed, 65 insertions, 98 deletions
diff --git a/src/binary/reader.rs b/src/binary/reader.rs index 128c573..2463be2 100644 --- a/src/binary/reader.rs +++ b/src/binary/reader.rs @@ -144,7 +144,6 @@ impl<R: Read + Seek> EventReader<R> { 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() { @@ -163,7 +162,8 @@ impl<R: Read + Seek> EventReader<R> { match item.ty { StackType::Array => return Ok(Some(PlistEvent::EndArray)), StackType::Dict => return Ok(Some(PlistEvent::EndDictionary)), - StackType::Root => return Ok(Some(PlistEvent::EndPlist)), + // We're at the end of the plist + StackType::Root => return Ok(None), } } } @@ -317,8 +317,7 @@ mod tests { let streaming_parser = EventReader::new(reader); let events: Vec<PlistEvent> = streaming_parser.map(|e| e.unwrap()).collect(); - let comparison = &[StartPlist, - StartDictionary(Some(6)), + let comparison = &[StartDictionary(Some(6)), StringValue("Lines".to_owned()), StartArray(Some(2)), StringValue("It is a tale told by an idiot,".to_owned()), @@ -334,8 +333,7 @@ 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, - EndPlist]; + EndDictionary]; assert_eq!(events, comparison); } @@ -347,6 +345,6 @@ mod tests { let reader = File::open(&Path::new("./tests/data/utf16_bplist.plist")).unwrap(); let streaming_parser = EventReader::new(reader); let events: Vec<PlistEvent> = streaming_parser.map(|e| e.unwrap()).collect(); - assert_eq!(events[39], StringValue("\u{2605} or better".to_owned())); + assert_eq!(events[38], StringValue("\u{2605} or better".to_owned())); } } diff --git a/src/builder.rs b/src/builder.rs index 6ce78c5..7bb09b9 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -17,15 +17,11 @@ impl<T: Iterator<Item = Result<PlistEvent>>> Builder<T> { pub fn build(mut self) -> Result<Plist> { try!(self.bump()); - if let Some(PlistEvent::StartPlist) = self.token { - try!(self.bump()); - } let plist = try!(self.build_value()); try!(self.bump()); match self.token { None => (), - Some(PlistEvent::EndPlist) => try!(self.bump()), // The stream should have finished _ => return Err(Error::InvalidData), }; @@ -43,9 +39,6 @@ impl<T: Iterator<Item = Result<PlistEvent>>> Builder<T> { fn build_value(&mut self) -> Result<Plist> { match self.token.take() { - Some(PlistEvent::StartPlist) => Err(Error::InvalidData), - Some(PlistEvent::EndPlist) => Err(Error::InvalidData), - Some(PlistEvent::StartArray(len)) => Ok(Plist::Array(try!(self.build_array(len)))), Some(PlistEvent::StartDictionary(len)) => { Ok(Plist::Dictionary(try!(self.build_dict(len)))) @@ -116,23 +109,19 @@ mod tests { // Input - let events = vec![ - StartPlist, - StartDictionary(None), - StringValue("Author".to_owned()), - StringValue("William Shakespeare".to_owned()), - StringValue("Lines".to_owned()), - StartArray(None), - StringValue("It is a tale told by an idiot,".to_owned()), - StringValue("Full of sound and fury, signifying nothing.".to_owned()), - EndArray, - StringValue("Birthdate".to_owned()), - IntegerValue(1564), - StringValue("Height".to_owned()), - RealValue(1.60), - EndDictionary, - EndPlist, - ]; + let events = vec![StartDictionary(None), + StringValue("Author".to_owned()), + StringValue("William Shakespeare".to_owned()), + StringValue("Lines".to_owned()), + StartArray(None), + StringValue("It is a tale told by an idiot,".to_owned()), + StringValue("Full of sound and fury, signifying nothing.".to_owned()), + EndArray, + StringValue("Birthdate".to_owned()), + IntegerValue(1564), + StringValue("Height".to_owned()), + RealValue(1.60), + EndDictionary]; let builder = Builder::new(events.into_iter().map(|e| Ok(e))); let plist = builder.build(); @@ -89,9 +89,6 @@ impl<I, E> SerdeDeserializer for Deserializer<I, E> where V: Visitor { match try_next!(self.events.next()) { - PlistEvent::StartPlist => panic!(), - PlistEvent::EndPlist => panic!(), - PlistEvent::StartArray(len) => { let len = try!(u64_option_to_usize(len)); visitor.visit_seq(MapSeq::new(self, len)) @@ -167,10 +167,6 @@ impl Plist { /// ``` #[derive(Clone, Debug, PartialEq)] pub enum PlistEvent { - // TODO: Kill these. They're unnecessary. - StartPlist, - EndPlist, - // While the length of an array or dict cannot be feasably greater than max(usize) this better // conveys the concept of an effectively unbounded event stream. StartArray(Option<u64>), diff --git a/src/xml/reader.rs b/src/xml/reader.rs index e32dd4a..9f57ab4 100644 --- a/src/xml/reader.rs +++ b/src/xml/reader.rs @@ -60,7 +60,7 @@ impl<R: Read> EventReader<R> { self.element_stack.push(name.local_name.clone()); match &name.local_name[..] { - "plist" => return Some(Ok(PlistEvent::StartPlist)), + "plist" => (), "array" => return Some(Ok(PlistEvent::StartArray(None))), "dict" => return Some(Ok(PlistEvent::StartDictionary(None))), "key" => return Some(self.read_content(|s| Ok(PlistEvent::StringValue(s)))), @@ -114,7 +114,7 @@ impl<R: Read> EventReader<R> { match &name.local_name[..] { "array" => return Some(Ok(PlistEvent::EndArray)), "dict" => return Some(Ok(PlistEvent::EndDictionary)), - "plist" => return Some(Ok(PlistEvent::EndPlist)), + "plist" => (), _ => (), } } @@ -170,8 +170,7 @@ mod tests { let streaming_parser = EventReader::new(reader); let events: Vec<PlistEvent> = streaming_parser.map(|e| e.unwrap()).collect(); - let comparison = &[StartPlist, - StartDictionary(None), + let comparison = &[StartDictionary(None), StringValue("Author".to_owned()), StringValue("William Shakespeare".to_owned()), StringValue("Lines".to_owned()), @@ -189,8 +188,7 @@ mod tests { DateValue(UTC.ymd(1981, 05, 16).and_hms(11, 32, 06)), StringValue("Blank".to_owned()), StringValue("".to_owned()), - EndDictionary, - EndPlist]; + EndDictionary]; assert_eq!(events, comparison); } diff --git a/src/xml/writer.rs b/src/xml/writer.rs index 633181b..148d9ad 100644 --- a/src/xml/writer.rs +++ b/src/xml/writer.rs @@ -74,6 +74,17 @@ impl<W: Write> EventWriter<W> { Ok(()) } + fn maybe_end_plist(&mut self) -> Result<()> { + // If there are no more open tags then write the </plist> element + if self.stack.len() == 1 { + try!(self.end_element("plist")); + if let Some(Element::Root) = self.stack.pop() {} else { + return Err(Error::InvalidData); + } + } + Ok(()) + } + pub fn write(&mut self, event: &PlistEvent) -> Result<()> { <Self as EventWriterTrait>::write(self, event) } @@ -88,7 +99,11 @@ impl<W: Write> EventWriterTrait for EventWriter<W> { try!(self.write_element_and_value("key", &*value)); self.stack.push(Element::Dictionary(DictionaryState::ExpectValue)); } - PlistEvent::EndDictionary => try!(self.end_element("dict")), + PlistEvent::EndDictionary => { + try!(self.end_element("dict")); + // We might be closing the last tag here as well + try!(self.maybe_end_plist()); + } _ => return Err(Error::InvalidData), }; return Ok(()); @@ -98,39 +113,20 @@ impl<W: Write> EventWriterTrait for EventWriter<W> { } Some(other) => self.stack.push(other), None => { - match *event { - PlistEvent::StartPlist => { - let version_name = Name::local("version"); - let version_attr = Attribute::new(version_name, "1.0"); - - let result = self.xml_writer.write(WriteXmlEvent::StartElement { - name: Name::local("plist"), - attributes: Cow::Borrowed(&[version_attr]), - namespace: Cow::Borrowed(&self.empty_namespace), - }); - - match result { - Ok(()) => (), - Err(_) => return Err(Error::InvalidData), - } - - self.stack.push(Element::Root); - return Ok(()); - } - _ => return Err(Error::InvalidData), - } - } - } + let version_name = Name::local("version"); + let version_attr = Attribute::new(version_name, "1.0"); - Ok(match *event { - PlistEvent::StartPlist => return Err(Error::InvalidData), - PlistEvent::EndPlist => { - try!(self.end_element("plist")); - if let Some(Element::Root) = self.stack.pop() {} else { - return Err(Error::InvalidData); - } + try!(self.xml_writer.write(WriteXmlEvent::StartElement { + name: Name::local("plist"), + attributes: Cow::Borrowed(&[version_attr]), + namespace: Cow::Borrowed(&self.empty_namespace), + })); + + self.stack.push(Element::Root); } + } + match *event { PlistEvent::StartArray(_) => { try!(self.start_element("array")); self.stack.push(Element::Array); @@ -173,7 +169,11 @@ impl<W: Write> EventWriterTrait for EventWriter<W> { PlistEvent::StringValue(ref value) => { try!(self.write_element_and_value("string", &*value)) } - }) + }; + + try!(self.maybe_end_plist()); + + Ok(()) } } @@ -188,8 +188,7 @@ mod tests { fn streaming_parser() { use PlistEvent::*; - let plist = &[StartPlist, - StartDictionary(None), + let plist = &[StartDictionary(None), StringValue("Author".to_owned()), StringValue("William Shakespeare".to_owned()), StringValue("Lines".to_owned()), @@ -205,8 +204,7 @@ mod tests { DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), StringValue("Birthdate".to_owned()), DateValue(UTC.ymd(1981, 05, 16).and_hms(11, 32, 06)), - EndDictionary, - EndPlist]; + EndDictionary]; let mut cursor = Cursor::new(Vec::new()); @@ -220,36 +218,27 @@ mod tests { let comparison = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <plist version=\"1.0\"> - \ - <dict> + <dict> <key>Author</key> - <string>William \ - Shakespeare</string> + <string>William Shakespeare</string> <key>Lines</key> <array> - \ - <string>It is a tale told by an idiot,</string> - \ - <string>Full of sound and fury, signifying nothing.</string> - \ - </array> + <string>It is a tale told by an idiot,</string> + <string>Full of sound and fury, signifying nothing.</string> + </array> <key>Death</key> <integer>1564</integer> - \ - <key>Height</key> + <key>Height</key> <real>1.6</real> <key>Data</key> - \ - <data>AAAAvgAAAAMAAAAeAAAA</data> + <data>AAAAvgAAAAMAAAAeAAAA</data> <key>Birthdate</key> - \ - <date>1981-05-16T11:32:06+00:00</date> + <date>1981-05-16T11:32:06+00:00</date> </dict> </plist>"; - let s = String::from_utf8(cursor.into_inner()).unwrap(); - assert_eq!(&s, comparison); + assert_eq!(s, comparison); } } |
