diff --git a/rustfmt.toml b/rustfmt.toml index 7d2cf54..e86028b 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1 @@ -merge_imports = true +imports_granularity="Crate" diff --git a/src/date.rs b/src/date.rs index af32833..9b6402f 100644 --- a/src/date.rs +++ b/src/date.rs @@ -68,7 +68,7 @@ impl Date { Ok(Date { inner }) } - pub(crate) fn to_seconds_since_plist_epoch(&self) -> f64 { + pub(crate) fn as_seconds_since_plist_epoch(&self) -> f64 { // needed until #![feature(duration_float)] is stabilized fn as_secs_f64(d: Duration) -> f64 { const NANOS_PER_SEC: f64 = 1_000_000_000.00; @@ -95,9 +95,9 @@ impl From for Date { } } -impl Into for Date { - fn into(self) -> SystemTime { - self.inner +impl From for SystemTime { + fn from(val: Date) -> Self { + val.inner } } diff --git a/src/de.rs b/src/de.rs index 272defe..420a19b 100644 --- a/src/de.rs +++ b/src/de.rs @@ -183,7 +183,7 @@ where } OptionMode::StructField => { // None struct values are ignored so if we're here the value must be Some. - self.with_option_mode(OptionMode::Explicit, |this| Ok(visitor.visit_some(this)?)) + self.with_option_mode(OptionMode::Explicit, |this| visitor.visit_some(this)) } OptionMode::Explicit => { expect!(self.events.next(), EventKind::StartDictionary); @@ -391,7 +391,7 @@ where OptionMode::Explicit }; self.de - .with_option_mode(option_mode, |this| Ok(seed.deserialize(this)?)) + .with_option_mode(option_mode, |this| seed.deserialize(this)) } fn size_hint(&self) -> Option { diff --git a/src/dictionary.rs b/src/dictionary.rs index 696b7ce..d3d43a2 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -14,6 +14,7 @@ use std::{ use crate::Value; /// Represents a plist dictionary type. +#[derive(Clone, Default, PartialEq)] pub struct Dictionary { map: IndexMap, } @@ -160,31 +161,6 @@ impl Dictionary { } } -impl Default for Dictionary { - #[inline] - fn default() -> Self { - Dictionary { - map: Default::default(), - } - } -} - -impl Clone for Dictionary { - #[inline] - fn clone(&self) -> Self { - Dictionary { - map: self.map.clone(), - } - } -} - -impl PartialEq for Dictionary { - #[inline] - fn eq(&self, other: &Self) -> bool { - self.map.eq(&other.map) - } -} - /// Access an element of this dictionary. Panics if the given key is not present in the dictionary. /// /// ``` diff --git a/src/error.rs b/src/error.rs index 4f9599d..44b3c3d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -88,11 +88,7 @@ impl Error { /// Returns true if this error was caused by prematurely reaching the end of the input data. pub fn is_eof(&self) -> bool { - if let ErrorKind::UnexpectedEof = self.inner.kind { - true - } else { - false - } + matches!(self.inner.kind, ErrorKind::UnexpectedEof) } /// Returns the underlying error if it was caused by a failure to read or write bytes on an IO @@ -219,6 +215,6 @@ pub(crate) fn from_io_without_position(err: io::Error) -> Error { } pub(crate) fn unexpected_event_type(expected: EventKind, found: &Event) -> Error { - let found = EventKind::of_event(&found); + let found = EventKind::of_event(found); ErrorKind::UnexpectedEventType { expected, found }.without_position() } diff --git a/src/serde_tests.rs b/src/serde_tests.rs index 0a799de..9a5b5d8 100644 --- a/src/serde_tests.rs +++ b/src/serde_tests.rs @@ -2,7 +2,7 @@ use serde::{ de::{Deserialize, DeserializeOwned}, ser::Serialize, }; -use std::{collections::BTreeMap, fmt::Debug, fs::File, io::Cursor, path::Path}; +use std::{collections::BTreeMap, fmt::Debug, fs::File, io::Cursor}; use crate::{ stream::{private::Sealed, Event, OwnedEvent, Writer}, @@ -183,7 +183,7 @@ fn dog() { fn frog() { let frog = Animal::Frog( Ok("hello".to_owned()), - Some(vec![1.0, 2.0, 3.14159, 0.000000001, 1.27e31]), + Some(vec![1.0, 2.0, std::f64::consts::PI, 0.000000001, 1.27e31]), ); let comparison = &[ @@ -199,7 +199,7 @@ fn frog() { Event::StartArray(Some(5)), Event::Real(1.0), Event::Real(2.0), - Event::Real(3.14159), + Event::Real(std::f64::consts::PI), Event::Real(0.000000001), Event::Real(1.27e31), Event::EndCollection, @@ -346,7 +346,7 @@ fn type_with_date() { let obj = TypeWithDate { a: Some(28), - b: Some(date.clone()), + b: Some(date), }; let comparison = &[ @@ -586,7 +586,7 @@ fn deserialise_old_enum_unit_variant_encoding() { #[test] fn deserialize_dictionary_xml() { - let reader = File::open(&Path::new("./tests/data/xml.plist")).unwrap(); + let reader = File::open("./tests/data/xml.plist").unwrap(); let dict: Dictionary = crate::from_reader(reader).unwrap(); check_common_plist(&dict); @@ -603,7 +603,7 @@ fn deserialize_dictionary_xml() { #[test] fn deserialize_dictionary_binary() { - let reader = File::open(&Path::new("./tests/data/binary.plist")).unwrap(); + let reader = File::open("./tests/data/binary.plist").unwrap(); let dict: Dictionary = crate::from_reader(reader).unwrap(); check_common_plist(&dict); @@ -634,9 +634,9 @@ fn check_common_plist(dict: &Dictionary) { // Boolean elements - assert_eq!(dict.get("IsTrue").unwrap().as_boolean().unwrap(), true); + assert!(dict.get("IsTrue").unwrap().as_boolean().unwrap()); - assert_eq!(dict.get("IsNotFalse").unwrap().as_boolean().unwrap(), false); + assert!(!dict.get("IsNotFalse").unwrap().as_boolean().unwrap()); // Data @@ -699,7 +699,7 @@ fn check_common_plist(dict: &Dictionary) { #[test] fn deserialize_dictionary_binary_nskeyedarchiver() { - let reader = File::open(&Path::new("./tests/data/binary_NSKeyedArchiver.plist")).unwrap(); + let reader = File::open("./tests/data/binary_NSKeyedArchiver.plist").unwrap(); let dict: Dictionary = crate::from_reader(reader).unwrap(); assert_eq!( diff --git a/src/stream/binary_reader.rs b/src/stream/binary_reader.rs index ded1592..40a0d59 100644 --- a/src/stream/binary_reader.rs +++ b/src/stream/binary_reader.rs @@ -187,7 +187,7 @@ impl BinaryReader { if offset >= self.trailer_start_offset { return Err(self.with_pos(ErrorKind::ObjectOffsetTooLarge)); } - Ok(self.reader.seek(SeekFrom::Start(offset))?) + self.reader.seek(SeekFrom::Start(offset)) } fn push_stack_item_and_check_for_recursion(&mut self, item: StackItem) -> Result<(), Error> { @@ -409,7 +409,7 @@ impl Iterator for BinaryReader { #[cfg(test)] mod tests { - use std::{fs::File, path::Path}; + use std::fs::File; use super::*; use crate::{stream::Event, Uid}; @@ -418,7 +418,7 @@ mod tests { fn streaming_parser() { use crate::stream::Event::*; - let reader = File::open(&Path::new("./tests/data/binary.plist")).unwrap(); + let reader = File::open("./tests/data/binary.plist").unwrap(); let streaming_parser = BinaryReader::new(reader); let events: Vec = streaming_parser.map(|e| e.unwrap()).collect(); @@ -463,7 +463,7 @@ mod tests { #[test] fn utf16_plist() { - let reader = File::open(&Path::new("./tests/data/utf16_bplist.plist")).unwrap(); + let reader = File::open("./tests/data/utf16_bplist.plist").unwrap(); let streaming_parser = BinaryReader::new(reader); let mut events: Vec = streaming_parser.map(|e| e.unwrap()).collect(); @@ -480,7 +480,7 @@ mod tests { #[test] fn nskeyedarchiver_plist() { - let reader = File::open(&Path::new("./tests/data/binary_NSKeyedArchiver.plist")).unwrap(); + let reader = File::open("./tests/data/binary_NSKeyedArchiver.plist").unwrap(); let streaming_parser = BinaryReader::new(reader); let events: Vec = streaming_parser.map(|e| e.unwrap()).collect(); diff --git a/src/stream/binary_writer.rs b/src/stream/binary_writer.rs index 0e04dbe..620f613 100644 --- a/src/stream/binary_writer.rs +++ b/src/stream/binary_writer.rs @@ -5,7 +5,6 @@ use indexmap::IndexMap; use std::{ borrow::Cow, io::{self, Write}, - mem, num::NonZeroUsize, }; @@ -273,7 +272,7 @@ impl BinaryWriter { self.writer.write_exact(b"bplist00")?; // Write objects - let mut events_vec = mem::replace(&mut self.events, Vec::new()); + let mut events_vec = std::mem::take(&mut self.events); let mut events = &mut events_vec[..]; let ref_size = plist_ref_size(self.num_objects - 1); let mut offset_table = vec![0; self.num_objects]; @@ -358,7 +357,7 @@ impl BinaryWriter { events: &mut [Event], ref_size: u8, next_object_ref: &mut ObjectRef, - offset_table: &mut Vec, + offset_table: &mut [usize], ) -> Result<(), Error> { if let Some(object_ref) = &collection.object_ref { offset_table[object_ref.value()] = self.writer.pos; @@ -437,7 +436,7 @@ impl BinaryWriter { fn write_plist_value( &mut self, value_index: usize, - offset_table: &mut Vec, + offset_table: &mut [usize], ) -> Result<(), Error> { let (value, value_state) = value_mut(&mut self.values, value_index); @@ -464,7 +463,7 @@ impl BinaryWriter { self.writer.write_exact(&v[..])?; } Value::Date(v) => { - let secs = v.to_seconds_since_plist_epoch(); + let secs = v.as_seconds_since_plist_epoch(); let mut buf: [_; 9] = [0x33, 0, 0, 0, 0, 0, 0, 0, 0]; buf[1..].copy_from_slice(&secs.to_bits().to_be_bytes()); self.writer.write_exact(&buf)?; @@ -527,6 +526,8 @@ impl BinaryWriter { self.writer.write_exact(&buf)?; } else { let mut buf: [_; 9] = [0x87, 0, 0, 0, 0, 0, 0, 0, 0]; + // we want to be explicit about the type here + #[allow(clippy::unnecessary_cast)] buf[1..].copy_from_slice(&(v as u64).to_be_bytes()); self.writer.write_exact(&buf)?; } @@ -701,7 +702,7 @@ mod tests { use crate::{stream::BinaryReader, Value}; - fn test_roundtrip(path: &Path) { + fn test_roundtrip>(path: P) { let reader = File::open(path).unwrap(); let streaming_parser = BinaryReader::new(reader); let value_to_encode = Value::from_events(streaming_parser).unwrap(); @@ -721,16 +722,16 @@ mod tests { #[test] fn bplist_roundtrip() { - test_roundtrip(&Path::new("./tests/data/binary.plist")) + test_roundtrip("./tests/data/binary.plist") } #[test] fn utf16_roundtrip() { - test_roundtrip(&Path::new("./tests/data/utf16_bplist.plist")) + test_roundtrip("./tests/data/utf16_bplist.plist") } #[test] fn nskeyedarchiver_roundtrip() { - test_roundtrip(&Path::new("./tests/data/binary_NSKeyedArchiver.plist")) + test_roundtrip("./tests/data/binary_NSKeyedArchiver.plist") } } diff --git a/src/stream/mod.rs b/src/stream/mod.rs index fc2ca9a..e9ecace 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -101,7 +101,7 @@ impl XmlWriteOptions { /// Since replacing `xml-rs` with `quick-xml`, the indent string has to consist of a single /// repeating ascii character. This is a backwards compatibility function, prefer using /// `XmlWriteOptions::with_indent`. - #[deprecated(since="1.4.0", note="please use `with_indent` instead")] + #[deprecated(since = "1.4.0", note = "please use `with_indent` instead")] pub fn indent_string(self, indent_str: impl Into>) -> Self { let indent_str = indent_str.into(); let indent_str = indent_str.as_ref(); @@ -190,7 +190,7 @@ impl<'a> Iterator for Events<'a> { Event::StartDictionary(Some(len as u64)) } Value::Boolean(value) => Event::Boolean(*value), - Value::Data(value) => Event::Data(Cow::Borrowed(&value)), + Value::Data(value) => Event::Data(Cow::Borrowed(value)), Value::Date(value) => Event::Date(*value), Value::Real(value) => Event::Real(*value), Value::Integer(value) => Event::Integer(*value), diff --git a/src/stream/xml_reader.rs b/src/stream/xml_reader.rs index bcde35e..35036e5 100644 --- a/src/stream/xml_reader.rs +++ b/src/stream/xml_reader.rs @@ -18,7 +18,7 @@ impl From<&[u8]> for ElmName { impl AsRef<[u8]> for ElmName { fn as_ref(&self) -> &[u8] { - &*self.0 + &self.0 } } @@ -172,12 +172,10 @@ impl ReaderState { _ => return Err(self.with_pos(ErrorKind::UnknownXmlElement)), } } - XmlEvent::End(name) => { - match name.local_name() { - b"array" | b"dict" => return Ok(Some(Event::EndCollection)), - b"plist" | _ => (), - } - } + XmlEvent::End(name) => match name.local_name() { + b"array" | b"dict" => return Ok(Some(Event::EndCollection)), + _ => (), + }, XmlEvent::Eof => return Ok(None), XmlEvent::Text(_) => { return Err(self.with_pos(ErrorKind::UnexpectedXmlCharactersExpectedElement)) @@ -197,14 +195,14 @@ impl ReaderState { #[cfg(test)] mod tests { - use std::{fs::File, path::Path}; + use std::fs::File; use super::*; use crate::stream::Event::{self, *}; #[test] fn streaming_parser() { - let reader = File::open(&Path::new("./tests/data/xml.plist")).unwrap(); + let reader = File::open("./tests/data/xml.plist").unwrap(); let streaming_parser = XmlReader::new(reader); let events: Vec = streaming_parser.map(|e| e.unwrap()).collect(); @@ -245,7 +243,7 @@ mod tests { #[test] fn bad_data() { - let reader = File::open(&Path::new("./tests/data/xml_error.plist")).unwrap(); + let reader = File::open("./tests/data/xml_error.plist").unwrap(); let streaming_parser = XmlReader::new(reader); let events: Vec<_> = streaming_parser.collect(); diff --git a/src/stream/xml_writer.rs b/src/stream/xml_writer.rs index e8581d8..6d01734 100644 --- a/src/stream/xml_writer.rs +++ b/src/stream/xml_writer.rs @@ -149,21 +149,21 @@ impl XmlWriter { fn handle_pending_collection(&mut self) -> Result<(), Error> { if let Some(PendingCollection::Array) = self.pending_collection { self.pending_collection = None; - let res = self.write_value_event(EventKind::StartArray, |this| { + + self.write_value_event(EventKind::StartArray, |this| { this.start_element("array")?; this.stack.push(Element::Array); Ok(()) - }); - res + }) } else if let Some(PendingCollection::Dictionary) = self.pending_collection { self.pending_collection = None; - let res = self.write_value_event(EventKind::StartDictionary, |this| { + + self.write_value_event(EventKind::StartDictionary, |this| { this.start_element("dict")?; this.stack.push(Element::Dictionary); this.expecting_key = true; Ok(()) - }); - res + }) } else { Ok(()) } @@ -200,10 +200,7 @@ impl Writer for XmlWriter { } _ => {} }; - match ( - this.stack.pop(), - this.expecting_key, - ) { + match (this.stack.pop(), this.expecting_key) { (Some(Element::Dictionary), true) => { this.end_element("dict")?; } @@ -234,7 +231,7 @@ impl Writer for XmlWriter { fn write_data(&mut self, value: &[u8]) -> Result<(), Error> { self.write_value_event(EventKind::Data, |this| { - let base64_data = base64_encode_plist(&value, this.stack.len()); + let base64_data = base64_encode_plist(value, this.stack.len()); this.write_element_and_value("data", &base64_data) }) } @@ -261,10 +258,10 @@ impl Writer for XmlWriter { self.handle_pending_collection()?; self.write_event(|this| { if this.expecting_key { - this.write_element_and_value("key", &*value)?; + this.write_element_and_value("key", value)?; this.expecting_key = false; } else { - this.write_element_and_value("string", &*value)?; + this.write_element_and_value("string", value)?; this.expecting_key = this.stack.last() == Some(&Element::Dictionary); } Ok(()) diff --git a/src/value.rs b/src/value.rs index 607479e..ae2d85b 100644 --- a/src/value.rs +++ b/src/value.rs @@ -796,11 +796,10 @@ mod tests { let plist = builder.build(); // Expected output - let mut lines = Vec::new(); - lines.push(Value::String("It is a tale told by an idiot,".to_owned())); - lines.push(Value::String( - "Full of sound and fury, signifying nothing.".to_owned(), - )); + let lines = vec![ + Value::String("It is a tale told by an idiot,".to_owned()), + Value::String("Full of sound and fury, signifying nothing.".to_owned()), + ]; let mut dict = Dictionary::new(); dict.insert(