Skip to content

Commit

Permalink
refactor: DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
flazepe committed Oct 26, 2024
1 parent 3380d5a commit 4e824a5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/clipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Clipper {
"no-video" | "vn" => inputs.set_no_video(true),
"no-audio" | "an" => inputs.set_no_audio(true),
"dry-run" | "d" => dry_run = true,
_ => error!(format!("Invalid option: -{option}")),
_ => error!("Invalid option: -{option}"),
}

continue;
Expand Down Expand Up @@ -140,8 +140,8 @@ macro_rules! string_vec {

#[macro_export]
macro_rules! error {
($message:expr) => {{
println!("\x1b[38;5;203m{}\x1b[0m", $message);
($string:expr) => {{
println!("\x1b[38;5;203m{}\x1b[0m", format!($string));
std::process::exit(1);
}};
}
4 changes: 2 additions & 2 deletions src/ffmpeg/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Encoder {
}

self.crf = Some(crf.parse::<f64>().unwrap_or_else(|_| {
error!(format!("Invalid CRF value: {crf}"));
error!("Invalid CRF value: {crf}");
}));
}

Expand All @@ -39,7 +39,7 @@ impl Encoder {
}

self.cq = Some(cq.parse::<f64>().unwrap_or_else(|_| {
error!(format!("Invalid CQ value: {cq}"));
error!("Invalid CQ value: {cq}");
}));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ffmpeg/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ impl Input {

pub fn set_video_track(&mut self, video_track: String) {
self.video_track = video_track.parse::<u8>().unwrap_or_else(|_| {
error!(format!("Invalid video track: {video_track}"));
error!("Invalid video track: {video_track}");
});
}

pub fn set_audio_track(&mut self, audio_track: String) {
self.audio_track = audio_track.parse::<u8>().unwrap_or_else(|_| {
error!(format!("Invalid audio track: {audio_track}"));
error!("Invalid audio track: {audio_track}");
});
}

pub fn set_subtitle_track(&mut self, subtitle_track: String) {
self.subtitle_track = Some(subtitle_track.parse::<u8>().unwrap_or_else(|_| {
error!(format!("Invalid subtitle track: {subtitle_track}"));
error!("Invalid subtitle track: {subtitle_track}");
}));
}
}
5 changes: 3 additions & 2 deletions src/ffmpeg/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl IntoIterator for Inputs {
}

if let Some(input) = self.inputs.iter().find(|input| input.segments.is_empty()) {
error!(format!(r#"Input "{}" has no segments."#, input.file));
let file = &input.file;
error!(r#"Input "{file}" has no segments."#);
}

if self.no_video && self.no_audio {
Expand Down Expand Up @@ -80,7 +81,7 @@ impl IntoIterator for Inputs {
.split_once('-')
.map(|(from, to)| (duration_to_secs(from), duration_to_secs(to)))
.unwrap_or_else(|| {
error!(format!("Invalid segment duration range: {segment}"));
error!("Invalid segment duration range: {segment}");
});
let fade_to = to - self.fade.unwrap_or(0.) - 0.5;

Expand Down
2 changes: 1 addition & 1 deletion src/ffmpeg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn duration_to_secs<T: Display>(duration: T) -> f64 {
.map(|entry| {
entry
.parse::<f64>()
.unwrap_or_else(|_| error!(format!("Invalid segment duration: {entry}")))
.unwrap_or_else(|_| error!("Invalid segment duration: {entry}"))
})
.collect::<Vec<f64>>();

Expand Down

0 comments on commit 4e824a5

Please sign in to comment.