Skip to content

Commit

Permalink
Fix issues reported by rustfmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgruenhage committed Nov 16, 2022
1 parent 64c550d commit 254d6df
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 84 deletions.
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
merge_imports = true
imports_granularity="Crate"
10 changes: 5 additions & 5 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Date {
}

/// Converts the `Date` to an XML plist date string.
pub fn to_xml_format(&self) -> String {
pub fn to_xml_format(self) -> String {
let datetime: OffsetDateTime = self.inner.into();
datetime.format(&Rfc3339).unwrap()
}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Date {
Ok(Date { inner })
}

pub(crate) fn to_seconds_since_plist_epoch(&self) -> f64 {
pub(crate) fn to_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;
Expand All @@ -95,9 +95,9 @@ impl From<SystemTime> for Date {
}
}

impl Into<SystemTime> for Date {
fn into(self) -> SystemTime {
self.inner
impl From<Date> for SystemTime {
fn from(date: Date) -> Self {
date.inner
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<usize> {
Expand Down
26 changes: 1 addition & 25 deletions src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::{
use crate::Value;

/// Represents a plist dictionary type.
#[derive(Default, Clone, PartialEq)]
pub struct Dictionary {
map: IndexMap<String, Value>,
}
Expand Down Expand Up @@ -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.
///
/// ```
Expand Down
8 changes: 2 additions & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,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
Expand Down Expand Up @@ -220,6 +216,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()
}
18 changes: 9 additions & 9 deletions src/serde_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 = &[
Expand All @@ -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,
Expand Down Expand Up @@ -346,7 +346,7 @@ fn type_with_date() {

let obj = TypeWithDate {
a: Some(28),
b: Some(date.clone()),
b: Some(date),
};

let comparison = &[
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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!(
Expand Down
10 changes: 5 additions & 5 deletions src/stream/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<R: Read + Seek> BinaryReader<R> {
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> {
Expand Down Expand Up @@ -409,7 +409,7 @@ impl<R: Read + Seek> Iterator for BinaryReader<R> {

#[cfg(test)]
mod tests {
use std::{fs::File, path::Path};
use std::fs::File;

use super::*;
use crate::{stream::Event, Uid};
Expand All @@ -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<Event> = streaming_parser.map(|e| e.unwrap()).collect();

Expand Down Expand Up @@ -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<Event> = streaming_parser.map(|e| e.unwrap()).collect();

Expand All @@ -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<Event> = streaming_parser.map(|e| e.unwrap()).collect();

Expand Down
20 changes: 9 additions & 11 deletions src/stream/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use indexmap::IndexMap;
use std::{
borrow::Cow,
io::{self, Write},
mem,
num::NonZeroUsize,
};

Expand Down Expand Up @@ -273,7 +272,7 @@ impl<W: Write> BinaryWriter<W> {
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];
Expand Down Expand Up @@ -358,7 +357,7 @@ impl<W: Write> BinaryWriter<W> {
events: &mut [Event],
ref_size: u8,
next_object_ref: &mut ObjectRef,
offset_table: &mut Vec<usize>,
offset_table: &mut [usize],
) -> Result<(), Error> {
if let Some(object_ref) = &collection.object_ref {
offset_table[object_ref.value()] = self.writer.pos;
Expand Down Expand Up @@ -437,7 +436,7 @@ impl<W: Write> BinaryWriter<W> {
fn write_plist_value(
&mut self,
value_index: usize,
offset_table: &mut Vec<usize>,
offset_table: &mut [usize],
) -> Result<(), Error> {
let (value, value_state) = value_mut(&mut self.values, value_index);

Expand Down Expand Up @@ -527,7 +526,7 @@ impl<W: Write> BinaryWriter<W> {
self.writer.write_exact(&buf)?;
} else {
let mut buf: [_; 9] = [0x87, 0, 0, 0, 0, 0, 0, 0, 0];
buf[1..].copy_from_slice(&(v as u64).to_be_bytes());
buf[1..].copy_from_slice(&v.to_be_bytes());
self.writer.write_exact(&buf)?;
}
}
Expand Down Expand Up @@ -701,7 +700,7 @@ mod tests {

use crate::{stream::BinaryReader, Value};

fn test_roundtrip(path: &Path) {
fn test_roundtrip<P: AsRef<Path>>(path: P) {
let reader = File::open(path).unwrap();
let streaming_parser = BinaryReader::new(reader);
let value_to_encode = Value::from_events(streaming_parser).unwrap();
Expand All @@ -713,24 +712,23 @@ mod tests {

let streaming_parser = BinaryReader::new(Cursor::new(buf_inner));

let events: Vec<Result<_, _>> = streaming_parser.collect();
let value_decoded_from_encode = Value::from_events(events.into_iter()).unwrap();
let value_decoded_from_encode = Value::from_events(streaming_parser).unwrap();

assert_eq!(value_to_encode, value_decoded_from_encode);
}

#[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")
}
}
2 changes: 1 addition & 1 deletion src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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),
Expand Down
12 changes: 6 additions & 6 deletions src/stream/xml_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl From<&[u8]> for ElmName {

impl AsRef<[u8]> for ElmName {
fn as_ref(&self) -> &[u8] {
&*self.0
&self.0
}
}

Expand Down Expand Up @@ -129,7 +129,7 @@ impl<R: Read> ReaderState<R> {

match closed_name.as_ref() {
b"array" | b"dict" => return Ok(Some(Event::EndCollection)),
b"plist" | _ => {}
_ => {}
}
}

Expand Down Expand Up @@ -214,7 +214,7 @@ impl<R: Read> ReaderState<R> {

match name.local_name() {
b"array" | b"dict" => return Ok(Some(Event::EndCollection)),
b"plist" | _ => (),
_ => (),
}
}
XmlEvent::Eof if self.element_stack.is_empty() => return Ok(None),
Expand Down Expand Up @@ -257,14 +257,14 @@ impl<R: Read> Iterator for XmlReader<R> {

#[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<Event> = streaming_parser.map(|e| e.unwrap()).collect();

Expand Down Expand Up @@ -305,7 +305,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();

Expand Down
6 changes: 3 additions & 3 deletions src/stream/xml_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<W: Write> Writer for XmlWriter<W> {
fn write_data(&mut self, value: &[u8]) -> Result<(), Error> {
self.handle_pending_collection()?;
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)
})
}
Expand Down Expand Up @@ -264,10 +264,10 @@ impl<W: Write> Writer for XmlWriter<W> {
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(())
Expand Down
Loading

0 comments on commit 254d6df

Please sign in to comment.