refactor: optimize imports

This commit is contained in:
Didier Slof 2023-05-08 10:07:09 +02:00
parent 25e63c2aae
commit ea076dcf72
Signed by: didier
GPG key ID: 01E71F18AA4398E5
7 changed files with 39 additions and 14 deletions

View file

@ -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"

View file

@ -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

13
src/error.rs Normal file
View file

@ -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),
}

View file

@ -1,5 +1,4 @@
use std::path::PathBuf;
use crate::configuration::Config;
pub fn get_files<S: Into<String>>(path: S) -> Vec<PathBuf> {
let mut files = Vec::new();

View file

@ -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")));

View file

@ -1,10 +1,19 @@
use std::error::Error;
use std::process::Command;
pub fn renice(pid: u32, niceness: u8) -> Result<(), Box<dyn Error>> {
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()?;
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))
}
}

View file

@ -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<Child, std::io::Error> {
pub fn run(&self, ffmpeg_config: &ConfigFFmpeg) -> Result<Child, error::Error> {
let mut command = self.build_command(ffmpeg_config);
command.spawn()
match command.spawn() {
Ok(child) => Ok(child),
Err(e) => Err(error::Error::JobError(e))
}
}
}