virittaa

Log | Files | Refs | README | LICENSE

commit 1a2fc99e702712236695b83983e00b20337a5ecb
parent 1701ad61c5a4e80e10cf115fa88ca1589909893d
Author: mtmn <miro@haravara.org>
Date:   Thu, 14 May 2026 12:10:46 +0200

feat: move helpers into utils.rs

Diffstat:
Msrc/main.rs | 62++++++++++++++++++++++++++++----------------------------------
Asrc/utils.rs | 24++++++++++++++++++++++++
2 files changed, 52 insertions(+), 34 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -3,6 +3,7 @@ pub mod constants; pub mod db; pub mod file_io; pub mod types; +pub mod utils; use anyhow::Result; use clap::{Arg, Command}; @@ -15,28 +16,7 @@ use crate::db::LibraryDb; use crate::db::TrackReport; use crate::file_io::process_file; use crate::types::TrackError; - -use crate::constants::SUPPORTED_EXTENSIONS; - -fn is_supported_audio_file(path: &std::path::Path) -> bool { - path.is_file() && path.extension().is_some_and(is_supported_extension) -} - -fn is_supported_extension(ext: &std::ffi::OsStr) -> bool { - let ext_lower = ext.to_string_lossy().to_lowercase(); - SUPPORTED_EXTENSIONS.contains(&*ext_lower) -} - -fn print_entries(entries: &[(String, TrackReport)], empty_msg: &str, count_suffix: &str) { - if entries.is_empty() { - eprintln!("{empty_msg}"); - } else { - for (key, report) in entries { - println!("{key}: {report}"); - } - println!("\n{} {count_suffix}", entries.len()); - } -} +use crate::utils::{is_supported_audio_file, print_entries}; #[allow(clippy::too_many_lines)] fn main() -> Result<()> { @@ -136,14 +116,12 @@ fn main() -> Result<()> { let search_pattern = matches.get_one::<String>("search"); let artist_prefix = matches.get_one::<String>("artist").map(String::as_str); let key_prefix = matches.get_one::<String>("key").map(String::as_str); - let bpm_range: Option<(f64, f64)> = matches - .get_many::<String>("bpm") - .map(|vals| { - let v: Vec<&String> = vals.collect(); - let min = v[0].parse::<f64>().expect("Invalid BPM min value"); - let max = v[1].parse::<f64>().expect("Invalid BPM max value"); - (min, max) - }); + let bpm_range: Option<(f64, f64)> = matches.get_many::<String>("bpm").map(|vals| { + let v: Vec<&String> = vals.collect(); + let min = v[0].parse::<f64>().expect("Invalid BPM min value"); + let max = v[1].parse::<f64>().expect("Invalid BPM max value"); + (min, max) + }); let db_path = if let Some(p) = matches.get_one::<String>("db-path") { p.clone() } else { @@ -170,13 +148,25 @@ fn main() -> Result<()> { } } else if let Some(prefix) = artist_prefix { let entries = db.find_by_artist(prefix)?; - print_entries(&entries, &format!("No tracks found for artist prefix: {prefix}"), "track(s) found."); + print_entries( + &entries, + &format!("No tracks found for artist prefix: {prefix}"), + "track(s) found.", + ); } else if let Some(prefix) = key_prefix { let entries = db.find_by_key(prefix)?; - print_entries(&entries, &format!("No tracks found for key prefix: {prefix}"), "track(s) found."); + print_entries( + &entries, + &format!("No tracks found for key prefix: {prefix}"), + "track(s) found.", + ); } else if let Some((min, max)) = bpm_range { let entries = db.find_by_bpm_range(min, max)?; - print_entries(&entries, &format!("No tracks found with BPM between {min} and {max}"), "track(s) found."); + print_entries( + &entries, + &format!("No tracks found with BPM between {min} and {max}"), + "track(s) found.", + ); } else if let Some(pattern) = search_pattern { let re = Regex::new(pattern)?; let entries = db.list()?; @@ -184,7 +174,11 @@ fn main() -> Result<()> { .into_iter() .filter(|(key, report)| re.is_match(key) || re.is_match(&report.to_string())) .collect(); - print_entries(&matches_found, &format!("No matches for: {pattern}"), "match(es) found."); + print_entries( + &matches_found, + &format!("No matches for: {pattern}"), + "match(es) found.", + ); } else { let entries = db.list()?; if entries.is_empty() { diff --git a/src/utils.rs b/src/utils.rs @@ -0,0 +1,24 @@ +use crate::constants::SUPPORTED_EXTENSIONS; +use crate::db::TrackReport; +use std::ffi::OsStr; +use std::path::Path; + +pub fn is_supported_audio_file(path: &Path) -> bool { + path.is_file() && path.extension().is_some_and(is_supported_extension) +} + +pub fn is_supported_extension(ext: &OsStr) -> bool { + let ext_lower = ext.to_string_lossy().to_lowercase(); + SUPPORTED_EXTENSIONS.contains(&*ext_lower) +} + +pub fn print_entries(entries: &[(String, TrackReport)], empty_msg: &str, count_suffix: &str) { + if entries.is_empty() { + eprintln!("{empty_msg}"); + } else { + for (key, report) in entries { + println!("{key}: {report}"); + } + println!("\n{} {count_suffix}", entries.len()); + } +}