tidy up transcode presets and use a error enum rather than static strings

This commit is contained in:
chaos 2023-11-08 13:00:06 +00:00
parent d5ab74d17a
commit f10506be0b
No known key found for this signature in database
5 changed files with 53 additions and 34 deletions

View file

@ -1,7 +1,7 @@
pub mod presets; pub mod presets;
pub mod progress_monitor;
#[allow(clippy::all)]
mod transcoder;
pub mod types; 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; pub use self::transcoder::transcode;

View file

@ -2,7 +2,7 @@ use crate::utils::transcoder::types::Preset;
use crate::utils::transcoder::types::PresetCategory; use crate::utils::transcoder::types::PresetCategory;
use crate::utils::transcoder::types::TranscodeConfig; use crate::utils::transcoder::types::TranscodeConfig;
pub fn add_preset(preset_categories: &mut Vec<PresetCategory>) { pub fn add_presets(preset_categories: &mut Vec<PresetCategory>) {
preset_categories.push(PresetCategory { preset_categories.push(PresetCategory {
name: "flac".to_string(), name: "flac".to_string(),
presets: Vec::from([Preset { presets: Vec::from([Preset {

View file

@ -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 flac;
mod g726; mod g726;
mod mp3; mod mp3;
@ -12,6 +6,12 @@ mod speex;
mod vorbis; mod vorbis;
mod wav; mod wav;
use lazy_static::lazy_static;
use crate::utils::transcoder::types::PresetCategory;
use super::types::TranscodeConfig;
lazy_static! { lazy_static! {
#[derive(Debug)] #[derive(Debug)]
pub static ref TRANSCODE_CONFIGS: Vec<PresetCategory> = { pub static ref TRANSCODE_CONFIGS: Vec<PresetCategory> = {
@ -22,8 +22,8 @@ lazy_static! {
vorbis::add_presets(&mut preset_categories); vorbis::add_presets(&mut preset_categories);
g726::add_presets(&mut preset_categories); g726::add_presets(&mut preset_categories);
speex::add_presets(&mut preset_categories); speex::add_presets(&mut preset_categories);
flac::add_preset(&mut preset_categories); flac::add_presets(&mut preset_categories);
wav::add_preset(&mut preset_categories); wav::add_presets(&mut preset_categories);
preset_categories preset_categories
}; };
@ -50,24 +50,42 @@ pub fn get_preset(name: String) -> Option<TranscodeConfig> {
None None
} }
#[derive(Debug)]
pub enum PresetError {
PresetNonExistant,
NoneSpecified,
LoadError(Box<dyn std::error::Error>),
}
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( pub fn transcode_preset_or_config(
preset_name: Option<&String>, preset_name: Option<&String>,
config_path: Option<&String>, config_path: Option<&String>,
) -> Result<TranscodeConfig, Box<dyn std::error::Error>> { ) -> Result<TranscodeConfig, PresetError> {
if let Some(preset_name) = preset_name { if let Some(preset_name) = preset_name {
let preset_config = get_preset(preset_name.to_string()); let preset_config = get_preset(preset_name.to_string());
match preset_config { match preset_config {
Some(config) => Ok(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 { } else if let Some(config_path) = config_path {
let config = TranscodeConfig::load(config_path.to_string())?; return match TranscodeConfig::load(config_path.to_string()) {
Ok(config) => Ok(config),
Ok(config) Err(e) => Err(PresetError::LoadError(Box::from(e))),
};
} else { } else {
Err(into_err( Err(PresetError::NoneSpecified)
"please provide a transcode config or preset".to_string(),
))
} }
} }

View file

@ -2,7 +2,7 @@ use crate::utils::transcoder::types::Preset;
use crate::utils::transcoder::types::PresetCategory; use crate::utils::transcoder::types::PresetCategory;
use crate::utils::transcoder::types::TranscodeConfig; use crate::utils::transcoder::types::TranscodeConfig;
pub fn add_preset(preset_categories: &mut Vec<PresetCategory>) { pub fn add_presets(preset_categories: &mut Vec<PresetCategory>) {
preset_categories.push(PresetCategory { preset_categories.push(PresetCategory {
name: "wav".to_string(), name: "wav".to_string(),
presets: Vec::from([Preset { presets: Vec::from([Preset {

View file

@ -1,7 +1,6 @@
use std::{fs::File, io::BufReader, path::PathBuf}; use std::{fs::File, io::BufReader, path::PathBuf};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use string_error::static_err;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Preset { pub struct Preset {
@ -32,19 +31,21 @@ pub struct TranscodeConfig {
impl TranscodeConfig { impl TranscodeConfig {
pub fn load(path: String) -> Result<TranscodeConfig, Box<dyn std::error::Error>> { pub fn load(path: String) -> Result<TranscodeConfig, Box<dyn std::error::Error>> {
let path_buf = PathBuf::from(&path); 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 file = File::open(path)?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
if extension == "yml" || extension == "yaml" { match extension {
let u: TranscodeConfig = Some("json") => {
serde_yaml::from_reader(reader).expect("error while reading yaml"); let conf: TranscodeConfig =
return Ok(u); serde_json::from_reader(reader).expect("error while reading json");
} else if extension == "json" { return Ok(conf);
let u: TranscodeConfig = }
serde_json::from_reader(reader).expect("error while reading json"); Some("yml") | Some("yaml") | Some(&_) | None => {
return Ok(u); let conf: TranscodeConfig =
} serde_json::from_reader(reader).expect("error while reading json");
Err(static_err("Invalid File Extension")) return Ok(conf);
}
};
} }
} }