diff --git a/src/utils/transcoder/mod.rs b/src/utils/transcoder/mod.rs index 4f91c67..15a4ba4 100644 --- a/src/utils/transcoder/mod.rs +++ b/src/utils/transcoder/mod.rs @@ -1,7 +1,7 @@ pub mod presets; -pub mod progress_monitor; -#[allow(clippy::all)] -mod transcoder; pub mod types; -use self::progress_monitor::progress_monitor; +mod progress_monitor; +mod transcoder; + +pub use self::progress_monitor::progress_monitor; pub use self::transcoder::transcode; diff --git a/src/utils/transcoder/presets/flac.rs b/src/utils/transcoder/presets/flac.rs index 63afa5c..345d7bf 100644 --- a/src/utils/transcoder/presets/flac.rs +++ b/src/utils/transcoder/presets/flac.rs @@ -2,7 +2,7 @@ use crate::utils::transcoder::types::Preset; use crate::utils::transcoder::types::PresetCategory; use crate::utils::transcoder::types::TranscodeConfig; -pub fn add_preset(preset_categories: &mut Vec) { +pub fn add_presets(preset_categories: &mut Vec) { preset_categories.push(PresetCategory { name: "flac".to_string(), presets: Vec::from([Preset { diff --git a/src/utils/transcoder/presets/mod.rs b/src/utils/transcoder/presets/mod.rs index 1efefd3..c332d60 100644 --- a/src/utils/transcoder/presets/mod.rs +++ b/src/utils/transcoder/presets/mod.rs @@ -1,9 +1,3 @@ -use lazy_static::lazy_static; -use string_error::into_err; - -use crate::utils::transcoder::types::PresetCategory; -use crate::utils::transcoder::types::TranscodeConfig; - mod flac; mod g726; mod mp3; @@ -12,6 +6,12 @@ mod speex; mod vorbis; mod wav; +use lazy_static::lazy_static; + +use crate::utils::transcoder::types::PresetCategory; + +use super::types::TranscodeConfig; + lazy_static! { #[derive(Debug)] pub static ref TRANSCODE_CONFIGS: Vec = { @@ -22,8 +22,8 @@ lazy_static! { vorbis::add_presets(&mut preset_categories); g726::add_presets(&mut preset_categories); speex::add_presets(&mut preset_categories); - flac::add_preset(&mut preset_categories); - wav::add_preset(&mut preset_categories); + flac::add_presets(&mut preset_categories); + wav::add_presets(&mut preset_categories); preset_categories }; @@ -50,24 +50,42 @@ pub fn get_preset(name: String) -> Option { None } +#[derive(Debug)] +pub enum PresetError { + PresetNonExistant, + NoneSpecified, + LoadError(Box), +} + +impl std::fmt::Display for PresetError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PresetError::PresetNonExistant => write!(f, "Preset does not exist"), + PresetError::NoneSpecified => write!(f, "No preset or config specified"), + PresetError::LoadError(err) => write!(f, "Error loading config: {}", err) + } + } +} + +impl std::error::Error for PresetError {} + pub fn transcode_preset_or_config( preset_name: Option<&String>, config_path: Option<&String>, -) -> Result> { +) -> Result { if let Some(preset_name) = preset_name { let preset_config = get_preset(preset_name.to_string()); match preset_config { Some(config) => Ok(config), - None => Err(into_err("invalid preset name".to_string())), + None => Err(PresetError::PresetNonExistant), } } else if let Some(config_path) = config_path { - let config = TranscodeConfig::load(config_path.to_string())?; - - Ok(config) + return match TranscodeConfig::load(config_path.to_string()) { + Ok(config) => Ok(config), + Err(e) => Err(PresetError::LoadError(Box::from(e))), + }; } else { - Err(into_err( - "please provide a transcode config or preset".to_string(), - )) + Err(PresetError::NoneSpecified) } } diff --git a/src/utils/transcoder/presets/wav.rs b/src/utils/transcoder/presets/wav.rs index 6186a48..7203bd7 100644 --- a/src/utils/transcoder/presets/wav.rs +++ b/src/utils/transcoder/presets/wav.rs @@ -2,7 +2,7 @@ use crate::utils::transcoder::types::Preset; use crate::utils::transcoder::types::PresetCategory; use crate::utils::transcoder::types::TranscodeConfig; -pub fn add_preset(preset_categories: &mut Vec) { +pub fn add_presets(preset_categories: &mut Vec) { preset_categories.push(PresetCategory { name: "wav".to_string(), presets: Vec::from([Preset { diff --git a/src/utils/transcoder/types.rs b/src/utils/transcoder/types.rs index 6c9324a..e1ea105 100644 --- a/src/utils/transcoder/types.rs +++ b/src/utils/transcoder/types.rs @@ -1,7 +1,6 @@ use std::{fs::File, io::BufReader, path::PathBuf}; use serde::{Deserialize, Serialize}; -use string_error::static_err; #[derive(Debug, Serialize, Deserialize)] pub struct Preset { @@ -32,19 +31,21 @@ pub struct TranscodeConfig { impl TranscodeConfig { pub fn load(path: String) -> Result> { let path_buf = PathBuf::from(&path); - let extension = path_buf.extension().unwrap(); + let extension = path_buf.extension().and_then(std::ffi::OsStr::to_str); let file = File::open(path)?; let reader = BufReader::new(file); - if extension == "yml" || extension == "yaml" { - let u: TranscodeConfig = - serde_yaml::from_reader(reader).expect("error while reading yaml"); - return Ok(u); - } else if extension == "json" { - let u: TranscodeConfig = - serde_json::from_reader(reader).expect("error while reading json"); - return Ok(u); - } - Err(static_err("Invalid File Extension")) + match extension { + Some("json") => { + let conf: TranscodeConfig = + serde_json::from_reader(reader).expect("error while reading json"); + return Ok(conf); + } + Some("yml") | Some("yaml") | Some(&_) | None => { + let conf: TranscodeConfig = + serde_json::from_reader(reader).expect("error while reading json"); + return Ok(conf); + } + }; } }