Skip to content

Commit

Permalink
Check with the latest clippy and fix almost all warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mindeng committed Jul 12, 2024
1 parent cfe8a02 commit 4b9e7f5
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/bbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub struct FullBoxHeader {
}

impl FullBoxHeader {
fn parse<'a>(input: &'a [u8]) -> IResult<&'a [u8], FullBoxHeader> {
fn parse(input: &[u8]) -> IResult<&[u8], FullBoxHeader> {
let (remain, header) = BoxHeader::parse(input)?;

let (remain, version) = number::streaming::u8(remain)?;
Expand Down Expand Up @@ -311,7 +311,7 @@ pub fn get_ftyp(input: &[u8]) -> crate::Result<Option<&[u8]>> {
)
.into());
}
let (_, ftyp) = complete::take(4 as usize)(bbox.body_data())?;
let (_, ftyp) = complete::take(4_usize)(bbox.body_data())?;
Ok(Some(ftyp))
} else if bbox.box_type() == "wide" {
// MOV files that extracted from HEIC starts with `wide` & `mdat` atoms
Expand Down
11 changes: 4 additions & 7 deletions src/bbox/iinf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct IinfBox {
}

impl ParseBody<IinfBox> for IinfBox {
fn parse_body<'a>(remain: &'a [u8], header: FullBoxHeader) -> IResult<&'a [u8], IinfBox> {
fn parse_body(remain: &[u8], header: FullBoxHeader) -> IResult<&[u8], IinfBox> {
let version = header.version;

let (remain, item_count) = if version > 0 {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl ParseBody<InfeBox> for InfeBox {

let (remain, item_type) = cond(
version >= 2,
map_res(streaming::take(4 as usize), |res: &'a [u8]| {
map_res(streaming::take(4_usize), |res: &'a [u8]| {
String::from_utf8(res.to_vec())
}),
)(remain)?;
Expand All @@ -95,7 +95,7 @@ impl ParseBody<InfeBox> for InfeBox {
let (remain, content_type, content_encoding) =
if version <= 1 || (version >= 2 && item_type.as_ref().unwrap() == "mime") {
let (remain, content_type) = parse_cstr(remain)?;
let (remain, content_encoding) = cond(remain.len() > 0, parse_cstr)(remain)?;
let (remain, content_encoding) = cond(!remain.is_empty(), parse_cstr)(remain)?;
(remain, Some(content_type), content_encoding)
} else {
(remain, None, None)
Expand Down Expand Up @@ -126,9 +126,6 @@ impl ParseBody<InfeBox> for InfeBox {

impl InfeBox {
fn key(&self) -> &String {
self.item_type
.as_ref()
.or_else(|| Some(&self.item_name))
.unwrap()
self.item_type.as_ref().unwrap_or(&self.item_name)
}
}
2 changes: 1 addition & 1 deletion src/bbox/iloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct IlocBox {
const MAX_ILOC_EXTENTS_PER_ITEM: u16 = 32;

impl ParseBody<IlocBox> for IlocBox {
fn parse_body<'a>(remain: &'a [u8], header: FullBoxHeader) -> IResult<&'a [u8], IlocBox> {
fn parse_body(remain: &[u8], header: FullBoxHeader) -> IResult<&[u8], IlocBox> {
let version = header.version;

let (remain, (offset_size, length_size)) =
Expand Down
2 changes: 1 addition & 1 deletion src/bbox/ilst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct IlstBox {
}

impl IlstBox {
pub fn parse_box<'a>(input: &'a [u8]) -> nom::IResult<&'a [u8], IlstBox> {
pub fn parse_box(input: &[u8]) -> nom::IResult<&[u8], IlstBox> {
let (remain, header) = BoxHeader::parse(input)?;
let (remain, items) = many0(IlstItem::parse)(remain)?;

Expand Down
2 changes: 1 addition & 1 deletion src/bbox/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct KeysBox {
}

impl ParseBody<KeysBox> for KeysBox {
fn parse_body<'a>(body: &'a [u8], header: FullBoxHeader) -> nom::IResult<&'a [u8], KeysBox> {
fn parse_body(body: &[u8], header: FullBoxHeader) -> nom::IResult<&[u8], KeysBox> {
let (remain, entry_count) = be_u32(body)?;
let (remain, entries) =
many_m_n(entry_count as usize, entry_count as usize, KeyEntry::parse)(remain)?;
Expand Down
14 changes: 7 additions & 7 deletions src/bbox/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ impl ParseBody<MetaBox> for MetaBox {
// parse iinf box
let iinf = boxes
.get("iinf")
.and_then(|iinf| Some(IinfBox::parse_box(iinf.data)))
.map(|iinf| IinfBox::parse_box(iinf.data))
.transpose()?
.map(|x| x.1);

// parse iloc box
let iloc = boxes
.get("iloc")
.and_then(|iloc| Some(IlocBox::parse_box(iloc.data)))
.map(|iloc| IlocBox::parse_box(iloc.data))
.transpose()?
.map(|x| x.1);

Expand Down Expand Up @@ -79,23 +79,23 @@ impl MetaBox {
.as_ref()
.and_then(|iloc| iloc.item_offset_len(exif_infe.id))
})
.and_then(|(construction_method, offset, length)| {
.map(|(construction_method, offset, length)| {
let start = offset as usize;
let end = (offset + length) as usize;
if construction_method == 0 {
// file offset
if end > input.len() {
Some(Err(nom::Err::Incomplete(Needed::new(end - input.len()))))
Err(nom::Err::Incomplete(Needed::new(end - input.len())))
} else {
Some(Ok((&input[end..], Some(&input[start..end])))) // Safe-slice
Ok((&input[end..], Some(&input[start..end]))) // Safe-slice
}
} else if construction_method == 1 {
// idat offset
eprintln!("idat offset construction method is not supported yet");
Some(fail(input))
fail(input)
} else {
eprintln!("item offset construction method is not supported yet");
Some(fail(input))
fail(input)
}
})
.unwrap_or(Ok((input, None)))
Expand Down
4 changes: 2 additions & 2 deletions src/bbox/tkhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct TkhdBox {
}

impl ParseBody<TkhdBox> for TkhdBox {
fn parse_body<'a>(body: &'a [u8], header: FullBoxHeader) -> nom::IResult<&'a [u8], TkhdBox> {
fn parse_body(body: &[u8], header: FullBoxHeader) -> nom::IResult<&[u8], TkhdBox> {
let (
remain,
(
Expand Down Expand Up @@ -95,7 +95,7 @@ impl ParseBody<TkhdBox> for TkhdBox {
}

/// Try to find a video track's tkhd in moov body. atom-path: "moov/trak/tkhd".
pub fn parse_video_tkhd_in_moov<'a>(input: &'a [u8]) -> crate::Result<TkhdBox> {
pub fn parse_video_tkhd_in_moov(input: &[u8]) -> crate::Result<TkhdBox> {
let bbox = find_video_track(input)?;
let (_, bbox) = travel_while(bbox.body_data(), |b| b.box_type() != "tkhd")
.map_err(|e| format!("find tkhd failed: {e:?}"))?;
Expand Down
12 changes: 1 addition & 11 deletions src/exif/gps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ pub struct GPSInfo {
pub altitude: URational,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct LatLng(pub URational, pub URational, pub URational);

impl Default for LatLng {
fn default() -> Self {
LatLng(
URational::default(),
URational::default(),
URational::default(),
)
}
}

impl GPSInfo {
/// Returns an ISO 6709 geographic point location string such as
/// `+48.8577+002.295/`.
Expand Down
8 changes: 2 additions & 6 deletions src/exif/ifd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl ImageFileDirectory {
pub fn find(&self, tag: u16) -> Option<&DirectoryEntry> {
self.entries
.get(&tag)
.and_then(|entry| Some(entry))
.or_else(|| self.exif_ifd().and_then(|exif_ifd| exif_ifd.find(tag)))
.or_else(|| self.gps_ifd().and_then(|gps_ifd| gps_ifd.find(tag)))
}
Expand Down Expand Up @@ -226,10 +225,7 @@ fn get_cstr(data: &[u8]) -> std::result::Result<String, FromUtf8Error> {
)
}

pub fn get_gps_info<'a>(
gps_ifd: &ImageFileDirectory,
endian: Endianness,
) -> crate::Result<GPSInfo> {
pub fn get_gps_info(gps_ifd: &ImageFileDirectory, endian: Endianness) -> crate::Result<GPSInfo> {
fn get_ref(gps_ifd: &ImageFileDirectory, tag: ExifTag) -> crate::Result<char> {
gps_ifd
.find(tag as u16)
Expand Down Expand Up @@ -257,7 +253,7 @@ pub fn get_gps_info<'a>(

let altitude_ref = gps_ifd
.find(ExifTag::GPSAltitudeRef as u16)
.and_then(|entry| Some(entry.data[0]))
.map(|entry| entry.data[0])
.unwrap_or(0);

let altitude = if let Some(entry) = gps_ifd.find(ExifTag::GPSAltitude as u16) {
Expand Down
2 changes: 1 addition & 1 deletion src/exif/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Exif {
.as_ref()
.and_then(|ifd0| {
ifd0.gps_ifd()
.and_then(|gps_ifd| Some(get_gps_info(gps_ifd, self.endian())))
.map(|gps_ifd| get_gps_info(gps_ifd, self.endian()))
})
.transpose()
}
Expand Down
8 changes: 4 additions & 4 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt::Display;
#[allow(unused)]
#[derive(Debug, PartialEq, Eq)]
pub enum FileType {
JPEG,
HEIF,
Jpeg,
Heif,
QuickTime,
MP4,
}
Expand All @@ -14,8 +14,8 @@ use FileType::*;
impl Display for FileType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JPEG => "JPEG".fmt(f),
HEIF => "HEIF/HEIC".fmt(f),
Jpeg => "JPEG".fmt(f),
Heif => "HEIF/HEIC".fmt(f),
QuickTime => "QuickTime".fmt(f),
MP4 => "MP4".fmt(f),
}
Expand Down
6 changes: 2 additions & 4 deletions src/heif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn parse_heif_exif<R: Read + Seek>(mut reader: R) -> crate::Result<Option<Ex

let Some(ftyp) = get_ftyp(&buf).map_err(|e| format!("unsupported HEIF/HEIC file; {}", e))?
else {
return Err(format!("unsupported HEIF/HEIC file; ftyp not found").into());
return Err("unsupported HEIF/HEIC file; ftyp not found".into());
};
if !HEIF_FTYPS.contains(&ftyp) {
Err(format!("unsupported HEIF/HEIC file; ftyp: {ftyp:?}"))?;
Expand Down Expand Up @@ -95,9 +95,7 @@ pub fn parse_heif_exif<R: Read + Seek>(mut reader: R) -> crate::Result<Option<Ex
}
};

exif_data
.and_then(|exif_data| Some(parse_exif(exif_data)))
.transpose()
exif_data.map(parse_exif).transpose()
}

const HEIF_FTYPS: [&[u8]; 7] = [
Expand Down
26 changes: 13 additions & 13 deletions src/jpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ impl<'a> Segment<'a> {
}
}

fn find_exif_segment<'a>(input: &'a [u8]) -> IResult<&'a [u8], Option<Segment<'a>>> {
fn find_exif_segment(input: &[u8]) -> IResult<&[u8], Option<Segment<'_>>> {
let (remain, segment) = travel_until(input, |s| {
(s.marker_code == MarkerCode::APP1.code() && check_exif_header(s.payload))
|| s.marker_code == MarkerCode::SOS.code() // searching stop at SOS
|| s.marker_code == MarkerCode::Sos.code() // searching stop at SOS
})?;

if segment.marker_code != MarkerCode::SOS.code() {
if segment.marker_code != MarkerCode::Sos.code() {
Ok((remain, Some(segment)))
} else {
Ok((remain, None))
Expand Down Expand Up @@ -147,18 +147,18 @@ pub fn check_jpeg(input: &[u8]) -> crate::Result<()> {
let (_, (_, code)) = tuple((streaming::tag([0xFF]), number::complete::u8))(input)?;

// SOI has no payload
if code != MarkerCode::SOI.code() {
if code != MarkerCode::Soi.code() {
Err("invalid JPEG file; SOI marker not found".into())
} else {
Ok(())
}
}

fn parse_segment<'a>(marker_code: u8, input: &'a [u8]) -> IResult<&'a [u8], Segment<'a>> {
fn parse_segment(marker_code: u8, input: &[u8]) -> IResult<&[u8], Segment<'_>> {
let remain = input;

// SOI has no payload
if marker_code == MarkerCode::SOI.code() {
if marker_code == MarkerCode::Soi.code() {
Ok((
remain,
Segment {
Expand Down Expand Up @@ -197,15 +197,15 @@ fn read_image_data<T: Read + Seek>(mut reader: T) -> crate::Result<Vec<u8>> {
return Err("".into());
}

if marker == MarkerCode::SOI.code() {
if marker == MarkerCode::Soi.code() {
// SOI has no body
continue;
}
if marker == MarkerCode::EOI.code() {
if marker == MarkerCode::Eoi.code() {
return Err(crate::Error::NotFound);
}

if marker == MarkerCode::SOS.code() {
if marker == MarkerCode::Sos.code() {
// found it
let mut data = Vec::new();
reader.read_to_end(&mut data)?;
Expand All @@ -216,7 +216,7 @@ fn read_image_data<T: Read + Seek>(mut reader: T) -> crate::Result<Vec<u8>> {
// empty
break;
};
if tail == MarkerCode::EOI.code() {
if tail == MarkerCode::Eoi.code() {
if let Some(tail) = data.pop() {
if tail == 0xFF {
// EOI marker has been popped
Expand All @@ -238,16 +238,16 @@ fn read_image_data<T: Read + Seek>(mut reader: T) -> crate::Result<Vec<u8>> {
/// A marker code is a byte following 0xFF that indicates the kind of marker.
enum MarkerCode {
// Start of Image
SOI = 0xD8,
Soi = 0xD8,

// APP1 marker
APP1 = 0xE1,

// Start of Scan
SOS = 0xDA,
Sos = 0xDA,

// End of Image
EOI = 0xD9,
Eoi = 0xD9,
}

impl MarkerCode {
Expand Down
18 changes: 8 additions & 10 deletions src/mov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn parse_metadata<R: Read + Seek>(reader: R) -> crate::Result<Vec<(String, E

entries.push(("duration".to_owned(), mvhd.duration_ms().into()));

if entries.iter().find(|x| x.0 == CREATIONDATE_KEY).is_none() {
if !entries.iter().any(|x| x.0 == CREATIONDATE_KEY) {
entries.push((
"com.apple.quicktime.creationdate".to_owned(),
EntryValue::Time(mvhd.creation_time()),
Expand Down Expand Up @@ -258,7 +258,7 @@ pub enum Error {
/// moov atom it may contain.
///
/// Regarding error handling, please refer to [Error] for more information.
fn extract_moov_body_from_buf<'a>(input: &'a [u8]) -> Result<Range<usize>, Error> {
fn extract_moov_body_from_buf(input: &[u8]) -> Result<Range<usize>, Error> {
// parse metadata from moov/meta/keys & moov/meta/ilst
let remain = input;

Expand All @@ -279,15 +279,13 @@ fn extract_moov_body_from_buf<'a>(input: &'a [u8]) -> Result<Range<usize>, Error
// stop travelling
skipped += h.header_size;
false
} else if (remain.len() as u64) < h.body_size() {
// stop travelling & skip unused box data
to_skip = h.body_size() - remain.len() as u64;
false
} else {
if (remain.len() as u64) < h.body_size() {
// stop travelling & skip unused box data
to_skip = h.body_size() - remain.len() as u64;
false
} else {
skipped += h.box_size as usize;
true
}
skipped += h.box_size as usize;
true
}
})
.map_err(|e| convert_error(e, "search atom moov failed"))?;
Expand Down
Loading

0 comments on commit 4b9e7f5

Please sign in to comment.