From 8f03cf4f17f437bd760c0d374c4cf934b9e4cba0 Mon Sep 17 00:00:00 2001 From: chaos Date: Thu, 31 Aug 2023 23:50:30 +0100 Subject: [PATCH] code tidying --- modules/taglib/src/impls/file.rs | 7 +-- modules/taglib/src/impls/oggtag.rs | 4 +- modules/taglib/src/impls/tag.rs | 6 ++- src/args.rs | 10 ++-- src/commands/copy.rs | 59 +++++++++++++----------- src/commands/genhtml.rs | 35 ++++++++------ src/commands/get_tags.rs | 2 +- src/commands/process.rs | 42 ++++++++--------- src/commands/set_tags.rs | 2 +- src/commands/transcode.rs | 22 +++------ src/main.rs | 33 ++++++------- src/types.rs | 16 ++++--- src/utils/ascii_reduce/mod.rs | 19 +++----- src/utils/format_detection.rs | 1 + src/utils/formats/flac.rs | 4 +- src/utils/formats/generic_ffmpeg.rs | 22 ++++++--- src/utils/formats/generic_taglib.rs | 4 +- src/utils/formats/id3.rs | 4 +- src/utils/formats/mod.rs | 1 + src/utils/music_scanner.rs | 2 +- src/utils/transcoder/progress_monitor.rs | 3 +- src/utils/transcoder/transcoder.rs | 30 ++++++------ 22 files changed, 169 insertions(+), 159 deletions(-) diff --git a/modules/taglib/src/impls/file.rs b/modules/taglib/src/impls/file.rs index b7ecbc4..330b489 100644 --- a/modules/taglib/src/impls/file.rs +++ b/modules/taglib/src/impls/file.rs @@ -13,7 +13,7 @@ pub fn new_taglib_file( filepath: String, taglib_type: Option, ) -> Result { - let filename_c = CString::new(filepath.clone()).unwrap(); + let filename_c = CString::new(filepath).unwrap(); let filename_c_ptr = filename_c.as_ptr(); let file = unsafe { @@ -70,10 +70,11 @@ impl TagLibFile { impl File for TagLibFile { fn save(&mut self) -> Result<(), TagLibError> { let result = unsafe { bindings::wrap_taglib_file_save(self.ctx) }; - return match result { + + match result { true => Ok(()), false => Err(TagLibError::SaveError), - }; + } } } diff --git a/modules/taglib/src/impls/oggtag.rs b/modules/taglib/src/impls/oggtag.rs index 1118b20..69fe539 100644 --- a/modules/taglib/src/impls/oggtag.rs +++ b/modules/taglib/src/impls/oggtag.rs @@ -12,9 +12,9 @@ impl TagLibOggTag { let value = unsafe { bindings::wrap_taglib_opustag_get_field(self.ctx, key.as_ptr()) }; if value.is_null() { - return None; + None } else { - return c_str_to_str(value); + c_str_to_str(value) } } diff --git a/modules/taglib/src/impls/tag.rs b/modules/taglib/src/impls/tag.rs index d7a829f..a588a4b 100644 --- a/modules/taglib/src/impls/tag.rs +++ b/modules/taglib/src/impls/tag.rs @@ -9,7 +9,8 @@ pub struct TagLibTag { impl Tag for TagLibTag { fn title(&self) -> Option { let title_ref = unsafe { bindings::wrap_taglib_tag_title(self.ctx) }; - return c_str_to_str(title_ref); + + c_str_to_str(title_ref) } fn set_title(&mut self, title: String) { let title = CString::new(title).unwrap(); @@ -18,7 +19,8 @@ impl Tag for TagLibTag { } fn artist(&self) -> Option { let artist_ref = unsafe { bindings::wrap_taglib_tag_artist(self.ctx) }; - return c_str_to_str(artist_ref); + + c_str_to_str(artist_ref) } fn set_artist(&mut self, artist: String) { let artist = CString::new(artist).unwrap(); diff --git a/src/args.rs b/src/args.rs index 17bd51e..d07988c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -4,10 +4,10 @@ use clap::{Args, Parser, Subcommand}; #[clap()] pub struct CLIArgs { #[clap(subcommand)] - pub command: Option, + pub command: Commands, } -#[derive(Debug, Subcommand)] +#[derive(Debug, Clone, Subcommand)] pub enum Commands { Process(ProcessCommandArgs), Genhtml(GenHTMLCommandArgs), @@ -17,7 +17,7 @@ pub enum Commands { GetTags(GetTagsCommandArgs), } -#[derive(Debug, Args)] +#[derive(Debug, Clone, Args)] pub struct ProcessCommandArgs { pub source: String, #[clap(long)] @@ -30,7 +30,7 @@ pub struct ProcessCommandArgs { pub replaygain_threads: Option, } -#[derive(Debug, Args)] +#[derive(Debug, Clone, Args)] pub struct GenHTMLCommandArgs { pub source: String, pub dest: String, @@ -42,7 +42,7 @@ pub struct GenHTMLCommandArgs { pub link_base: Option, } -#[derive(Debug, Args)] +#[derive(Debug, Clone, Args)] pub struct TranscodeCommandArgs { pub source: String, pub dest: String, diff --git a/src/commands/copy.rs b/src/commands/copy.rs index bf1f0f1..ebedf8a 100644 --- a/src/commands/copy.rs +++ b/src/commands/copy.rs @@ -23,7 +23,7 @@ use crate::{ }; pub fn copy_command( - _args: &CLIArgs, + _args: CLIArgs, copy_args: &CopyCommandArgs, ) -> Result<(), Box> { if copy_args.transcode_config.is_none() && copy_args.transcode_preset.is_none() { @@ -56,6 +56,7 @@ pub fn copy_command( for file in files.iter() { let filename = file.join_filename(); + if let Entry::Vacant(entry) = seen.entry(filename.clone()) { entry.insert(true); } else { @@ -82,7 +83,7 @@ pub fn copy_command( for directory in directories.iter() { fs::create_dir_all( PathBuf::from_str(copy_args.dest.as_str()) - .unwrap() + .expect("invalid destination") .join(directory), )?; } @@ -107,14 +108,15 @@ pub fn copy_command( fn copy_file(file: &File, copy_args: &CopyCommandArgs) -> Result<(), Box> { let from_path = file.join_path_to(); - let to_path_dest = PathBuf::from_str(copy_args.dest.as_str()).unwrap(); + let to_path_dest = PathBuf::from_str(copy_args.dest.as_str()).expect("invalid destination"); let to_path = match copy_args.single_directory { true => to_path_dest.join(file.join_filename()), false => to_path_dest .join(file.path_from_source.clone()) .join(file.join_filename()), }; - let to_path_string = to_path.as_os_str().to_str().unwrap().to_string(); + + let to_path_string = to_path.to_string_lossy(); if !copy_args.no_skip_existing && to_path.exists() { println!( @@ -148,7 +150,7 @@ fn copy_files( fn transcode_file( file: &File, copy_args: &CopyCommandArgs, - config: TranscodeConfig, + config: &TranscodeConfig, is_threaded: bool, ) -> Result<(), Box> { let new_filename_full: String = match config.file_extension.clone() { @@ -158,14 +160,15 @@ fn transcode_file( } }; - let to_path_dest = PathBuf::from_str(copy_args.dest.as_str()).unwrap(); + let to_path_dest = PathBuf::from_str(copy_args.dest.as_str()).expect("invalid destination"); let to_path = match copy_args.single_directory { true => to_path_dest.join(new_filename_full), false => to_path_dest .join(file.path_from_source.clone()) .join(new_filename_full), }; - let to_path_string = to_path.as_os_str().to_str().unwrap().to_string(); + + let to_path_string = to_path.to_string_lossy(); if !file.extra_files.is_empty() { for extra_file in file.extra_files.iter() { @@ -178,11 +181,14 @@ fn transcode_file( "Skipping transcode for {} as file already exists", to_path_string ); + return Ok(()); } println!("Transcoding {}", to_path_string); - transcode(file.to_owned(), to_path_string.clone(), config, None)?; + + transcode(file.to_owned(), to_path_string.to_string(), config, None)?; + if is_threaded { println!("Finished Transcoding {}", to_path_string); } @@ -198,41 +204,38 @@ fn transcode_files( copy_args.transcode_preset.as_ref(), copy_args.transcode_config.as_ref(), ) - .unwrap(); + .expect("transcode config error"); - if copy_args.threads.is_some() && copy_args.threads.unwrap() > 1 { + let threads = copy_args.threads.unwrap_or(1); + + if threads > 1 { let files_copy = files.to_vec(); let jobs: Arc>> = Arc::new(Mutex::new(files_copy)); - let copy_args_arc = Arc::new(copy_args); - let transcode_config_arc = Arc::new(transcode_config); + + let copy_args = Arc::new(copy_args); + let transcode_config = Arc::new(transcode_config); scope(|s| { - for _ in 0..copy_args.threads.unwrap() { + for _ in 0..threads { s.spawn(|| loop { let mut jobs = jobs.lock().unwrap(); - let job = jobs.pop(); - if job.is_none() { - break; - } - let job = job.unwrap().clone(); - drop(jobs); - let result = transcode_file( - &job, - ©_args_arc, - transcode_config_arc.as_ref().clone(), - true, - ); - if result.is_err() { - panic!("Error Transcoding: {}", result.unwrap_err()) + let job = jobs.pop(); + if let Some(job) = job { + let result = transcode_file(&job, ©_args, &transcode_config, true); + if let Err(err) = result { + panic!("Error Transcoding: {}", err) + } + } else { + break; } }); } }); } else { for file in files.iter() { - transcode_file(file, copy_args, transcode_config.clone(), false)?; + transcode_file(file, copy_args, &transcode_config, false)?; } } diff --git a/src/commands/genhtml.rs b/src/commands/genhtml.rs index 9cbbdb7..383ca49 100644 --- a/src/commands/genhtml.rs +++ b/src/commands/genhtml.rs @@ -10,8 +10,9 @@ use std::io::Write; use html_escape::encode_text; use urlencoding::encode as url_encode; -fn table_for_files(files: Vec, includes_path: bool, link_base: Option) -> String { +fn table_for_files(files: Vec, includes_path: bool, link_base: &Option) -> String { let mut html_content = String::new(); + let mut path_head = String::new(); if includes_path { path_head.push_str("Path") @@ -51,6 +52,7 @@ fn table_for_files(files: Vec, includes_path: bool, link_base: Option, includes_path: bool, link_base: Option = file_path.iter().collect(); + for i in 0..(file_path.len()) { + let file_path_element = file_path.get(i).unwrap(); + url.push_str( - url_encode(file_path.get(i).unwrap().to_str().unwrap()) - .to_string() - .as_str(), + url_encode( + file_path_element + .to_str() + .expect("invalid character in filename"), + ) + .to_string() + .as_str(), ); if i != file_path.len() - 1 { url.push('/'); @@ -118,7 +127,7 @@ fn table_for_files(files: Vec, includes_path: bool, link_base: Option Result<(), Box> { println!("Scanning For Music"); @@ -171,20 +180,18 @@ pub fn genhtml_command( .as_str(), ); - html_content.push_str(&table_for_files( - files, - true, - genhtml_args.link_base.clone(), - )); + html_content.push_str(&table_for_files(files, true, &genhtml_args.link_base)); html_content.push_str(""); let file_path = std::path::PathBuf::from(genhtml_args.dest.as_str()).join("index.html"); let html_index_file = std::fs::File::create(file_path); + + match html_index_file { Ok(mut file) => match file.write_all(html_content.as_bytes()) { Ok(_) => {} Err(e) => { - panic!("Could not write html file: {}", e); + panic!("Could not write HTML file: {}", e); } }, Err(e) => { diff --git a/src/commands/get_tags.rs b/src/commands/get_tags.rs index 5536313..9dc1ff5 100644 --- a/src/commands/get_tags.rs +++ b/src/commands/get_tags.rs @@ -22,7 +22,7 @@ fn from_main_tags(tags: &crate::types::Tags) -> Tags { } pub fn get_tags_command( - _args: &CLIArgs, + _args: CLIArgs, get_tags_args: &GetTagsCommandArgs, ) -> Result<(), Box> { let mut files: Vec = Vec::new(); diff --git a/src/commands/process.rs b/src/commands/process.rs index cf1f11c..1b25031 100644 --- a/src/commands/process.rs +++ b/src/commands/process.rs @@ -10,7 +10,7 @@ use crate::utils::formats::get_format_handler; use crate::utils::replaygain::analyze_replaygain; use crate::utils::scan_for_music; -fn rename_file(_args: &CLIArgs, process_args: &ProcessCommandArgs, file: &mut File) { +fn rename_file(process_args: &ProcessCommandArgs, file: &mut File) { let title = &file.info.tags.title; let artist = &file.info.tags.artist; @@ -136,7 +136,7 @@ pub fn add_replaygain_tags(file: &File, force: bool) -> Result<(), Box Result<(), Box> { println!("Scanning For Music"); @@ -153,40 +153,40 @@ pub fn process_command( println!("Renaming Files"); for file in files.iter_mut() { - rename_file(args, process_args, file); + rename_file(process_args, file); } if !process_args.skip_replaygain && !process_args.dry_run { println!("Adding ReplayGain Tags to Files"); - if process_args.replaygain_threads.is_some() && process_args.replaygain_threads.unwrap() > 1 - { - let files_copy = files.to_vec(); + let threads = process_args.replaygain_threads.unwrap_or(0); - let jobs: Arc>> = Arc::new(Mutex::new(files_copy)); + if threads <= 1 { + for file in files.iter_mut() { + add_replaygain_tags(file, process_args.force_replaygain)?; + } + + return Ok(()); + } else { + let jobs: Arc>> = Arc::new(Mutex::new(files)); scope(|s| { - for _ in 0..process_args.replaygain_threads.unwrap() { + for _ in 0..threads { s.spawn(|| loop { let mut jobs = jobs.lock().unwrap(); - let job = jobs.pop(); - if job.is_none() { - break; - } - let job = job.unwrap().clone(); - drop(jobs); - let result = add_replaygain_tags(&job, process_args.force_replaygain); - if result.is_err() { - panic!("Error doing replaygain: {}", result.unwrap_err()) + let job = jobs.pop(); + if let Some(job) = job { + let result = add_replaygain_tags(&job, process_args.force_replaygain); + if let Err(err) = result { + panic!("Error doing replaygain: {}", err) + } + } else { + break; } }); } }); - } else { - for file in files.iter_mut() { - add_replaygain_tags(file, process_args.force_replaygain)?; - } } } diff --git a/src/commands/set_tags.rs b/src/commands/set_tags.rs index 001b003..4c2e365 100644 --- a/src/commands/set_tags.rs +++ b/src/commands/set_tags.rs @@ -6,7 +6,7 @@ use crate::types::File; use crate::utils::formats::get_format_handler; pub fn set_tags_command( - _args: &CLIArgs, + _args: CLIArgs, add_tags_args: &SetTagsCommandArgs, ) -> Result<(), Box> { let mut files: Vec = Vec::new(); diff --git a/src/commands/transcode.rs b/src/commands/transcode.rs index 14fd8df..23217c2 100644 --- a/src/commands/transcode.rs +++ b/src/commands/transcode.rs @@ -11,13 +11,9 @@ use crate::utils::transcoder::presets::transcode_preset_or_config; use crate::utils::transcoder::transcode; pub fn transcode_command( - _args: &CLIArgs, + _args: CLIArgs, transcode_args: &TranscodeCommandArgs, ) -> Result<(), Box> { - if transcode_args.transcode_config.is_none() && transcode_args.transcode_preset.is_none() { - panic!("Please provide Transcode Preset/Config"); - } - if let Some(preset) = &transcode_args.transcode_preset { if preset == "list" { print_presets(); @@ -25,19 +21,15 @@ pub fn transcode_command( } } - println!("Transcoding"); - let input_file = File::from_path("".to_string(), PathBuf::from(&transcode_args.source)); - let output_file = File::from_path("".to_string(), PathBuf::from(&transcode_args.dest)); - let transcode_config = transcode_preset_or_config( transcode_args.transcode_preset.as_ref(), transcode_args.transcode_config.as_ref(), - ); + ).expect("transcode config error"); - if transcode_config.is_err() { - panic!("Please provide Transcode Preset/Config"); - } - let transcode_config = transcode_config.unwrap(); + println!("Transcoding"); + + let input_file = File::from_path("".to_string(), PathBuf::from(&transcode_args.source)); + let output_file = File::from_path("".to_string(), PathBuf::from(&transcode_args.dest)); if !transcode_args.ignore_extension { if let Some(ref file_extension) = transcode_config.file_extension { @@ -69,7 +61,7 @@ pub fn transcode_command( transcode( input_file, transcode_args.dest.clone(), - transcode_config, + &transcode_config, Some(tx), )?; child.join().expect("oops! the child thread panicked"); diff --git a/src/main.rs b/src/main.rs index 8657666..3181006 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,29 +17,26 @@ use commands::transcode::transcode_command; fn main() -> Result<(), Box> { let cli = CLIArgs::parse(); - match cli.command.as_ref() { - Some(Commands::Process(process_args)) => { - process_command(&cli, process_args)?; + let command = cli.command.to_owned(); + + match command { + Commands::Process(process_args) => { + return process_command(cli, &process_args); } - Some(Commands::Genhtml(genhtml_args)) => { - genhtml_command(&cli, genhtml_args)?; + Commands::Genhtml(genhtml_args) => { + return genhtml_command(cli, &genhtml_args); } - Some(Commands::Transcode(transcode_args)) => { - transcode_command(&cli, transcode_args)?; + Commands::Transcode(transcode_args) => { + return transcode_command(cli, &transcode_args); } - Some(Commands::Copy(copy_args)) => { - copy_command(&cli, copy_args)?; + Commands::Copy(copy_args) => { + return copy_command(cli, ©_args); } - Some(Commands::SetTags(set_tags_args)) => { - set_tags_command(&cli, set_tags_args)?; + Commands::SetTags(set_tags_args) => { + return set_tags_command(cli, &set_tags_args); } - Some(Commands::GetTags(get_tags_args)) => { - get_tags_command(&cli, get_tags_args)?; - } - None => { - panic!("please provide a subcommand"); + Commands::GetTags(get_tags_args) => { + return get_tags_command(cli, &get_tags_args); } } - - Ok(()) } diff --git a/src/types.rs b/src/types.rs index 73caae6..9135a45 100644 --- a/src/types.rs +++ b/src/types.rs @@ -72,22 +72,24 @@ pub struct File { impl File { pub fn from_path(source_dir: String, full_path: PathBuf) -> File { let full_file_path = PathBuf::from(&source_dir).join(full_path); - let filename = full_file_path + + let filename_without_extension = full_file_path .file_stem() - .unwrap() - .to_str() - .unwrap() + .expect("filename invalid") + .to_string_lossy() .to_string(); let extension = full_file_path.extension(); let extension = if let Some(extension) = extension { - extension.to_str().unwrap().to_string() + extension.to_string_lossy().to_string() } else { "".to_string() }; - let path_from_src = full_file_path.strip_prefix(&source_dir).unwrap(); + let path_from_src = full_file_path + .strip_prefix(&source_dir) + .expect("couldn't get path relative to source"); let mut folder_path_from_src = path_from_src.to_path_buf(); folder_path_from_src.pop(); @@ -95,7 +97,7 @@ impl File { let path_to = PathBuf::from(&source_dir).join(&folder_path_from_src); File { - filename, + filename: filename_without_extension, extension, path_from_source: folder_path_from_src, path_to, diff --git a/src/utils/ascii_reduce/mod.rs b/src/utils/ascii_reduce/mod.rs index 4e1444e..24ddc6d 100644 --- a/src/utils/ascii_reduce/mod.rs +++ b/src/utils/ascii_reduce/mod.rs @@ -5,16 +5,16 @@ const MAPPINGS_DATA: &str = include_str!("mappings.json"); lazy_static! { static ref MAPPINGS: HashMap = { - let data: HashMap = serde_json::from_str(MAPPINGS_DATA).unwrap(); + let data: HashMap = + serde_json::from_str(MAPPINGS_DATA).expect("mapping data invalid"); let mut replacement_map: HashMap = HashMap::new(); for (chr, repl) in &data { match chr.parse::() { Ok(n) => { - let b = char::from_u32(n); - if b.is_some() { - replacement_map.insert(b.unwrap(), repl.to_string()); - } + let b = char::from_u32(n).expect("invalid char in string"); + + replacement_map.insert(b, repl.to_string()); } Err(e) => { panic!( @@ -41,13 +41,8 @@ pub fn reduce_to_ascii(input: String) -> String { continue; } - match MAPPINGS.get(&c) { - Some(replacement) => { - output.push_str(replacement); - } - None => { - output.push_str(""); - } + if let Some(replacement) = MAPPINGS.get(&c) { + output.push_str(replacement); } } diff --git a/src/utils/format_detection.rs b/src/utils/format_detection.rs index e3fab2c..e474aed 100644 --- a/src/utils/format_detection.rs +++ b/src/utils/format_detection.rs @@ -43,6 +43,7 @@ impl ToString for FileFormat { pub fn detect_ogg_subformat(path: &Path) -> Result { let file = File::open(path); + if let Err(_error) = file { return Err(FormatDetectionError::FileReadError); } diff --git a/src/utils/formats/flac.rs b/src/utils/formats/flac.rs index 5fe6883..83f7cd2 100644 --- a/src/utils/formats/flac.rs +++ b/src/utils/formats/flac.rs @@ -98,12 +98,12 @@ impl AudioContainerFormat for FLACAudioFormat { &mut self, allow_missing_tags: bool, ) -> Result { - return Ok(AudioFileInfo { + Ok(AudioFileInfo { tags: self.get_tags(allow_missing_tags)?, contains_replaygain: self.contains_replaygain_tags(), supports_replaygain: self.supports_replaygain(), format: Some(FileFormat::FLAC), - }); + }) } } diff --git a/src/utils/formats/generic_ffmpeg.rs b/src/utils/formats/generic_ffmpeg.rs index f4de27c..efd92a8 100644 --- a/src/utils/formats/generic_ffmpeg.rs +++ b/src/utils/formats/generic_ffmpeg.rs @@ -165,6 +165,13 @@ impl AudioContainerFormat for GenericFFMpegAudioFormat { } fn save_changes(&mut self) -> Result<(), BoxedError> { + if self.changes.title.is_none() + && self.changes.artist.is_none() + && self.changes.replaygain_data.is_none() + { + return Ok(()); + } + let mut args: Vec = Vec::new(); let tempdir = tempfile::tempdir()?; @@ -179,15 +186,18 @@ impl AudioContainerFormat for GenericFFMpegAudioFormat { args.extend(vec!["-c".to_string(), "copy".to_string()]); - if let Some(title) = self.changes.title.clone() { + if let Some(title) = &self.changes.title { args.extend(vec![ "-metadata".to_string(), - format!("title=\"{}\"", title), + format!("title=\"{}\"", title.as_str()), ]) } - if let Some(artist) = self.changes.artist.clone() { - args.extend(vec!["-metadata".to_string(), format!("artist={}", artist)]) + if let Some(artist) = &self.changes.artist { + args.extend(vec![ + "-metadata".to_string(), + format!("artist={}", artist.as_str()), + ]) } args.push(temp_file.to_string_lossy().to_string()); @@ -205,12 +215,12 @@ impl AudioContainerFormat for GenericFFMpegAudioFormat { &mut self, allow_missing_tags: bool, ) -> Result { - return Ok(AudioFileInfo { + Ok(AudioFileInfo { tags: self.get_tags(allow_missing_tags)?, contains_replaygain: self.contains_replaygain_tags(), supports_replaygain: self.supports_replaygain(), format: Some(self.format_type), - }); + }) } } diff --git a/src/utils/formats/generic_taglib.rs b/src/utils/formats/generic_taglib.rs index 6066265..878a7bc 100644 --- a/src/utils/formats/generic_taglib.rs +++ b/src/utils/formats/generic_taglib.rs @@ -134,12 +134,12 @@ impl AudioContainerFormat for TaglibAudioFormat { &mut self, allow_missing_tags: bool, ) -> Result { - return Ok(AudioFileInfo { + Ok(AudioFileInfo { tags: self.get_tags(allow_missing_tags)?, contains_replaygain: self.contains_replaygain_tags(), supports_replaygain: self.supports_replaygain(), format: self.file_format, - }); + }) } } diff --git a/src/utils/formats/id3.rs b/src/utils/formats/id3.rs index ecc81b3..692025a 100644 --- a/src/utils/formats/id3.rs +++ b/src/utils/formats/id3.rs @@ -115,12 +115,12 @@ impl AudioContainerFormat for ID3AudioFormat { &mut self, allow_missing_tags: bool, ) -> Result { - return Ok(AudioFileInfo { + Ok(AudioFileInfo { tags: self.get_tags(allow_missing_tags)?, supports_replaygain: self.supports_replaygain(), format: Some(FileFormat::MP3), contains_replaygain: self.contains_replaygain_tags(), - }); + }) } } diff --git a/src/utils/formats/mod.rs b/src/utils/formats/mod.rs index cdbcd6c..b235ea9 100644 --- a/src/utils/formats/mod.rs +++ b/src/utils/formats/mod.rs @@ -84,6 +84,7 @@ pub fn get_format_handler(file: &File) -> Result, fn is_supported_extension(file_path: &Path) -> bool { let ext = file_path.extension(); + if ext.is_none() { return false; } diff --git a/src/utils/music_scanner.rs b/src/utils/music_scanner.rs index 3872adf..4480a5c 100644 --- a/src/utils/music_scanner.rs +++ b/src/utils/music_scanner.rs @@ -36,7 +36,7 @@ pub fn find_extra_files( pub fn scan_for_music(src_dir: &String) -> Result, Box> { let mut files: Vec = Vec::new(); - for entry in WalkDir::new(&src_dir) { + for entry in WalkDir::new(src_dir) { let entry = entry.unwrap(); let entry_path = entry.into_path(); if entry_path.is_dir() { diff --git a/src/utils/transcoder/progress_monitor.rs b/src/utils/transcoder/progress_monitor.rs index c737a4d..273d787 100644 --- a/src/utils/transcoder/progress_monitor.rs +++ b/src/utils/transcoder/progress_monitor.rs @@ -84,8 +84,7 @@ pub fn progress_monitor( let mut watcher = RecommendedWatcher::new( tx, notify::Config::default().with_poll_interval(Duration::from_millis(100)), - ) - .expect("could not watch for ffmpeg log progress status"); + ).expect("could not watch for ffmpeg log progress status"); watcher .watch(&file_path, RecursiveMode::NonRecursive) diff --git a/src/utils/transcoder/transcoder.rs b/src/utils/transcoder/transcoder.rs index ac5d7a4..4a3603c 100644 --- a/src/utils/transcoder/transcoder.rs +++ b/src/utils/transcoder/transcoder.rs @@ -8,7 +8,7 @@ use super::{progress_monitor, types::TranscodeConfig}; pub fn transcode( file: File, dest: String, - config: TranscodeConfig, + config: &TranscodeConfig, progress_sender: Option>, ) -> Result<(), Box> { let mut command_args: Vec = Vec::new(); @@ -19,28 +19,28 @@ pub fn transcode( file.join_path_to().to_string_lossy().to_string(), ]); - if let Some(encoder) = config.encoder { - command_args.extend(vec!["-c:a".to_string(), encoder]); + if let Some(encoder) = &config.encoder { + command_args.extend(vec!["-c:a".to_string(), encoder.to_string()]); } - if let Some(container) = config.container { - command_args.extend(vec!["-f".to_string(), container]); + if let Some(container) = &config.container { + command_args.extend(vec!["-f".to_string(), container.to_string()]); } - if let Some(sample_rate) = config.sample_rate { - command_args.extend(vec!["-ar".to_string(), sample_rate]); + if let Some(sample_rate) = &config.sample_rate { + command_args.extend(vec!["-ar".to_string(), sample_rate.to_string()]); } - if let Some(channels) = config.channels { - command_args.extend(vec!["-ac".to_string(), channels]); + if let Some(channels) = &config.channels { + command_args.extend(vec!["-ac".to_string(), channels.to_string()]); } - if let Some(quality) = config.quality { - command_args.extend(vec!["-q:a".to_string(), quality]); + if let Some(quality) = &config.quality { + command_args.extend(vec!["-q:a".to_string(), quality.to_string()]); } - if let Some(bitrate) = config.bitrate { - command_args.extend(vec!["-b:a".to_string(), bitrate]); + if let Some(bitrate) = &config.bitrate { + command_args.extend(vec!["-b:a".to_string(), bitrate.to_string()]); } command_args.push(dest); @@ -48,9 +48,9 @@ pub fn transcode( let mut progress_thread: Option> = None; let mut progress_file: Option = None; - if progress_sender.is_some() { - let sender = progress_sender.as_ref().unwrap(); + if let Some(sender) = &progress_sender { let result = progress_monitor(file.join_path_to(), sender); + if let Ok(result) = result { progress_thread = Some(result.1); progress_file = Some(result.0.clone());