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

View file

@ -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<PresetCategory>) {
pub fn add_presets(preset_categories: &mut Vec<PresetCategory>) {
preset_categories.push(PresetCategory {
name: "flac".to_string(),
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 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<PresetCategory> = {
@ -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<TranscodeConfig> {
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(
preset_name: Option<&String>,
config_path: Option<&String>,
) -> Result<TranscodeConfig, Box<dyn std::error::Error>> {
) -> Result<TranscodeConfig, PresetError> {
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)
}
}

View file

@ -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<PresetCategory>) {
pub fn add_presets(preset_categories: &mut Vec<PresetCategory>) {
preset_categories.push(PresetCategory {
name: "wav".to_string(),
presets: Vec::from([Preset {

View file

@ -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<TranscodeConfig, Box<dyn std::error::Error>> {
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);
}
};
}
}