Skip to content

Commit 4b2ba71

Browse files
committed
Clippy, etc.
1 parent 079ee3d commit 4b2ba71

File tree

15 files changed

+126
-152
lines changed

15 files changed

+126
-152
lines changed

src/bin/ffmpeg_source.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::source::{Fps, Source};
2-
use crate::SrcPath;
3-
use crate::BinResult;
4-
use gifski::Collector;
5-
use gifski::Settings;
2+
use crate::{BinResult, SrcPath};
3+
use gifski::{Collector, Settings};
64
use imgref::*;
75
use rgb::*;
86

@@ -17,6 +15,7 @@ impl Source for FfmpegDecoder {
1715
fn total_frames(&self) -> Option<u64> {
1816
Some(self.frames)
1917
}
18+
2019
fn collect(&mut self, dest: &mut Collector) -> BinResult<()> {
2120
self.collect_frames(dest)
2221
}
@@ -36,12 +35,7 @@ impl FfmpegDecoder {
3635
let stream = input_context.streams().best(ffmpeg::media::Type::Video).ok_or("The file has no video tracks")?;
3736
let time_base = stream.time_base().numerator() as f64 / stream.time_base().denominator() as f64;
3837
let frames = (stream.duration() as f64 * time_base * filter_fps as f64).ceil() as u64;
39-
Ok(Self {
40-
input_context,
41-
frames,
42-
rate,
43-
settings,
44-
})
38+
Ok(Self { input_context, frames, rate, settings })
4539
}
4640

4741
#[inline(never)]
@@ -76,7 +70,6 @@ impl FfmpegDecoder {
7670
(stream.index(), decoder, filter)
7771
};
7872

79-
8073
let add_frame = |rgba_frame: &ffmpeg::util::frame::Video, pts: f64, pos: i64| -> BinResult<()> {
8174
let stride = rgba_frame.stride(0) as usize;
8275
if stride % 4 != 0 {

src/bin/gif_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! This is for reading GIFs as an input for re-encoding as another GIF
22
3-
use std::io::Read;
43
use crate::source::{Fps, Source};
54
use crate::{BinResult, SrcPath};
65
use gif::Decoder;
76
use gifski::Collector;
7+
use std::io::Read;
88

99
pub struct GifDecoder {
1010
speed: f32,

src/bin/gifski.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@
88
#![allow(clippy::redundant_closure_for_method_calls)]
99
#![allow(clippy::wildcard_imports)]
1010

11-
use clap::error::ErrorKind::MissingRequiredArgument;
1211
use clap::builder::NonEmptyStringValueParser;
12+
use clap::error::ErrorKind::MissingRequiredArgument;
13+
use clap::value_parser;
14+
use gifski::{Repeat, Settings};
1315
use std::io::stdin;
1416
use std::io::BufRead;
1517
use std::io::BufReader;
1618
use std::io::IsTerminal;
1719
use std::io::Read;
1820
use std::io::StdinLock;
1921
use std::io::Stdout;
20-
use gifski::{Settings, Repeat};
21-
use clap::value_parser;
2222

2323
#[cfg(feature = "video")]
2424
mod ffmpeg_source;
25-
mod png;
2625
mod gif_source;
27-
mod y4m_source;
26+
mod png;
2827
mod source;
28+
mod y4m_source;
2929
use crate::source::Source;
3030

3131
use gifski::progress::{NoProgress, ProgressReporter};
3232

3333
pub type BinResult<T, E = Box<dyn std::error::Error + Send + Sync>> = Result<T, E>;
3434

35-
use clap::{Command, Arg, ArgAction};
35+
use clap::{Arg, ArgAction, Command};
3636

3737
use std::env;
3838
use std::fmt;
@@ -239,8 +239,7 @@ fn bin_main() -> BinResult<()> {
239239

240240
if fps > 100.0 || fps <= 0.0 {
241241
return Err("100 fps is maximum".into());
242-
}
243-
else if !quiet && fps > 50.0 {
242+
} else if !quiet && fps > 50.0 {
244243
eprintln!("warning: web browsers support max 50 fps");
245244
}
246245

@@ -285,7 +284,7 @@ fn bin_main() -> BinResult<()> {
285284
}
286285
SrcPath::Stdin(BufReader::new(fd))
287286
} else {
288-
SrcPath::Path(path.to_path_buf())
287+
SrcPath::Path(path.clone())
289288
};
290289
match file_type(&mut src).unwrap_or(FileType::Other) {
291290
FileType::PNG | FileType::JPEG => return Err("Only a single image file was given as an input. This is not enough to make an animation.".into()),
@@ -413,7 +412,7 @@ fn panic_err(err: Box<dyn std::any::Any + Send>) -> String {
413412
}
414413

415414
fn parse_color(c: &str) -> Result<rgb::RGB8, String> {
416-
let c = c.trim_matches(|c:char| c.is_ascii_whitespace());
415+
let c = c.trim_matches(|c: char| c.is_ascii_whitespace());
417416
let c = c.strip_prefix('#').unwrap_or(c);
418417

419418
if c.len() != 6 {
@@ -429,15 +428,15 @@ fn parse_color(c: &str) -> Result<rgb::RGB8, String> {
429428
}
430429

431430
fn parse_colors(colors: &str) -> Result<Vec<rgb::RGB8>, String> {
432-
colors.split(|c:char| c == ' ' || c == ',')
431+
colors.split([' ', ','])
433432
.filter(|c| !c.is_empty())
434433
.map(parse_color)
435434
.collect()
436435
}
437436

438437
#[test]
439438
fn color_parser() {
440-
assert_eq!(parse_colors("#123456 78abCD,, ,").unwrap(), vec![rgb::RGB8::new(0x12,0x34,0x56), rgb::RGB8::new(0x78,0xab,0xcd)]);
439+
assert_eq!(parse_colors("#123456 78abCD,, ,").unwrap(), vec![rgb::RGB8::new(0x12, 0x34, 0x56), rgb::RGB8::new(0x78, 0xab, 0xcd)]);
441440
assert!(parse_colors("#12345").is_err());
442441
}
443442

@@ -456,7 +455,7 @@ fn file_type(src: &mut SrcPath) -> BinResult<FileType> {
456455
_ => {
457456
let mut file = std::fs::File::open(path)?;
458457
file.read_exact(&mut buf)?;
459-
}
458+
},
460459
},
461460
SrcPath::Stdin(stdin) => {
462461
let buf_in = stdin.fill_buf()?;
@@ -494,18 +493,15 @@ fn check_if_paths_exist(paths: &[PathBuf]) -> BinResult<()> {
494493
};
495494
let canon = path.canonicalize();
496495
if let Some(parent) = canon.as_deref().unwrap_or(path).parent() {
497-
if parent.as_os_str() != "" {
498-
if let Ok(false) = path.try_exists() {
499-
use std::fmt::Write;
500-
if msg.len() > 80 {
501-
msg.push('\n');
502-
}
503-
write!(&mut msg, " (directory \"{}\" doesn't exist either)", parent.display())?;
504-
496+
if parent.as_os_str() != "" && matches!(path.try_exists(), Ok(false)) {
497+
use std::fmt::Write;
498+
if msg.len() > 80 {
499+
msg.push('\n');
505500
}
501+
write!(&mut msg, " (directory \"{}\" doesn't exist either)", parent.display())?;
506502
}
507503
}
508-
if path.to_str().map_or(false, |p| p.contains(['*','?','['])) {
504+
if path.to_str().is_some_and(|p| p.contains(['*', '?', '['])) {
509505
msg += "\nThe wildcard pattern did not match any files.";
510506
} else if path.is_relative() {
511507
use std::fmt::Write;
@@ -514,7 +510,7 @@ fn check_if_paths_exist(paths: &[PathBuf]) -> BinResult<()> {
514510
if path.extension() == Some("gif".as_ref()) {
515511
msg = format!("\nDid you mean to use -o \"{}\" to specify it as the output file instead?", path.display());
516512
}
517-
return Err(msg.into())
513+
return Err(msg.into());
518514
}
519515
Ok(())
520516
}
@@ -571,7 +567,7 @@ fn get_video_decoder(ftype: FileType, src: SrcPath, fps: source::Fps, _: Setting
571567
SrcPath::Path(path) => path,
572568
SrcPath::Stdin(_) => Path::new("video.mp4"),
573569
};
574-
let rel_path = path.file_name().map(Path::new).unwrap_or(path);
570+
let rel_path = path.file_name().map_or(path, Path::new);
575571
Err(format!(r#"Video support is permanently disabled in this distribution of gifski.
576572
577573
The only 'video' format supported at this time is YUV4MPEG2, which can be piped from ffmpeg:
@@ -627,12 +623,12 @@ impl ProgressReporter for ProgressBar {
627623
let total_size = bytes * self.pb.total / self.frames;
628624
let new_estimate = if total_size >= self.previous_estimate { total_size } else { (self.previous_estimate + total_size) / 2 };
629625
self.previous_estimate = new_estimate;
630-
if self.displayed_estimate.abs_diff(new_estimate) > new_estimate/10 {
626+
if self.displayed_estimate.abs_diff(new_estimate) > new_estimate / 10 {
631627
self.displayed_estimate = new_estimate;
632628
let (num, unit, x) = if new_estimate > 1_000_000 {
633-
(new_estimate as f64/1_000_000., "MB", if new_estimate > 10_000_000 {0} else {1})
629+
(new_estimate as f64 / 1_000_000., "MB", if new_estimate > 10_000_000 { 0 } else { 1 })
634630
} else {
635-
(new_estimate as f64/1_000., "KB", 0)
631+
(new_estimate as f64 / 1_000., "KB", 0)
636632
};
637633
self.pb.message(&format!("{num:.x$}{unit} GIF; Frame "));
638634
}

src/bin/png.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
2-
use crate::source::Fps;
3-
use crate::source::Source;
1+
use crate::source::{Fps, Source};
42
use crate::BinResult;
53
use gifski::Collector;
64
use std::path::PathBuf;
@@ -12,7 +10,10 @@ pub struct Lodecoder {
1210

1311
impl Lodecoder {
1412
pub fn new(frames: Vec<PathBuf>, params: Fps) -> Self {
15-
Self { frames, fps: f64::from(params.fps) * f64::from(params.speed) }
13+
Self {
14+
frames,
15+
fps: f64::from(params.fps) * f64::from(params.speed),
16+
}
1617
}
1718
}
1819

src/bin/y4m_source.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use std::io::BufReader;
22
use std::io::Read;
33
use imgref::ImgVec;
4-
use y4m::Colorspace;
5-
use y4m::Decoder;
6-
use y4m::ParseError;
74
use gifski::Collector;
8-
use yuv::color::MatrixCoefficients;
9-
use yuv::color::Range;
5+
use y4m::{Colorspace, Decoder, ParseError};
6+
use yuv::color::{MatrixCoefficients, Range};
107
use yuv::convert::RGBConvert;
118
use yuv::YUV;
129
use crate::{SrcPath, BinResult};
@@ -87,6 +84,7 @@ impl Source for Y4MDecoder {
8784
file_size.saturating_sub(self.decoder.get_raw_params().len() as _) / (w * h * d * s / 4 + 6) as u64
8885
})
8986
}
87+
9088
fn collect(&mut self, c: &mut Collector) -> BinResult<()> {
9189
let fps = self.decoder.get_framerate();
9290
let frame_time = 1. / (fps.num as f64 / fps.den as f64);
@@ -175,8 +173,8 @@ impl Source for Y4MDecoder {
175173
},
176174
Samp::S2x1 => {
177175
let y = y.chunks_exact(width);
178-
let u = u.chunks_exact((width+1)/2);
179-
let v = v.chunks_exact((width+1)/2);
176+
let u = u.chunks_exact(width.div_ceil(2));
177+
let v = v.chunks_exact(width.div_ceil(2));
180178
if y.len() != v.len() {
181179
return bad_frame(raw_params_str);
182180
}
@@ -192,8 +190,8 @@ impl Source for Y4MDecoder {
192190
},
193191
Samp::S2x2 => {
194192
let y = y.chunks_exact(width);
195-
let u = u.chunks_exact((width+1)/2).flat_map(|r| [r, r]);
196-
let v = v.chunks_exact((width+1)/2).flat_map(|r| [r, r]);
193+
let u = u.chunks_exact(width.div_ceil(2)).flat_map(|r| [r, r]);
194+
let v = v.chunks_exact(width.div_ceil(2)).flat_map(|r| [r, r]);
197195
for (y, (u, v)) in y.zip(u.zip(v)) {
198196
let u = u.iter().copied().flat_map(|x| [x, x]);
199197
let v = v.iter().copied().flat_map(|x| [x, x]);
@@ -204,7 +202,7 @@ impl Source for Y4MDecoder {
204202
}));
205203
}
206204
},
207-
};
205+
}
208206
if out.len() != width * height {
209207
return bad_frame(raw_params_str);
210208
}

0 commit comments

Comments
 (0)