Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Nov 29, 2024
1 parent 0eb2932 commit 7445924
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
/Cargo.lock
19 changes: 9 additions & 10 deletions src/isobmff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct Location {
impl<R> Parser<R> where R: BufRead + Seek {
fn new(reader: R) -> Self {
Self {
reader: reader,
reader,
ftyp_checked: false,
item_id: None,
item_location: None,
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<R> Parser<R> where R: BufRead + Seek {
let mut buf = [0; 8];
self.reader.read_exact(&mut buf)?;
let size = match BigEndian::loadu32(&buf, 0) {
0 => Some(std::u64::MAX),
0 => Some(u64::MAX),
1 => read64(&mut self.reader)?.checked_sub(16),
x => u64::from(x).checked_sub(8),
}.ok_or("Invalid box size")?;
Expand All @@ -138,7 +138,7 @@ impl<R> Parser<R> where R: BufRead + Seek {
fn read_file_level_box(&mut self, size: u64) -> Result<Vec<u8>, Error> {
let mut buf;
match size {
std::u64::MAX => {
u64::MAX => {
buf = Vec::new();
self.reader.read_to_end(&mut buf)?;
},
Expand All @@ -154,7 +154,7 @@ impl<R> Parser<R> where R: BufRead + Seek {

fn skip_file_level_box(&mut self, size: u64) -> Result<(), Error> {
match size {
std::u64::MAX => self.reader.seek(SeekFrom::End(0))?,
u64::MAX => self.reader.seek(SeekFrom::End(0))?,
_ => self.reader.seek(SeekFrom::Current(
size.try_into().or(Err("Large seek not supported"))?))?,
};
Expand All @@ -164,7 +164,7 @@ impl<R> Parser<R> where R: BufRead + Seek {
fn parse_ftyp(&mut self, mut boxp: BoxSplitter) -> Result<(), Error> {
let head = boxp.slice(8)?;
let _major_brand = &head[0..4];
let _minor_version = BigEndian::loadu32(&head, 4);
let _minor_version = BigEndian::loadu32(head, 4);
while let Ok(compat_brand) = boxp.array4() {
if HEIF_BRANDS.contains(&compat_brand) {
return Ok(());
Expand Down Expand Up @@ -301,9 +301,8 @@ impl<R> Parser<R> where R: BufRead + Seek {
};
for _ in 0..entry_count {
let (boxtype, body) = boxp.child_box()?;
match boxtype {
b"infe" => self.parse_infe(body)?,
_ => {},
if boxtype == b"infe" {
self.parse_infe(body)?
}
}
Ok(())
Expand Down Expand Up @@ -483,7 +482,7 @@ mod tests {
// to the end of the file
let mut p = Parser::new(Cursor::new(b"\0\0\0\0abcd"));
assert_eq!(p.read_box_header().unwrap(),
Some((std::u64::MAX, *b"abcd")));
Some((u64::MAX, *b"abcd")));
// largesize
let mut p = Parser::new(Cursor::new(
b"\0\0\0\x01abcd\0\0\0\0\0\0\0\x10"));
Expand All @@ -498,7 +497,7 @@ mod tests {
let mut p = Parser::new(Cursor::new(
b"\0\0\0\x01abcd\xff\xff\xff\xff\xff\xff\xff\xff"));
assert_eq!(p.read_box_header().unwrap(),
Some((std::u64::MAX.wrapping_sub(16), *b"abcd")));
Some((u64::MAX.wrapping_sub(16), *b"abcd")));
}

#[test]
Expand Down
10 changes: 8 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub struct Reader {
continue_on_error: bool,
}

impl Default for Reader {
fn default() -> Self {
Self::new()
}
}

impl Reader {
/// Constructs a new `Reader`.
pub fn new() -> Self {
Expand Down Expand Up @@ -106,14 +112,14 @@ impl Reader {
/// If an error occurred, `exif::Error` is returned.
pub fn read_raw(&self, data: Vec<u8>) -> Result<Exif, Error> {
let mut parser = tiff::Parser::new();
parser.continue_on_error = self.continue_on_error.then(|| Vec::new());
parser.continue_on_error = self.continue_on_error.then(Vec::new);
parser.parse(&data)?;
let entry_map = parser.entries.iter().enumerate()
.map(|(i, e)| (e.ifd_num_tag(), i)).collect();
let exif = Exif {
buf: data,
entries: parser.entries,
entry_map: entry_map,
entry_map,
little_endian: parser.little_endian,
};
match parser.continue_on_error {
Expand Down
12 changes: 6 additions & 6 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,10 @@ generate_well_known_tag_constants!(
);

// For Value::display_as().
pub fn display_value_as<'a>(value: &'a Value, tag: Tag) -> value::Display<'a> {
pub fn display_value_as(value: &Value, tag: Tag) -> value::Display<'_> {
match get_tag_info(tag) {
Some(ti) => value::Display { fmt: ti.dispval, value: value },
None => value::Display { fmt: d_default, value: value },
Some(ti) => value::Display { fmt: ti.dispval, value },
None => value::Display { fmt: d_default, value },
}
}

Expand Down Expand Up @@ -1048,13 +1048,13 @@ fn d_subjarea(w: &mut dyn fmt::Write, value: &Value) -> fmt::Result {
// Acceleration (Exif 0x9404), CameraElevationAngle (Exif 0x9405)
fn d_optdecimal(w: &mut dyn fmt::Write, value: &Value) -> fmt::Result {
match *value {
Value::Rational(ref v) if v.len() > 0 =>
Value::Rational(ref v) if !v.is_empty() =>
if v[0].denom != 0xffffffff {
write!(w, "{}", v[0].to_f64())
} else {
w.write_str("unknown")
},
Value::SRational(ref v) if v.len() > 0 =>
Value::SRational(ref v) if !v.is_empty() =>
if v[0].denom != -1 {
write!(w, "{}", v[0].to_f64())
} else {
Expand Down Expand Up @@ -1463,7 +1463,7 @@ where I: IntoIterator<Item = T>, T: fmt::Display {

struct AsciiDisplay<'a>(&'a [u8]);

impl<'a> fmt::Display for AsciiDisplay<'a> {
impl fmt::Display for AsciiDisplay<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
d_sub_ascii(f, self.0)
}
Expand Down
15 changes: 9 additions & 6 deletions src/tiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl Parser {
}
let entry = Self::parse_ifd_entry::<E>(data, offset);
offset += 12;
let (tag, val) = match entry {
let (tag, value) = match entry {
Ok(x) => x,
Err(e) => {
self.check_error(e)?;
Expand All @@ -256,11 +256,11 @@ impl Parser {
Tag::InteropIFDPointer => Context::Interop,
_ => {
self.entries.push(IfdEntry { field: Field {
tag: tag, ifd_num: In(ifd_num), value: val }.into()});
tag, ifd_num: In(ifd_num), value }.into()});
continue;
},
};
self.parse_child_ifd::<E>(data, val, child_ctx, ifd_num)
self.parse_child_ifd::<E>(data, value, child_ctx, ifd_num)
.or_else(|e| self.check_error(e))?;
}

Expand Down Expand Up @@ -313,7 +313,10 @@ impl Parser {

fn check_error(&mut self, err: Error) -> Result<(), Error> {
match self.continue_on_error {
Some(ref mut v) => Ok(v.push(err)),
Some(ref mut v) => {
v.push(err);
Ok(())
},
None => Err(err),
}
}
Expand Down Expand Up @@ -502,12 +505,12 @@ impl<'a> DisplayValue<'a> {
ifd_num: self.ifd_num,
value_display: self.value_display,
unit: self.tag.unit(),
unit_provider: unit_provider,
unit_provider,
}
}
}

impl<'a> fmt::Display for DisplayValue<'a> {
impl fmt::Display for DisplayValue<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value_display.fmt(f)
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T> BufReadExt for T where T: io::BufRead {
fn discard_exact(&mut self, mut len: usize) -> io::Result<()> {
while len > 0 {
let consume_len = match self.fill_buf() {
Ok(buf) if buf.is_empty() =>
Ok([]) =>
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof, "unexpected EOF")),
Ok(buf) => buf.len().min(len),
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<T> ReadExt for T where T: io::Read {
// This function must not be called with more than 4 bytes.
pub fn atou16(bytes: &[u8]) -> Result<u16, Error> {
debug_assert!(bytes.len() <= 4);
if bytes.len() == 0 {
if bytes.is_empty() {
return Err(Error::InvalidFormat("Not a number"));
}
let mut n = 0;
Expand Down
18 changes: 9 additions & 9 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Value {
Value::Short(ref v) =>
Some(UIntIter { iter: Box::new(v.iter().map(|&x| x as u32)) }),
Value::Long(ref v) =>
Some(UIntIter { iter: Box::new(v.iter().map(|&x| x)) }),
Some(UIntIter { iter: Box::new(v.iter().copied()) }),
_ => None,
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ impl UIntValue {
match self.0 {
Value::Byte(ref v) => v.get(index).map(|&x| x.into()),
Value::Short(ref v) => v.get(index).map(|&x| x.into()),
Value::Long(ref v) => v.get(index).map(|&x| x),
Value::Long(ref v) => v.get(index).copied(),
_ => panic!(),
}
}
Expand All @@ -224,7 +224,7 @@ pub struct UIntIter<'a> {
iter: Box<dyn ExactSizeIterator<Item=u32> + 'a>
}

impl<'a> Iterator for UIntIter<'a> {
impl Iterator for UIntIter<'_> {
type Item = u32;

#[inline]
Expand All @@ -238,7 +238,7 @@ impl<'a> Iterator for UIntIter<'a> {
}
}

impl<'a> ExactSizeIterator for UIntIter<'a> {}
impl ExactSizeIterator for UIntIter<'_> {}

/// Helper struct for printing a value in a tag-specific format.
#[derive(Copy, Clone)]
Expand All @@ -247,7 +247,7 @@ pub struct Display<'a> {
pub value: &'a Value,
}

impl<'a> fmt::Display for Display<'a> {
impl fmt::Display for Display<'_> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self.fmt)(f, self.value)
Expand Down Expand Up @@ -292,7 +292,7 @@ where F: Fn() -> T, T: Iterator<Item = I>, I: fmt::Debug {

struct AsciiDebugAdapter<'a>(&'a [u8]);

impl<'a> fmt::Debug for AsciiDebugAdapter<'a> {
impl fmt::Debug for AsciiDebugAdapter<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_char('"')?;
self.0.iter().try_for_each(|&c| match c {
Expand All @@ -306,7 +306,7 @@ impl<'a> fmt::Debug for AsciiDebugAdapter<'a> {

struct HexDebugAdapter<'a>(&'a [u8]);

impl<'a> fmt::Debug for HexDebugAdapter<'a> {
impl fmt::Debug for HexDebugAdapter<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("0x")?;
self.0.iter().try_for_each(|x| write!(f, "{:02x}", x))
Expand Down Expand Up @@ -471,9 +471,9 @@ fn parse_byte(data: &[u8], offset: usize, count: usize) -> Value {
fn parse_ascii(data: &[u8], offset: usize, count: usize) -> Value {
// Any ASCII field can contain multiple strings [TIFF6 Image File
// Directory].
let iter = (&data[offset .. offset + count]).split(|&b| b == b'\0');
let iter = (data[offset .. offset + count]).split(|&b| b == b'\0');
let mut v: Vec<Vec<u8>> = iter.map(|x| x.to_vec()).collect();
if v.last().map_or(false, |x| x.len() == 0) {
if v.last().map_or(false, |x| x.is_empty()) {
v.pop();
}
Value::Ascii(v)
Expand Down
Loading

0 comments on commit 7445924

Please sign in to comment.