diff options
| author | Edward Barnard | 2018-03-16 11:09:05 +0000 |
|---|---|---|
| committer | Edward Barnard | 2018-03-16 11:09:05 +0000 |
| commit | 8c2d6a8463dd00ee956d196914b65d6b235e5992 (patch) | |
| tree | b95599078518d8894fae217eec6377665ff97fde | |
| parent | c91087906540c210bdb457b32fb976ce735b22f9 (diff) | |
| download | rust-plist-8c2d6a8463dd00ee956d196914b65d6b235e5992.tar.bz2 | |
Run rustfmt.
| -rw-r--r-- | src/binary/reader.rs | 60 | ||||
| -rw-r--r-- | src/builder.rs | 40 | ||||
| -rw-r--r-- | src/date.rs | 18 | ||||
| -rw-r--r-- | src/plist.rs | 15 | ||||
| -rw-r--r-- | src/serde/de.rs | 174 | ||||
| -rw-r--r-- | src/serde/ser.rs | 299 | ||||
| -rw-r--r-- | src/xml/reader.rs | 48 | ||||
| -rw-r--r-- | src/xml/writer.rs | 57 | ||||
| -rw-r--r-- | tests/fuzzer.rs | 3 | ||||
| -rw-r--r-- | tests/serde_tests/mod.rs | 202 |
10 files changed, 520 insertions, 396 deletions
diff --git a/src/binary/reader.rs b/src/binary/reader.rs index bf21864..870dc05 100644 --- a/src/binary/reader.rs +++ b/src/binary/reader.rs @@ -1,9 +1,9 @@ use byteorder::{BigEndian, ReadBytesExt}; use std::io::{Read, Seek, SeekFrom}; use std::mem::size_of; -use std::string::{FromUtf8Error, FromUtf16Error}; +use std::string::{FromUtf16Error, FromUtf8Error}; -use {Date, Error, Result, PlistEvent, u64_to_usize}; +use {Date, Error, PlistEvent, Result, u64_to_usize}; impl From<FromUtf8Error> for Error { fn from(_: FromUtf8Error) -> Error { @@ -90,13 +90,13 @@ impl<R: Read + Seek> EventReader<R> { let offset_size = self.reader.read_u8()?; match offset_size { 1 | 2 | 4 | 8 => (), - _ => return Err(Error::InvalidData) + _ => return Err(Error::InvalidData), } self.ref_size = self.reader.read_u8()?; match self.ref_size { 1 | 2 | 4 | 8 => (), - _ => return Err(Error::InvalidData) + _ => return Err(Error::InvalidData), } let num_objects = self.reader.read_u64::<BigEndian>()?; @@ -166,7 +166,9 @@ impl<R: Read + Seek> EventReader<R> { fn seek_to_object(&mut self, object_ref: u64) -> Result<u64> { let object_ref = u64_to_usize(object_ref)?; - let offset = *self.object_offsets.get(object_ref).ok_or(Error::InvalidData)?; + let offset = *self.object_offsets + .get(object_ref) + .ok_or(Error::InvalidData)?; Ok(self.reader.seek(SeekFrom::Start(offset))?) } @@ -212,8 +214,12 @@ impl<R: Read + Seek> EventReader<R> { (0x0, 0x09) => Some(PlistEvent::BooleanValue(true)), (0x0, 0x0f) => return Err(Error::InvalidData), // fill (0x1, 0) => Some(PlistEvent::IntegerValue(self.reader.read_u8()? as i64)), - (0x1, 1) => Some(PlistEvent::IntegerValue(self.reader.read_u16::<BigEndian>()? as i64)), - (0x1, 2) => Some(PlistEvent::IntegerValue(self.reader.read_u32::<BigEndian>()? as i64)), + (0x1, 1) => Some(PlistEvent::IntegerValue( + self.reader.read_u16::<BigEndian>()? as i64, + )), + (0x1, 2) => Some(PlistEvent::IntegerValue( + self.reader.read_u32::<BigEndian>()? as i64, + )), (0x1, 3) => Some(PlistEvent::IntegerValue(self.reader.read_i64::<BigEndian>()?)), (0x1, 4) => return Err(Error::InvalidData), // 128 bit int (0x1, _) => return Err(Error::InvalidData), // variable length int @@ -223,7 +229,9 @@ impl<R: Read + Seek> EventReader<R> { (0x3, 3) => { // Date. Seconds since 1/1/2001 00:00:00. let secs = self.reader.read_f64::<BigEndian>()?; - Some(PlistEvent::DateValue(Date::from_seconds_since_plist_epoch(secs)?)) + Some(PlistEvent::DateValue( + Date::from_seconds_since_plist_epoch(secs)?, + )) } (0x4, n) => { // Data @@ -335,23 +343,25 @@ mod tests { let streaming_parser = EventReader::new(reader); let events: Vec<PlistEvent> = streaming_parser.map(|e| e.unwrap()).collect(); - let comparison = &[StartDictionary(Some(6)), - StringValue("Lines".to_owned()), - StartArray(Some(2)), - StringValue("It is a tale told by an idiot,".to_owned()), - StringValue("Full of sound and fury, signifying nothing.".to_owned()), - EndArray, - StringValue("Death".to_owned()), - IntegerValue(1564), - StringValue("Height".to_owned()), - RealValue(1.60), - StringValue("Birthdate".to_owned()), - DateValue(Date::from_chrono(Utc.ymd(1981, 05, 16).and_hms(11, 32, 06))), - StringValue("Author".to_owned()), - 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]; + let comparison = &[ + StartDictionary(Some(6)), + StringValue("Lines".to_owned()), + StartArray(Some(2)), + StringValue("It is a tale told by an idiot,".to_owned()), + StringValue("Full of sound and fury, signifying nothing.".to_owned()), + EndArray, + StringValue("Death".to_owned()), + IntegerValue(1564), + StringValue("Height".to_owned()), + RealValue(1.60), + StringValue("Birthdate".to_owned()), + DateValue(Date::from_chrono(Utc.ymd(1981, 05, 16).and_hms(11, 32, 06))), + StringValue("Author".to_owned()), + 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, + ]; assert_eq!(events, comparison); } diff --git a/src/builder.rs b/src/builder.rs index 2398a8b..6f448e8 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use {Error, Result, Plist, PlistEvent, u64_option_to_usize}; +use {Error, Plist, PlistEvent, Result, u64_option_to_usize}; pub struct Builder<T> { stream: T, @@ -107,19 +107,21 @@ mod tests { // Input - 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 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(); @@ -128,11 +130,15 @@ mod tests { let mut lines = Vec::new(); lines.push(Plist::String("It is a tale told by an idiot,".to_owned())); - lines.push(Plist::String("Full of sound and fury, signifying nothing.".to_owned())); + lines.push(Plist::String( + "Full of sound and fury, signifying nothing.".to_owned(), + )); let mut dict = BTreeMap::new(); - dict.insert("Author".to_owned(), - Plist::String("William Shakespeare".to_owned())); + dict.insert( + "Author".to_owned(), + Plist::String("William Shakespeare".to_owned()), + ); dict.insert("Lines".to_owned(), Plist::Array(lines)); dict.insert("Birthdate".to_owned(), Plist::Integer(1564)); dict.insert("Height".to_owned(), Plist::Real(1.60)); diff --git a/src/date.rs b/src/date.rs index bb1a887..43dec90 100644 --- a/src/date.rs +++ b/src/date.rs @@ -42,7 +42,9 @@ impl Date { let dur = dur + Duration::nanoseconds(submilli_nanos as i64); let plist_epoch = Utc.ymd(2001, 1, 1).and_hms(0, 0, 0); - let date = plist_epoch.checked_add_signed(dur).ok_or(Error::InvalidData)?; + let date = plist_epoch + .checked_add_signed(dur) + .ok_or(Error::InvalidData)?; Ok(Date { inner: date }) } @@ -67,7 +69,7 @@ impl Into<SystemTime> for Date { #[cfg(feature = "serde")] pub mod serde_impls { - use serde_base::de::{Deserialize, Deserializer, Error, Visitor, Unexpected}; + use serde_base::de::{Deserialize, Deserializer, Error, Unexpected, Visitor}; use serde_base::ser::{Serialize, Serializer}; use std::fmt; @@ -77,7 +79,8 @@ pub mod serde_impls { impl Serialize for Date { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> - where S: Serializer + where + S: Serializer, { let date_str = self.to_rfc3339(); serializer.serialize_newtype_struct(DATE_NEWTYPE_STRUCT_NAME, &date_str) @@ -94,7 +97,8 @@ pub mod serde_impls { } fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> - where D: Deserializer<'de> + where + D: Deserializer<'de>, { deserializer.deserialize_str(DateStrVisitor) } @@ -110,7 +114,8 @@ pub mod serde_impls { } fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> - where E: Error + where + E: Error, { Date::from_rfc3339(v).map_err(|_| E::invalid_value(Unexpected::Str(v), &self)) } @@ -118,7 +123,8 @@ pub mod serde_impls { impl<'de> Deserialize<'de> for Date { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where D: Deserializer<'de> + where + D: Deserializer<'de>, { deserializer.deserialize_newtype_struct(DATE_NEWTYPE_STRUCT_NAME, DateNewtypeVisitor) } diff --git a/src/plist.rs b/src/plist.rs index 9fbfe5a..f0646f5 100644 --- a/src/plist.rs +++ b/src/plist.rs @@ -22,7 +22,8 @@ impl Plist { } pub fn from_events<T>(events: T) -> Result<Plist> - where T: IntoIterator<Item = Result<PlistEvent>> + where + T: IntoIterator<Item = Result<PlistEvent>>, { let iter = events.into_iter(); let builder = builder::Builder::new(iter); @@ -356,8 +357,10 @@ mod tests { let slice: &[u8] = &[1, 2, 3]; assert_eq!(Plist::Data(slice.to_vec()).as_data(), Some(slice)); - assert_eq!(Plist::Data(slice.to_vec()).into_data(), - Some(slice.to_vec())); + assert_eq!( + Plist::Data(slice.to_vec()).into_data(), + Some(slice.to_vec()) + ); let date: Date = SystemTime::now().into(); assert_eq!(Plist::Date(date.clone()).as_date(), Some(&date)); @@ -365,7 +368,9 @@ mod tests { assert_eq!(Plist::Real(0.0).as_real(), Some(0.0)); assert_eq!(Plist::Integer(1).as_integer(), Some(1)); assert_eq!(Plist::String("2".to_owned()).as_string(), Some("2")); - assert_eq!(Plist::String("t".to_owned()).into_string(), - Some("t".to_owned())); + assert_eq!( + Plist::String("t".to_owned()).into_string(), + Some("t".to_owned()) + ); } } diff --git a/src/serde/de.rs b/src/serde/de.rs index a33b480..72f2fab 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -42,26 +42,32 @@ impl de::Error for Error { } pub struct Deserializer<I> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { events: Peekable<<I as IntoIterator>::IntoIter>, } impl<I> Deserializer<I> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { pub fn new(iter: I) -> Deserializer<I> { - Deserializer { events: iter.into_iter().peekable() } + Deserializer { + events: iter.into_iter().peekable(), + } } } impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { type Error = Error; fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { match try_next!(self.events.next()) { PlistEvent::StartArray(len) => { @@ -97,14 +103,16 @@ impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I> } fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { expect!(self.events.next(), PlistEvent::StringValue(_)); visitor.visit_unit() } fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { expect!(self.events.next(), PlistEvent::StartDictionary(_)); @@ -122,21 +130,25 @@ impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I> Ok(ret) } - fn deserialize_newtype_struct<V>(self, - _name: &'static str, - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn deserialize_newtype_struct<V>( + self, + _name: &'static str, + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { visitor.visit_newtype_struct(self) } - fn deserialize_struct<V>(self, - _name: &'static str, - _fields: &'static [&'static str], - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn deserialize_struct<V>( + self, + _name: &'static str, + _fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { expect!(self.events.next(), PlistEvent::StartDictionary(_)); let ret = visitor.visit_map(MapAndSeqAccess::new(self, true, None))?; @@ -144,12 +156,14 @@ impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I> Ok(ret) } - fn deserialize_enum<V>(self, - _enum: &'static str, - _variants: &'static [&'static str], - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn deserialize_enum<V>( + self, + _enum: &'static str, + _variants: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { expect!(self.events.next(), PlistEvent::StartDictionary(_)); let ret = visitor.visit_enum(&mut *self)?; @@ -159,20 +173,23 @@ impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer<I> } impl<'de, 'a, I> de::EnumAccess<'de> for &'a mut Deserializer<I> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { type Error = Error; type Variant = Self; fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Self::Error> - where V: de::DeserializeSeed<'de> + where + V: de::DeserializeSeed<'de>, { Ok((seed.deserialize(&mut *self)?, self)) } } impl<'de, 'a, I> de::VariantAccess<'de> for &'a mut Deserializer<I> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { type Error = Error; @@ -181,22 +198,26 @@ impl<'de, 'a, I> de::VariantAccess<'de> for &'a mut Deserializer<I> } fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> - where T: de::DeserializeSeed<'de> + where + T: de::DeserializeSeed<'de>, { seed.deserialize(self) } fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { <Self as de::Deserializer>::deserialize_tuple(self, len, visitor) } - fn struct_variant<V>(self, - fields: &'static [&'static str], - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn struct_variant<V>( + self, + fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { let name = ""; <Self as de::Deserializer>::deserialize_struct(self, name, fields, visitor) @@ -204,18 +225,21 @@ impl<'de, 'a, I> de::VariantAccess<'de> for &'a mut Deserializer<I> } pub struct StructValueDeserializer<'a, I: 'a> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { de: &'a mut Deserializer<I>, } impl<'de, 'a, I> de::Deserializer<'de> for StructValueDeserializer<'a, I> - where I: IntoIterator<Item = Result<PlistEvent, Error>> +where + I: IntoIterator<Item = Result<PlistEvent, Error>>, { type Error = Error; fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { self.de.deserialize_any(visitor) } @@ -227,50 +251,59 @@ impl<'de, 'a, I> de::Deserializer<'de> for StructValueDeserializer<'a, I> } fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { self.de.deserialize_unit(visitor) } fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + where + V: de::Visitor<'de>, { // None struct values are ignored so if we're here the value must be Some. visitor.visit_some(self.de) } - fn deserialize_newtype_struct<V>(self, - name: &'static str, - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn deserialize_newtype_struct<V>( + self, + name: &'static str, + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { self.de.deserialize_newtype_struct(name, visitor) } - fn deserialize_struct<V>(self, - name: &'static str, - fields: &'static [&'static str], - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn deserialize_struct<V>( + self, + name: &'static str, + fields: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { self.de.deserialize_struct(name, fields, visitor) } - fn deserialize_enum<V>(self, - enum_: &'static str, - variants: &'static [&'static str], - visitor: V) - -> Result<V::Value, Self::Error> - where V: de::Visitor<'de> + fn deserialize_enum<V>( + self, + enum_: &'static str, + variants: &'static [&'static str], + visitor: V, + ) -> Result<V::Value, Self::Error> + where + V: de::Visitor<'de>, { self.de.deserialize_enum(enum_, variants, visitor) } } struct MapAndSeqAccess<'a, I> - where I: 'a + IntoIterator<Item = Result<PlistEvent, Error>> +where + I: 'a + IntoIterator<Item = Result<PlistEvent, Error>>, { de: &'a mut Deserializer<I>, is_struct: bool, @@ -278,12 +311,14 @@ struct MapAndSeqAccess<'a, I> } impl<'a, I> MapAndSeqAccess<'a, I> - where I: 'a + IntoIterator<Item = Result<PlistEvent, Error>> +where + I: 'a + IntoIterator<Item = Result<PlistEvent, Error>>, { - fn new(de: &'a mut Deserializer<I>, - is_struct: bool, - len: Option<usize>) - -> MapAndSeqAccess<'a, I> { + fn new( + de: &'a mut Deserializer<I>, + is_struct: bool, + len: Option<usize>, + ) -> MapAndSeqAccess<'a, I> { MapAndSeqAccess { de: de, is_struct: is_struct, @@ -293,12 +328,14 @@ impl<'a, I> MapAndSeqAccess<'a, I> } impl<'de, 'a, I> de::SeqAccess<'de> for MapAndSeqAccess<'a, I> - where I: 'a + IntoIterator<Item = Result<PlistEvent, Error>> +where + I: 'a + IntoIterator<Item = Result<PlistEvent, Error>>, { type Error = Error; fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> - where T: de::DeserializeSeed<'de> + where + T: de::DeserializeSeed<'de>, { if let Some(&Ok(PlistEvent::EndArray)) = self.de.events.peek() { return Ok(None); @@ -314,12 +351,14 @@ impl<'de, 'a, I> de::SeqAccess<'de> for MapAndSeqAccess<'a, I> } impl<'de, 'a, I> de::MapAccess<'de> for MapAndSeqAccess<'a, I> - where I: 'a + IntoIterator<Item = Result<PlistEvent, Error>> +where + I: 'a + IntoIterator<Item = Result<PlistEvent, Error>>, { type Error = Error; fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> - where K: de::DeserializeSeed<'de> + where + K: de::DeserializeSeed<'de>, { if let Some(&Ok(PlistEvent::EndDictionary)) = self.de.events.peek() { return Ok(None); @@ -330,7 +369,8 @@ impl<'de, 'a, I> de::MapAccess<'de> for MapAndSeqAccess<'a, I> } fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error> - where V: de::DeserializeSeed<'de> + where + V: de::DeserializeSeed<'de>, { if self.is_struct { seed.deserialize(StructValueDeserializer { de: &mut *self.de }) diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 5e04d2e..3a05296 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -130,21 +130,23 @@ impl<'a, W: EventWriter> ser::Serializer for &'a mut Serializer<W> { self.serialize_unit() } - fn serialize_unit_variant(self, - _name: &'static str, - _variant_index: u32, - variant: &'static str) - -> Result<(), Self::Error> { + fn serialize_unit_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + ) -> Result<(), Self::Error> { self.single_key_dict(variant.to_owned())?; self.serialize_unit()?; self.single_key_dict_end()?; Ok(()) } - fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(self, - name: &'static str, - value: &T) - -> Result<(), Self::Error> { + fn serialize_newtype_struct<T: ?Sized + ser::Serialize>( + self, + name: &'static str, + value: &T, + ) -> Result<(), Self::Error> { if name == DATE_NEWTYPE_STRUCT_NAME { value.serialize(DateSerializer { ser: &mut *self }) } else { @@ -152,12 +154,13 @@ impl<'a, W: EventWriter> ser::Serializer for &'a mut Serializer<W> { } } - fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(self, - _name: &'static str, - _variant_index: u32, - variant: &'static str, - value: &T) - -> Result<(), Self::Error> { + fn serialize_newtype_variant<T: ?Sized + ser::Serialize>( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result<(), Self::Error> { self.single_key_dict(variant.to_owned())?; value.serialize(&mut *self)?; self.single_key_dict_end() @@ -173,19 +176,21 @@ impl<'a, W: EventWriter> ser::Serializer for &'a mut Serializer<W> { self.serialize_seq(Some(len)) } - fn serialize_tuple_struct(self, - _name: &'static str, - len: usize) - -> Result<Self::SerializeTupleStruct, Self::Error> { + fn serialize_tuple_struct( + self, + _name: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleStruct, Self::Error> { self.serialize_tuple(len) } - fn serialize_tuple_variant(self, - _name: &'static str, - _variant_index: u32, - variant: &'static str, - len: usize) - -> Result<Self::SerializeTupleVariant, Self::Error> { + fn serialize_tuple_variant( + self, + _name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleVariant, Self::Error> { self.single_key_dict(variant.to_owned())?; self.serialize_tuple(len) } @@ -196,20 +201,22 @@ impl<'a, W: EventWriter> ser::Serializer for &'a mut Serializer<W> { Ok(Compound { ser: self }) } - fn serialize_struct(self, - _name: &'static str, - _len: usize) - -> Result<Self::SerializeStruct, Self::Error> { + fn serialize_struct( + self, + _name: &'static str, + _len: usize, + ) -> Result<Self::SerializeStruct, Self::Error> { // The number of struct fields is not known as fields with None values are ignored. self.serialize_map(None) } - fn serialize_struct_variant(self, - name: &'static str, - _variant_index: u32, - variant: &'static str, - len: usize) - -> Result<Self::SerializeStructVariant, Self::Error> { + fn serialize_struct_variant( + self, + name: &'static str, + _variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeStructVariant, Self::Error> { self.single_key_dict(variant.to_owned())?; self.serialize_struct(name, len) } @@ -223,7 +230,8 @@ struct StructFieldSerializer<'a, W: 'a + EventWriter> { impl<'a, W: EventWriter> StructFieldSerializer<'a, W> { fn use_ser(self) -> Result<&'a mut Serializer<W>, Error> { // We are going to serialize something so write the struct field name. - self.ser.emit(PlistEvent::StringValue(self.field_name.to_owned()))?; + self.ser + .emit(PlistEvent::StringValue(self.field_name.to_owned()))?; Ok(self.ser) } } @@ -314,28 +322,33 @@ impl<'a, W: EventWriter> ser::Serializer for StructFieldSerializer<'a, W> { self.use_ser()?.serialize_unit_struct(name) } - fn serialize_unit_variant(self, - name: &'static str, - variant_index: u32, - variant: &'static str) - -> Result<(), Self::Error> { - self.use_ser()?.serialize_unit_variant(name, variant_index, variant) + fn serialize_unit_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + ) -> Result<(), Self::Error> { + self.use_ser()? + .serialize_unit_variant(name, variant_index, variant) } - fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(self, - name: &'static str, - value: &T) - -> Result<(), Self::Error> { + fn serialize_newtype_struct<T: ?Sized + ser::Serialize>( + self, + name: &'static str, + value: &T, + ) -> Result<(), Self::Error> { self.use_ser()?.serialize_newtype_struct(name, value) } - fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(self, - name: &'static str, - variant_index: u32, - variant: &'static str, - value: &T) - -> Result<(), Self::Error> { - self.use_ser()?.serialize_newtype_variant(name, variant_index, variant, value) + fn serialize_newtype_variant<T: ?Sized + ser::Serialize>( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + value: &T, + ) -> Result<(), Self::Error> { + self.use_ser()? + .serialize_newtype_variant(name, variant_index, variant, value) } fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { @@ -346,40 +359,46 @@ impl<'a, W: EventWriter> ser::Serializer for StructFieldSerializer<'a, W> { self.use_ser()?.serialize_tuple(len) } - fn serialize_tuple_struct(self, - name: &'static str, - len: usize) - -> Result<Self::SerializeTupleStruct, Self::Error> { + fn serialize_tuple_struct( + self, + name: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleStruct, Self::Error> { self.use_ser()?.serialize_tuple_struct(name, len) } - fn serialize_tuple_variant(self, - name: &'static str, - variant_index: u32, - variant: &'static str, - len: usize) - -> Result<Self::SerializeTupleVariant, Self::Error> { - self.use_ser()?.serialize_tuple_variant(name, variant_index, variant, len) + fn serialize_tuple_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeTupleVariant, Self::Error> { + self.use_ser()? + .serialize_tuple_variant(name, variant_index, variant, len) } fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { self.use_ser()?.serialize_map(len) } - fn serialize_struct(self, - name: &'static str, - len: usize) - -> Result<Self::SerializeStruct, Self::Error> { + fn serialize_struct( + self, + name: &'static str, + len: usize, + ) -> Result<Self::SerializeStruct, Self::Error> { self.use_ser()?.serialize_struct(name, len) } - fn serialize_struct_variant(self, - name: &'static str, - variant_index: u32, - variant: &'static str, - len: usize) - -> Result<Self::SerializeStructVariant, Self::Error> { - self.use_ser()?.serialize_struct_variant(name, variant_index, variant, len) + fn serialize_struct_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize, + ) -> Result<Self::SerializeStructVariant, Self::Error> { + self.use_ser()? + .serialize_struct_variant(name, variant_index, variant, len) } } @@ -478,27 +497,30 @@ impl<'a, W: EventWriter> ser::Serializer for DateSerializer<'a, W> { Err(self.expecting_date_error()) } - fn serialize_unit_variant(self, - _: &'static str, - _: u32, - _: &'static str) - -> Result<(), Self::Error> { + fn serialize_unit_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + ) -> Result<(), Self::Error> { Err(self.expecting_date_error()) } - fn serialize_newtype_struct<T: ?Sized + ser::Serialize>(self, - _: &'static str, - _: &T) - -> Result<(), Self::Error> { + fn serialize_newtype_struct<T: ?Sized + ser::Serialize>( + self, + _: &'static str, + _: &T, + ) -> Result<(), Self::Error> { Err(self.expecting_date_error()) } - fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(self, - _: &'static str, - _: u32, - _: &'static str, - _: &T) - -> Result<(), Self::Error> { + fn serialize_newtype_variant<T: ?Sized + ser::Serialize>( + self, + _: &'static str, + _: u32, + _: &'static str, + _: &T, + ) -> Result<(), Self::Error> { Err(self.expecting_date_error()) } @@ -510,19 +532,21 @@ impl<'a, W: EventWriter> ser::Serializer for DateSerializer<'a, W> { Err(self.expecting_date_error()) } - fn serialize_tuple_struct(self, - _: &'static str, - _: usize) - -> Result<Self::SerializeTupleStruct, Self::Error> { + fn serialize_tuple_struct( + self, + _: &'static str, + _: usize, + ) -> Result<Self::SerializeTupleStruct, Self::Error> { Err(self.expecting_date_error()) } - fn serialize_tuple_variant(self, - _: &'static str, - _: u32, - _: &'static str, - _: usize) - -> Result<Self::SerializeTupleVariant, Self::Error> { + fn serialize_tuple_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + _: usize, + ) -> Result<Self::SerializeTupleVariant, Self::Error> { Err(self.expecting_date_error()) } @@ -530,19 +554,21 @@ impl<'a, W: EventWriter> ser::Serializer for DateSerializer<'a, W> { Err(self.expecting_date_error()) } - fn serialize_struct(self, - _: &'static str, - _: usize) - -> Result<Self::SerializeStruct, Self::Error> { + fn serialize_struct( + self, + _: &'static str, + _: usize, + ) -> Result<Self::SerializeStruct, Self::Error> { Err(self.expecting_date_error()) } - fn serialize_struct_variant(self, - _: &'static str, - _: u32, - _: &'static str, - _: usize) - -> Result<Self::SerializeStructVariant, Self::Error> { + fn serialize_struct_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + _: usize, + ) -> Result<Self::SerializeStructVariant, Self::Error> { Err(self.expecting_date_error()) } } @@ -556,9 +582,10 @@ impl<'a, W: EventWriter> ser::SerializeSeq for Compound<'a, W> { type Ok = (); type Error = Error; - fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, - value: &T) - -> Result<(), Self::Error> { + fn serialize_element<T: ?Sized + ser::Serialize>( + &mut self, + value: &T, + ) -> Result<(), Self::Error> { value.serialize(&mut *self.ser) } @@ -571,9 +598,10 @@ impl<'a, W: EventWriter> ser::SerializeTuple for Compound<'a, W> { type Ok = (); type Error = Error; - fn serialize_element<T: ?Sized + ser::Serialize>(&mut self, - value: &T) - -> Result<(), Self::Error> { + fn serialize_element<T: ?Sized + ser::Serialize>( + &mut self, + value: &T, + ) -> Result<(), Self::Error> { <Self as ser::SerializeSeq>::serialize_element(self, value) } @@ -586,9 +614,10 @@ impl<'a, W: EventWriter> ser::SerializeTupleStruct for Compound<'a, W> { type Ok = (); type Error = Error; - fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, - value: &T) - -> Result<(), Self::Error> { + fn serialize_field<T: ?Sized + ser::Serialize>( + &mut self, + value: &T, + ) -> Result<(), Self::Error> { <Self as ser::SerializeSeq>::serialize_element(self, value) } @@ -601,9 +630,10 @@ impl<'a, W: EventWriter> ser::SerializeTupleVariant for Compound<'a, W> { type Ok = (); type Error = Error; - fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, - value: &T) - -> Result<(), Self::Error> { + fn serialize_field<T: ?Sized + ser::Serialize>( + &mut self, + value: &T, + ) -> Result<(), Self::Error> { <Self as ser::SerializeSeq>::serialize_element(self, value) } @@ -621,9 +651,10 @@ impl<'a, W: EventWriter> ser::SerializeMap for Compound<'a, W> { key.serialize(&mut *self.ser) } - fn serialize_value<T: ?Sized + ser::Serialize>(&mut self, - value: &T) - -> Result<(), Self::Error> { + fn serialize_value<T: ?Sized + ser::Serialize>( + &mut self, + value: &T, + ) -> Result<(), Self::Error> { value.serialize(&mut *self.ser) } @@ -636,10 +667,11 @@ impl<'a, W: EventWriter> ser::SerializeStruct for Compound<'a, W> { type Ok = (); type Error = Error; - fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, - key: &'static str, - value: &T) - -> Result<(), Self::Error> { + fn serialize_field<T: ?Sized + ser::Serialize>( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> { // We don't want to serialize None if the Option is a struct field as this is how null // fields are represented in plists. value.serialize(StructFieldSerializer { @@ -657,10 +689,11 @@ impl<'a, W: EventWriter> ser::SerializeStructVariant for Compound<'a, W> { type Ok = (); type Error = Error; - fn serialize_field<T: ?Sized + ser::Serialize>(&mut self, - key: &'static str, - value: &T) - -> Result<(), Self::Error> { + fn serialize_field<T: ?Sized + ser::Serialize>( + &mut self, + key: &'static str, + value: &T, + ) -> Result<(), Self::Error> { <Self as ser::SerializeStruct>::serialize_field(self, key, value) } diff --git a/src/xml/reader.rs b/src/xml/reader.rs index 66fd8f1..c1c2b3b 100644 --- a/src/xml/reader.rs +++ b/src/xml/reader.rs @@ -3,7 +3,7 @@ use std::io::Read; use std::str::FromStr; use xml_rs::reader::{EventReader as XmlEventReader, ParserConfig, XmlEvent}; -use {Date, Error, Result, PlistEvent}; +use {Date, Error, PlistEvent, Result}; pub struct EventReader<R: Read> { xml_reader: XmlEventReader<R>, @@ -30,7 +30,8 @@ impl<R: Read> EventReader<R> { } fn read_content<F>(&mut self, f: F) -> Result<PlistEvent> - where F: FnOnce(String) -> Result<PlistEvent> + where + F: FnOnce(String) -> Result<PlistEvent>, { match self.xml_reader.next() { Ok(XmlEvent::Characters(s)) => f(s), @@ -66,7 +67,8 @@ impl<R: Read> EventReader<R> { "false" => return Some(Ok(PlistEvent::BooleanValue(false))), "data" => { return Some(self.read_content(|s| { - let data = base64::decode_config(&s, base64::MIME).map_err(|_| Error::InvalidData)?; + let data = base64::decode_config(&s, base64::MIME) + .map_err(|_| Error::InvalidData)?; Ok(PlistEvent::DataValue(data)) })) } @@ -161,25 +163,27 @@ mod tests { let streaming_parser = EventReader::new(reader); let events: Vec<PlistEvent> = streaming_parser.map(|e| e.unwrap()).collect(); - let comparison = &[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("Death".to_owned()), - IntegerValue(1564), - StringValue("Height".to_owned()), - RealValue(1.60), - StringValue("Data".to_owned()), - DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), - StringValue("Birthdate".to_owned()), - DateValue(Date::from_chrono(Utc.ymd(1981, 05, 16).and_hms(11, 32, 06))), - StringValue("Blank".to_owned()), - StringValue("".to_owned()), - EndDictionary]; + let comparison = &[ + 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("Death".to_owned()), + IntegerValue(1564), + StringValue("Height".to_owned()), + RealValue(1.60), + StringValue("Data".to_owned()), + DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), + StringValue("Birthdate".to_owned()), + DateValue(Date::from_chrono(Utc.ymd(1981, 05, 16).and_hms(11, 32, 06))), + StringValue("Blank".to_owned()), + StringValue("".to_owned()), + EndDictionary, + ]; assert_eq!(events, comparison); } diff --git a/src/xml/writer.rs b/src/xml/writer.rs index ffb8aa4..77ccd87 100644 --- a/src/xml/writer.rs +++ b/src/xml/writer.rs @@ -4,7 +4,7 @@ use std::io::Write; use xml_rs::attribute::Attribute; use xml_rs::name::Name; use xml_rs::namespace::Namespace; -use xml_rs::writer::{Error as XmlWriterError, EventWriter as XmlEventWriter, EmitterConfig}; +use xml_rs::writer::{EmitterConfig, Error as XmlWriterError, EventWriter as XmlEventWriter}; use xml_rs::writer::events::XmlEvent as WriteXmlEvent; use {Error, EventWriter as PlistEventWriter, PlistEvent, Result}; @@ -72,7 +72,9 @@ impl<W: Write> EventWriter<W> { } fn end_element(&mut self, name: &str) -> Result<()> { - self.xml_writer.write(WriteXmlEvent::EndElement { name: Some(Name::local(name)) })?; + self.xml_writer.write(WriteXmlEvent::EndElement { + name: Some(Name::local(name)), + })?; Ok(()) } @@ -105,7 +107,8 @@ impl<W: Write> PlistEventWriter for EventWriter<W> { match *event { PlistEvent::StringValue(ref value) => { self.write_element_and_value("key", &*value)?; - self.stack.push(Element::Dictionary(DictionaryState::ExpectValue)); + self.stack + .push(Element::Dictionary(DictionaryState::ExpectValue)); } PlistEvent::EndDictionary => { self.end_element("dict")?; @@ -116,9 +119,8 @@ impl<W: Write> PlistEventWriter for EventWriter<W> { }; return Ok(()); } - Some(Element::Dictionary(DictionaryState::ExpectValue)) => { - self.stack.push(Element::Dictionary(DictionaryState::ExpectKey)) - } + Some(Element::Dictionary(DictionaryState::ExpectValue)) => self.stack + .push(Element::Dictionary(DictionaryState::ExpectKey)), Some(other) => self.stack.push(other), None => { let version_name = Name::local("version"); @@ -149,7 +151,8 @@ impl<W: Write> PlistEventWriter for EventWriter<W> { PlistEvent::StartDictionary(_) => { self.start_element("dict")?; - self.stack.push(Element::Dictionary(DictionaryState::ExpectKey)); + self.stack + .push(Element::Dictionary(DictionaryState::ExpectKey)); } PlistEvent::EndDictionary => return Err(Error::InvalidData), @@ -174,9 +177,7 @@ impl<W: Write> PlistEventWriter for EventWriter<W> { PlistEvent::RealValue(ref value) => { self.write_element_and_value("real", &value.to_string())? } - PlistEvent::StringValue(ref value) => { - self.write_element_and_value("string", &*value)? - } + PlistEvent::StringValue(ref value) => self.write_element_and_value("string", &*value)?, }; self.maybe_end_plist()?; @@ -197,23 +198,25 @@ mod tests { fn streaming_parser() { use PlistEvent::*; - let plist = &[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("Death".to_owned()), - IntegerValue(1564), - StringValue("Height".to_owned()), - RealValue(1.60), - StringValue("Data".to_owned()), - DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), - StringValue("Birthdate".to_owned()), - DateValue(Date::from_chrono(Utc.ymd(1981, 05, 16).and_hms(11, 32, 06))), - EndDictionary]; + let plist = &[ + 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("Death".to_owned()), + IntegerValue(1564), + StringValue("Height".to_owned()), + RealValue(1.60), + StringValue("Data".to_owned()), + DataValue(vec![0, 0, 0, 190, 0, 0, 0, 3, 0, 0, 0, 30, 0, 0, 0]), + StringValue("Birthdate".to_owned()), + DateValue(Date::from_chrono(Utc.ymd(1981, 05, 16).and_hms(11, 32, 06))), + EndDictionary, + ]; let mut cursor = Cursor::new(Vec::new()); diff --git a/tests/fuzzer.rs b/tests/fuzzer.rs index 44d4297..8ed21f2 100644 --- a/tests/fuzzer.rs +++ b/tests/fuzzer.rs @@ -48,7 +48,8 @@ fn binary_circular_array() { // Issue 20 - not found by fuzzing but this is a convenient place to put the test. #[test] fn issue_20_binary_with_data_in_trailer() { - let data = b"bplist00\xd0\x08\0\0\0\0\0\0\x01\x01\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\t"; + let data = + b"bplist00\xd0\x08\0\0\0\0\0\0\x01\x01\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\t"; test_fuzzer_data_ok(data); } diff --git a/tests/serde_tests/mod.rs b/tests/serde_tests/mod.rs index d8583a2..c4b8c4a 100644 --- a/tests/serde_tests/mod.rs +++ b/tests/serde_tests/mod.rs @@ -1,5 +1,5 @@ use plist::{Date, EventWriter, PlistEvent, Result as PlistResult}; -use plist::serde::{Serializer, Deserializer}; +use plist::serde::{Deserializer, Serializer}; use plist::PlistEvent::*; use serde::de::DeserializeOwned; use serde::ser::Serialize; @@ -37,7 +37,8 @@ fn new_deserializer(events: Vec<PlistEvent>) -> Deserializer<Vec<PlistResult<Pli } fn assert_roundtrip<T>(obj: T, comparison: Option<&[PlistEvent]>) - where T: Debug + DeserializeOwned + PartialEq + Serialize +where + T: Debug + DeserializeOwned + PartialEq + Serialize, { let mut se = new_serializer(); @@ -84,75 +85,82 @@ struct DogInner { fn cow() { let cow = Animal::Cow; - let comparison = &[StartDictionary(Some(1)), - StringValue("Cow".to_owned()), - StringValue("".to_owned()), - EndDictionary]; + let comparison = &[ + StartDictionary(Some(1)), + StringValue("Cow".to_owned()), + StringValue("".to_owned()), + EndDictionary, + ]; assert_roundtrip(cow, Some(comparison)); } - #[test] fn dog() { let dog = Animal::Dog(DogOuter { - inner: vec![DogInner { - a: (), - b: 12, - c: vec!["a".to_string(), "b".to_string()], - }], + inner: vec![ + DogInner { + a: (), + b: 12, + c: vec!["a".to_string(), "b".to_string()], + }, + ], }); - let comparison = &[StartDictionary(Some(1)), - StringValue("Dog".to_owned()), - StartDictionary(None), - StringValue("inner".to_owned()), - StartArray(Some(1)), - StartDictionary(None), - StringValue("a".to_owned()), - StringValue("".to_owned()), - StringValue("b".to_owned()), - IntegerValue(12), - StringValue("c".to_owned()), - StartArray(Some(2)), - StringValue("a".to_owned()), - StringValue("b".to_owned()), - EndArray, - EndDictionary, - EndArray, - EndDictionary, - EndDictionary]; + let comparison = &[ + StartDictionary(Some(1)), + StringValue("Dog".to_owned()), + StartDictionary(None), + StringValue("inner".to_owned()), + StartArray(Some(1)), + StartDictionary(None), + StringValue("a".to_owned()), + StringValue("".to_owned()), + StringValue("b".to_owned()), + IntegerValue(12), + StringValue("c".to_owned()), + StartArray(Some(2)), + StringValue("a".to_owned()), + StringValue("b".to_owned()), + EndArray, + EndDictionary, + EndArray, + EndDictionary, + EndDictionary, + ]; assert_roundtrip(dog, Some(comparison)); } - #[test] fn frog() { - let frog = Animal::Frog(Ok("hello".to_owned()), - vec![1.0, 2.0, 3.14159, 0.000000001, 1.27e31]); - - let comparison = &[StartDictionary(Some(1)), - StringValue("Frog".to_owned()), - StartArray(Some(2)), - StartDictionary(Some(1)), - StringValue("Ok".to_owned()), - StringValue("hello".to_owned()), - EndDictionary, - StartArray(Some(5)), - RealValue(1.0), - RealValue(2.0), - RealValue(3.14159), - RealValue(0.000000001), - RealValue(1.27e31), - EndArray, - EndArray, - EndDictionary]; + let frog = Animal::Frog( + Ok("hello".to_owned()), + vec![1.0, 2.0, 3.14159, 0.000000001, 1.27e31], + ); + + let comparison = &[ + StartDictionary(Some(1)), + StringValue("Frog".to_owned()), + StartArray(Some(2)), + StartDictionary(Some(1)), + StringValue("Ok".to_owned()), + StringValue("hello".to_owned()), + EndDictionary, + StartArray(Some(5)), + RealValue(1.0), + RealValue(2.0), + RealValue(3.14159), + RealValue(0.000000001), + RealValue(1.27e31), + EndArray, + EndArray, + EndDictionary, + ]; assert_roundtrip(frog, Some(comparison)); } - #[test] fn cat() { let cat = Animal::Cat { @@ -161,27 +169,29 @@ fn cat() { firmware: Some(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]), }; - let comparison = &[StartDictionary(Some(1)), - StringValue("Cat".to_owned()), - StartDictionary(None), - StringValue("age".to_owned()), - IntegerValue(12), - StringValue("name".to_owned()), - StringValue("Paws".to_owned()), - StringValue("firmware".to_owned()), - StartArray(Some(9)), - IntegerValue(0), - IntegerValue(1), - IntegerValue(2), - IntegerValue(3), - IntegerValue(4), - IntegerValue(5), - IntegerValue(6), - IntegerValue(7), - IntegerValue(8), - EndArray, - EndDictionary, - EndDictionary]; + let comparison = &[ + StartDictionary(Some(1)), + StringValue("Cat".to_owned()), + StartDictionary(None), + StringValue("age".to_owned()), + IntegerValue(12), + StringValue("name".to_owned()), + StringValue("Paws".to_owned()), + StringValue("firmware".to_owned()), + StartArray(Some(9)), + IntegerValue(0), + IntegerValue(1), + IntegerValue(2), + IntegerValue(3), + IntegerValue(4), + IntegerValue(5), + IntegerValue(6), + IntegerValue(7), + IntegerValue(8), + EndArray, + EndDictionary, + EndDictionary, + ]; assert_roundtrip(cat, Some(comparison)); } @@ -196,11 +206,13 @@ struct NewtypeInner(u8, u8, u8); fn newtype_struct() { let newtype = NewtypeStruct(NewtypeInner(34, 32, 13)); - let comparison = &[StartArray(Some(3)), - IntegerValue(34), - IntegerValue(32), - IntegerValue(13), - EndArray]; + let comparison = &[ + StartArray(Some(3)), + IntegerValue(34), + IntegerValue(32), + IntegerValue(13), + EndArray, + ]; assert_roundtrip(newtype, Some(comparison)); } @@ -226,15 +238,17 @@ fn type_with_options() { c: Some(Box::new(inner)), }; - let comparison = &[StartDictionary(None), - StringValue("a".to_owned()), - StringValue("hello".to_owned()), - StringValue("c".to_owned()), - StartDictionary(None), - StringValue("b".to_owned()), - IntegerValue(12), - EndDictionary, - EndDictionary]; + let comparison = &[ + StartDictionary(None), + StringValue("a".to_owned()), + StringValue("hello".to_owned()), + StringValue("c".to_owned()), + StartDictionary(None), + StringValue("b".to_owned()), + IntegerValue(12), + EndDictionary, + EndDictionary, + ]; assert_roundtrip(obj, Some(comparison)); } @@ -254,12 +268,14 @@ fn type_with_date() { b: Some(date.clone()), }; - let comparison = &[StartDictionary(None), - StringValue("a".to_owned()), - IntegerValue(28), - StringValue("b".to_owned()), - DateValue(date), - EndDictionary]; + let comparison = &[ + StartDictionary(None), + StringValue("a".to_owned()), + IntegerValue(28), + StringValue("b".to_owned()), + DateValue(date), + EndDictionary, + ]; assert_roundtrip(obj, Some(comparison)); } |
