Commit Diff


commit - 1701ad61c5a4e80e10cf115fa88ca1589909893d
commit + 1a2fc99e702712236695b83983e00b20337a5ecb
blob - 982457c87913165949952018259198e0cbae9a7d
blob + 59c5439655e2e8da66ba67affc886bab81bab266
--- src/main.rs
+++ 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,29 +16,8 @@ use crate::db::LibraryDb;
 use crate::db::TrackReport;
 use crate::file_io::process_file;
 use crate::types::TrackError;
+use crate::utils::{is_supported_audio_file, print_entries};
 
-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());
-    }
-}
-
 #[allow(clippy::too_many_lines)]
 fn main() -> Result<()> {
     if std::env::args().len() == 1 {
@@ -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() {
blob - /dev/null
blob + c7099a21de605756414c62a40ac81b2560955821 (mode 644)
--- /dev/null
+++ 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());
+    }
+}