From ea076dcf72af4c81805108b54475556697cc0881 Mon Sep 17 00:00:00 2001 From: Didier Date: Mon, 8 May 2023 10:07:09 +0200 Subject: [PATCH] refactor: optimize imports --- Cargo.toml | 1 + src/configuration/mod.rs | 4 ---- src/error.rs | 13 +++++++++++++ src/files.rs | 1 - src/main.rs | 5 ++++- src/renice.rs | 17 +++++++++++++---- src/transcode/job.rs | 12 ++++++++---- 7 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 src/error.rs diff --git a/Cargo.toml b/Cargo.toml index 1a99af9..b01de41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" simple-log = "1.6.0" toml = "0.7.3" serde = { version = "1.0.162", features = ["derive"] } +thiserror = "1.0.40" \ No newline at end of file diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index d99717c..fb9b9b7 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,9 +1,5 @@ -use std::error::Error; - use serde::{Deserialize, Serialize}; -use crate::ffmpeg::FFmpegCommandOptions; - mod ffmpeg; #[allow(non_camel_case_types)] // this is allowed cuz we want to use snake case in the config file diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..1f32a9e --- /dev/null +++ b/src/error.rs @@ -0,0 +1,13 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum Error { + #[error("Renice error: {0}")] + ReniceError(std::io::Error), + + #[error("FFmpeg error: {0}")] + FFmpegError(std::io::Error), + + #[error("Job error: {0}")] + JobError(std::io::Error), +} \ No newline at end of file diff --git a/src/files.rs b/src/files.rs index a1bf1db..3ed35c2 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,5 +1,4 @@ use std::path::PathBuf; -use crate::configuration::Config; pub fn get_files>(path: S) -> Vec { let mut files = Vec::new(); diff --git a/src/main.rs b/src/main.rs index 9d6a1c0..98e0cb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,9 @@ extern crate simple_log; use std::env; use std::path::PathBuf; + use simple_log::LogConfigBuilder; + use renice::renice; use transcode::job::TranscodeJob; @@ -12,6 +14,7 @@ mod files; mod ffmpeg; mod renice; mod transcode; +mod error; fn main() { let config = configuration::Config::from_file(&env::var("CONFIG").unwrap_or(String::from("./config.toml"))); @@ -91,4 +94,4 @@ fn setup_logger(config: &configuration::Config) { .build(); simple_log::new(log_config).unwrap(); -} +} \ No newline at end of file diff --git a/src/renice.rs b/src/renice.rs index ece236b..01867e1 100644 --- a/src/renice.rs +++ b/src/renice.rs @@ -1,10 +1,19 @@ -use std::error::Error; use std::process::Command; -pub fn renice(pid: u32, niceness: u8) -> Result<(), Box> { +use crate::error; + +pub fn renice(pid: u32, niceness: u8) -> Result<(), error::Error> { let mut command = Command::new("renice"); command.arg(niceness.to_string()); command.arg(pid.to_string()); - command.spawn()?; - Ok(()) + match command.output() { + Ok(output) => { + if output.status.success() { + Ok(()) + } else { + Err(error::Error::ReniceError(std::io::Error::new(std::io::ErrorKind::Other, "renice failed"))) + } + }, + Err(e) => Err(error::Error::ReniceError(e)) + } } \ No newline at end of file diff --git a/src/transcode/job.rs b/src/transcode/job.rs index 5fb5177..4fe40a3 100644 --- a/src/transcode/job.rs +++ b/src/transcode/job.rs @@ -1,6 +1,7 @@ -use std::path::PathBuf; use std::process::{Child, Command}; -use crate::configuration::{Config, ConfigFFmpeg}; + +use crate::configuration::ConfigFFmpeg; +use crate::error; pub struct TranscodeJob { pub input: String, @@ -26,8 +27,11 @@ impl TranscodeJob { std::path::Path::new(&self.output).exists() } - pub fn run(&self, ffmpeg_config: &ConfigFFmpeg) -> Result { + pub fn run(&self, ffmpeg_config: &ConfigFFmpeg) -> Result { let mut command = self.build_command(ffmpeg_config); - command.spawn() + match command.spawn() { + Ok(child) => Ok(child), + Err(e) => Err(error::Error::JobError(e)) + } } } \ No newline at end of file