Skip to content

Commit

Permalink
fix clippy lints and rustfmt settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgruenhage committed Nov 19, 2022
1 parent 5fdedbe commit fd1fc26
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 91 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"
8 changes: 4 additions & 4 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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(val: Date) -> Self {
val.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(Clone, Default, 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 @@ -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
Expand Down Expand Up @@ -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()
}
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
19 changes: 10 additions & 9 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 All @@ -464,7 +463,7 @@ impl<W: Write> BinaryWriter<W> {
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)?;
Expand Down Expand Up @@ -527,6 +526,8 @@ impl<W: Write> BinaryWriter<W> {
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)?;
}
Expand Down Expand Up @@ -701,7 +702,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 @@ -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")
}
}
4 changes: 2 additions & 2 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<'static, str>>) -> Self {
let indent_str = indent_str.into();
let indent_str = indent_str.as_ref();
Expand Down Expand Up @@ -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),
Expand Down
18 changes: 8 additions & 10 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 @@ -172,12 +172,10 @@ impl<R: Read> ReaderState<R> {
_ => 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))
Expand All @@ -197,14 +195,14 @@ impl<R: Read> ReaderState<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 @@ -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();

Expand Down
Loading

0 comments on commit fd1fc26

Please sign in to comment.