fix all clippy "errors"

This commit is contained in:
Simon Benezan 2024-04-29 00:03:53 +02:00
parent 0889f94370
commit b11502f2e9
3 changed files with 33 additions and 38 deletions

View file

@ -304,7 +304,7 @@ impl Downloader {
.enumerate() .enumerate()
.map(|(i, f)| { .map(|(i, f)| {
len_from_segments(&f.video.0.segments()) len_from_segments(&f.video.0.segments())
- tmp_offsets.get(&i).map(|o| *o).unwrap_or_default() - tmp_offsets.get(&i).copied().unwrap_or_default()
}) })
.collect(); .collect();
let min = formats_with_offset.iter().min().unwrap(); let min = formats_with_offset.iter().min().unwrap();
@ -323,7 +323,7 @@ impl Downloader {
let mut audio_count: usize = 0; let mut audio_count: usize = 0;
let mut subtitle_count: usize = 0; let mut subtitle_count: usize = 0;
for (i, format) in self.formats.iter().enumerate() { for (i, format) in self.formats.iter().enumerate() {
let offset = offsets.get(&i).map(|o| *o).unwrap_or_default(); let offset = offsets.get(&i).copied().unwrap_or_default();
let format_len = format let format_len = format
.video .video
.0 .0
@ -372,7 +372,7 @@ impl Downloader {
root_format.subtitles.extend(subtitle_append); root_format.subtitles.extend(subtitle_append);
self.formats = vec![root_format]; self.formats = vec![root_format];
video_offset = offsets.get(&root_format_idx).map(|o| *o); video_offset = offsets.get(&root_format_idx).copied();
for raw_audio in raw_audios.iter_mut() { for raw_audio in raw_audios.iter_mut() {
raw_audio.video_idx = root_format_idx; raw_audio.video_idx = root_format_idx;
} }
@ -391,7 +391,7 @@ impl Downloader {
audios.push(FFmpegAudioMeta { audios.push(FFmpegAudioMeta {
path: raw_audio.path, path: raw_audio.path,
locale: raw_audio.locale, locale: raw_audio.locale,
start_time: audio_offsets.get(&raw_audio.format_id).map(|o| *o), start_time: audio_offsets.get(&raw_audio.format_id).copied(),
video_idx: raw_audio.video_idx, video_idx: raw_audio.video_idx,
}) })
} }

View file

@ -544,7 +544,7 @@ impl Format {
path.set_file_name(format!("{}.{}", &name[..(255 - ext.len() - 1)], ext)) path.set_file_name(format!("{}.{}", &name[..(255 - ext.len() - 1)], ext))
} }
} }
path.into_iter() path.iter()
.map(|s| { .map(|s| {
if s.len() > 255 { if s.len() > 255 {
s.to_string_lossy()[..255].to_string() s.to_string_lossy()[..255].to_string()

View file

@ -111,7 +111,7 @@ pub fn sync_audios(
for sync_audio in &sync_audios { for sync_audio in &sync_audios {
let chromaprint = generate_chromaprint( let chromaprint = generate_chromaprint(
&sync_audio.1, sync_audio.1,
&start, &start,
&end, &end,
initial_offsets.get(&sync_audio.0).unwrap(), initial_offsets.get(&sync_audio.0).unwrap(),
@ -130,7 +130,7 @@ pub fn sync_audios(
generate_chromaprint(base_audio.1, &start, &end, &base_offset)?, generate_chromaprint(base_audio.1, &start, &end, &base_offset)?,
); );
for audio in &sync_audios { for audio in &sync_audios {
let initial_offset = initial_offsets.get(&audio.0).map(|o| *o).unwrap(); let initial_offset = initial_offsets.get(&audio.0).copied().unwrap();
let offset = find_offset( let offset = find_offset(
(&base_audio.0, chromaprints.get(&base_audio.0).unwrap()), (&base_audio.0, chromaprints.get(&base_audio.0).unwrap()),
&base_offset, &base_offset,
@ -148,15 +148,12 @@ pub fn sync_audios(
audio.0, audio.0,
result result
.get(&audio.0) .get(&audio.0)
.map(|o| *o) .copied()
.unwrap_or_default() .unwrap_or_default()
.checked_add(&offset) .checked_add(&offset)
.unwrap(), .unwrap(),
); );
runs.insert( runs.insert(audio.0, runs.get(&audio.0).copied().unwrap_or_default() + 1);
audio.0,
runs.get(&audio.0).map(|o| *o).unwrap_or_default() + 1,
);
} }
} }
let mut result: HashMap<usize, TimeDelta> = result let mut result: HashMap<usize, TimeDelta> = result
@ -165,7 +162,7 @@ pub fn sync_audios(
( (
*format_id, *format_id,
TimeDelta::milliseconds( TimeDelta::milliseconds(
offset.num_milliseconds() / runs.get(format_id).map(|o| *o).unwrap(), offset.num_milliseconds() / runs.get(format_id).copied().unwrap(),
), ),
) )
}) })
@ -183,7 +180,7 @@ fn find_offset(
start: &TimeDelta, start: &TimeDelta,
sync_tolerance: u32, sync_tolerance: u32,
) -> Option<TimeDelta> { ) -> Option<TimeDelta> {
let (lhs_ranges, rhs_ranges) = compare_chromaprints(&lhs.1, &rhs.1, sync_tolerance); let (lhs_ranges, rhs_ranges) = compare_chromaprints(lhs.1, rhs.1, sync_tolerance);
if lhs_ranges.is_empty() || rhs_ranges.is_empty() { if lhs_ranges.is_empty() || rhs_ranges.is_empty() {
return None; return None;
} }
@ -191,8 +188,8 @@ fn find_offset(
let rhs_range = rhs_ranges[0]; let rhs_range = rhs_ranges[0];
let offset = rhs_range.end - lhs_range.end; let offset = rhs_range.end - lhs_range.end;
let offset = TimeDelta::milliseconds((offset * 1000.0) as i64) let offset = TimeDelta::milliseconds((offset * 1000.0) as i64)
.checked_add(&lhs_shift)? .checked_add(lhs_shift)?
.checked_sub(&rhs_shift)?; .checked_sub(rhs_shift)?;
debug!( debug!(
"Found offset of {}ms ({} - {} {}s) ({} - {} {}s) for format {} to {}", "Found offset of {}ms ({} - {} {}s) ({} - {} {}s) for format {} to {}",
offset.num_milliseconds(), offset.num_milliseconds(),
@ -205,7 +202,7 @@ fn find_offset(
rhs.0, rhs.0,
lhs.0 lhs.0
); );
return Some(offset); Some(offset)
} }
fn generate_chromaprint( fn generate_chromaprint(
@ -255,10 +252,10 @@ fn generate_chromaprint(
let mut chromaprint = Vec::with_capacity(length / 4); let mut chromaprint = Vec::with_capacity(length / 4);
for i in 0..length / 4 { for i in 0..length / 4 {
chromaprint.push(as_u32_le( chromaprint.push(as_u32_le(
raw_chromaprint[i * 4 + 0..i * 4 + 4].try_into().unwrap(), raw_chromaprint[i * 4..i * 4 + 4].try_into().unwrap(),
)); ));
} }
return Ok(chromaprint); Ok(chromaprint)
} }
fn compare_chromaprints( fn compare_chromaprints(
@ -266,8 +263,8 @@ fn compare_chromaprints(
rhs_chromaprint: &Vec<u32>, rhs_chromaprint: &Vec<u32>,
sync_tolerance: u32, sync_tolerance: u32,
) -> (Vec<TimeRange>, Vec<TimeRange>) { ) -> (Vec<TimeRange>, Vec<TimeRange>) {
let lhs_inverse_index = create_inverse_index(&lhs_chromaprint); let lhs_inverse_index = create_inverse_index(lhs_chromaprint);
let rhs_inverse_index = create_inverse_index(&rhs_chromaprint); let rhs_inverse_index = create_inverse_index(rhs_chromaprint);
let mut possible_shifts = HashSet::new(); let mut possible_shifts = HashSet::new();
for lhs_pair in lhs_inverse_index { for lhs_pair in lhs_inverse_index {
@ -275,7 +272,7 @@ fn compare_chromaprints(
for i in -2..=2 { for i in -2..=2 {
let modified_point = (original_point as i32 + i) as u32; let modified_point = (original_point as i32 + i) as u32;
if rhs_inverse_index.contains_key(&modified_point) { if rhs_inverse_index.contains_key(&modified_point) {
let rhs_index = rhs_inverse_index.get(&modified_point).map(|o| *o).unwrap(); let rhs_index = rhs_inverse_index.get(&modified_point).copied().unwrap();
possible_shifts.insert(rhs_index as i32 - lhs_pair.1 as i32); possible_shifts.insert(rhs_index as i32 - lhs_pair.1 as i32);
} }
} }
@ -285,8 +282,8 @@ fn compare_chromaprints(
let mut all_rhs_time_ranges = vec![]; let mut all_rhs_time_ranges = vec![];
for shift_amount in possible_shifts { for shift_amount in possible_shifts {
let time_range_pair = find_time_ranges( let time_range_pair = find_time_ranges(
&lhs_chromaprint, lhs_chromaprint,
&rhs_chromaprint, rhs_chromaprint,
shift_amount, shift_amount,
sync_tolerance, sync_tolerance,
); );
@ -324,20 +321,20 @@ fn compare_chromaprints(
all_rhs_time_ranges.sort_by(|a, b| (a.end - a.start).total_cmp(&(b.end - b.start))); all_rhs_time_ranges.sort_by(|a, b| (a.end - a.start).total_cmp(&(b.end - b.start)));
all_rhs_time_ranges.reverse(); all_rhs_time_ranges.reverse();
return (all_lhs_time_ranges, all_rhs_time_ranges); (all_lhs_time_ranges, all_rhs_time_ranges)
} }
fn create_inverse_index(chromaprint: &Vec<u32>) -> HashMap<u32, usize> { fn create_inverse_index(chromaprint: &Vec<u32>) -> HashMap<u32, usize> {
let mut inverse_index = HashMap::with_capacity(chromaprint.capacity()); let mut inverse_index = HashMap::with_capacity(chromaprint.capacity());
for i in 0..chromaprint.capacity() { for (i, fingerprint) in chromaprint.iter().enumerate().take(chromaprint.capacity()) {
inverse_index.insert(chromaprint[i], i); inverse_index.insert(*fingerprint, i);
} }
return inverse_index; inverse_index
} }
fn find_time_ranges( fn find_time_ranges(
lhs_chromaprint: &Vec<u32>, lhs_chromaprint: &[u32],
rhs_chromaprint: &Vec<u32>, rhs_chromaprint: &[u32],
shift_amount: i32, shift_amount: i32,
sync_tolerance: u32, sync_tolerance: u32,
) -> Option<(Vec<TimeRange>, Vec<TimeRange>)> { ) -> Option<(Vec<TimeRange>, Vec<TimeRange>)> {
@ -372,13 +369,11 @@ fn find_time_ranges(
rhs_matching_timestamps.push(f64::MAX); rhs_matching_timestamps.push(f64::MAX);
let lhs_time_ranges = timestamps_to_ranges(lhs_matching_timestamps); let lhs_time_ranges = timestamps_to_ranges(lhs_matching_timestamps);
if lhs_time_ranges.is_none() { lhs_time_ranges.as_ref()?;
return None;
}
let lhs_time_ranges = lhs_time_ranges.unwrap(); let lhs_time_ranges = lhs_time_ranges.unwrap();
let rhs_time_ranges = timestamps_to_ranges(rhs_matching_timestamps).unwrap(); let rhs_time_ranges = timestamps_to_ranges(rhs_matching_timestamps).unwrap();
return Some((lhs_time_ranges, rhs_time_ranges)); Some((lhs_time_ranges, rhs_time_ranges))
} }
fn timestamps_to_ranges(mut timestamps: Vec<f64>) -> Option<Vec<TimeRange>> { fn timestamps_to_ranges(mut timestamps: Vec<f64>) -> Option<Vec<TimeRange>> {
@ -402,20 +397,20 @@ fn timestamps_to_ranges(mut timestamps: Vec<f64>) -> Option<Vec<TimeRange>> {
continue; continue;
} }
time_ranges.push(current_range.clone()); time_ranges.push(current_range);
current_range.start = next; current_range.start = next;
current_range.end = next; current_range.end = next;
} }
return if time_ranges.len() > 0 { if !time_ranges.is_empty() {
Some(time_ranges) Some(time_ranges)
} else { } else {
None None
}; }
} }
fn as_u32_le(array: &[u8; 4]) -> u32 { fn as_u32_le(array: &[u8; 4]) -> u32 {
#![allow(arithmetic_overflow)] #![allow(arithmetic_overflow)]
((array[0] as u32) << 0) (array[0] as u32)
| ((array[1] as u32) << 8) | ((array[1] as u32) << 8)
| ((array[2] as u32) << 16) | ((array[2] as u32) << 16)
| ((array[3] as u32) << 24) | ((array[3] as u32) << 24)