From 9032112e197df4126f3b4fe3c8a7e033c43b9205 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 14:32:39 -0400 Subject: [PATCH 1/6] Added lofty dependency --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 9971259..7782f1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ rust-version = "1.86" [dependencies] metadata = { version = "0.1.10" } +lofty = { version = "0.22.3" } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" } -- 2.43.0 From e17079e72077609465a6f5affc0146682a9c835d Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 14:34:34 -0400 Subject: [PATCH 2/6] Making way for different dependency --- src/lib.rs | 83 +++--------------------------------------------------- 1 file changed, 4 insertions(+), 79 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a2d6e3..15f3b44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,85 +9,8 @@ pub mod meta_type { } } -pub mod meta_nouveaou { +pub mod meta_next { use super::*; - - pub fn get_meta(t: meta_type::Type, filepath: &String) -> Result { - match t { - meta_type::Type::Title => match new_meta(filepath) { - Ok(metaa) => match get_val(t, metaa.tags) { - Ok(val) => Ok(val), - Err(err) => Err(err), - }, - Err(err) => Err(err), - }, - meta_type::Type::Artist => match new_meta(filepath) { - Ok(metaa) => match get_val(t, metaa.tags) { - Ok(val) => Ok(val), - Err(err) => Err(err), - }, - Err(err) => Err(err), - }, - meta_type::Type::Album => match new_meta(filepath) { - Ok(metaa) => match get_val(t, metaa.tags) { - Ok(val) => Ok(val), - Err(err) => Err(err), - }, - Err(err) => Err(err), - }, - meta_type::Type::Genre => match new_meta(filepath) { - Ok(metaa) => match get_val(t, metaa.tags) { - Ok(val) => Ok(val), - Err(err) => Err(err), - }, - Err(err) => Err(err), - }, - meta_type::Type::Year => match new_meta(filepath) { - Ok(metaa) => match get_val(t, metaa.tags) { - Ok(val) => Ok(val), - Err(err) => Err(err), - }, - Err(err) => Err(err), - }, - meta_type::Type::Track => match new_meta(filepath) { - Ok(metaa) => match get_val(t, metaa.tags) { - Ok(val) => Ok(val), - Err(err) => Err(err), - }, - Err(err) => Err(err), - }, - } - } - - fn get_type(t: meta_type::Type) -> Result { - match t { - meta_type::Type::Title => Ok("TITLE".to_string()), - meta_type::Type::Artist => Ok("ARTIST".to_string()), - meta_type::Type::Album => Ok("".to_string()), - meta_type::Type::Genre => Ok("".to_string()), - meta_type::Type::Year => Ok("".to_string()), - meta_type::Type::Track => Ok("".to_string()), - } - } - - fn get_val(t: meta_type::Type, tags: Vec<(String, String)>) -> Result { - let type_ma: String = get_type(t).unwrap(); - for tag in tags { - if tag.0 == type_ma { - return Ok(tag.1); - } - } - - Err(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - "Invalid", - )) - } - - fn new_meta(filepath: &String) -> Result { - let path = std::path::Path::new(&filepath); - metadata::MediaFileMetadata::new(&path) - } } #[cfg(test)] @@ -104,8 +27,9 @@ mod tests { println!("Path: {:?}", full_path); assert!(full_path.exists(), "Path does not exists {:?}", full_path); - let filepath = full_path.display().to_string(); + let _filepath = full_path.display().to_string(); + /* match meta_nouveaou::get_meta(meta_type::Type::Title, &filepath) { Ok(title) => { let found = title == "Just roll it"; @@ -115,5 +39,6 @@ mod tests { assert!(false, "Error: {:?}", err); } } + */ } } -- 2.43.0 From 72d102f3905b5261845db010263f064504a6c534 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 14:34:59 -0400 Subject: [PATCH 3/6] Removed metadata dependency --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7782f1c..60c2c90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,5 @@ edition = "2024" rust-version = "1.86" [dependencies] -metadata = { version = "0.1.10" } lofty = { version = "0.22.3" } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" } -- 2.43.0 From e19e0bfe77f1fc9a7b4f01e9ac77615ab47748ec Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 14:59:07 -0400 Subject: [PATCH 4/6] Got it working --- src/lib.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 15f3b44..9c045db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,53 @@ pub mod meta_type { } pub mod meta_next { + use lofty::file::AudioFile; + use super::*; + + fn get_type(t: meta_type::Type) -> Result { + match t { + meta_type::Type::Title => Ok("TITLE".to_owned()), + meta_type::Type::Artist => Ok("ARTIST".to_owned()), + meta_type::Type::Album => Ok("ALBUM".to_owned()), + meta_type::Type::Genre => Ok("GENRE".to_owned()), + meta_type::Type::Year => Ok("YEAR".to_owned()), + meta_type::Type::Track => Ok("TRACK".to_owned()), + } + } + + pub fn get_meta(t: meta_type::Type, filepath: &String) -> Result { + match std::fs::File::open(filepath) { + Ok(mut content) => { + match lofty::flac::FlacFile::read_from( + &mut content, + lofty::config::ParseOptions::new(), + ) { + Ok(flac_file) => match flac_file.vorbis_comments() { + Some(vb) => { + let type_str: String = get_type(t).unwrap(); + match vb.get(&type_str) { + Some(val) => Ok(val.to_owned()), + None => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Could not get tag data", + )), + } + } + None => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + "No tags found", + )), + }, + Err(err) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + err.to_string(), + )), + } + } + Err(err) => Err(err), + } + } } #[cfg(test)] @@ -27,10 +73,9 @@ mod tests { println!("Path: {:?}", full_path); assert!(full_path.exists(), "Path does not exists {:?}", full_path); - let _filepath = full_path.display().to_string(); + let filepath = full_path.display().to_string(); - /* - match meta_nouveaou::get_meta(meta_type::Type::Title, &filepath) { + match meta_next::get_meta(meta_type::Type::Title, &filepath) { Ok(title) => { let found = title == "Just roll it"; assert!(found, "Meta information was not found {:?}", title); @@ -39,6 +84,5 @@ mod tests { assert!(false, "Error: {:?}", err); } } - */ } } -- 2.43.0 From 8f2f88669d8700657c9def4b7dbefac9b6a16979 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 15:14:59 -0400 Subject: [PATCH 5/6] Refactor --- src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9c045db..d7ba937 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,28 +61,65 @@ pub mod meta_next { #[cfg(test)] mod tests { + use util::{file_exists, get_full_path}; + use super::*; + mod util { + pub fn get_full_path( + directory: &String, + filename: &String, + ) -> Result { + match path_buf(directory, filename) { + Ok(pf) => Ok(pf.display().to_string()), + Err(err) => Err(err), + } + } + + pub fn file_exists(directory: &String, filename: &String) -> Result { + match path_buf(directory, filename) { + Ok(pf) => Ok(pf.exists()), + Err(err) => Err(err), + } + } + + fn path_buf( + directory: &String, + filename: &String, + ) -> Result { + let dir_path = std::path::Path::new(&directory); + Ok(dir_path.join(filename)) + } + } + #[test] fn test_get_title() { let filename = String::from("track01.flac"); let dir = String::from("tests/sample_tracks3"); - let dir_path = std::path::Path::new(&dir); - let full_path = dir_path.join(filename); + // let dir_path = std::path::Path::new(&dir); + // let full_path = dir_path.join(filename); - println!("Path: {:?}", full_path); + // println!("Path: {:?}", full_path); - assert!(full_path.exists(), "Path does not exists {:?}", full_path); - let filepath = full_path.display().to_string(); + // assert!(full_path.exists(), "Path does not exists {:?}", full_path); + match file_exists(&dir, &filename) { + Ok(_) => { + // let filepath = full_path.display().to_string(); + let filepath = get_full_path(&dir, &filename).unwrap(); - match meta_next::get_meta(meta_type::Type::Title, &filepath) { - Ok(title) => { - let found = title == "Just roll it"; - assert!(found, "Meta information was not found {:?}", title); + match meta_next::get_meta(meta_type::Type::Title, &filepath) { + Ok(title) => { + let found = title == "Just roll it"; + assert!(found, "Meta information was not found {:?}", title); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } } Err(err) => { - assert!(false, "Error: {:?}", err); + assert!(false, "Error: File does not exist {:?}", err.to_string()); } - } + }; } } -- 2.43.0 From a30445f634ba9c5463a39b2e29f64fe066e05308 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 15:17:20 -0400 Subject: [PATCH 6/6] Refactor --- src/lib.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d7ba937..e09f795 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -90,21 +90,19 @@ mod tests { let dir_path = std::path::Path::new(&directory); Ok(dir_path.join(filename)) } + + pub fn test_file_directory() -> String { + String::from("tests/sample_tracks3") + } } #[test] fn test_get_title() { let filename = String::from("track01.flac"); - let dir = String::from("tests/sample_tracks3"); - // let dir_path = std::path::Path::new(&dir); - // let full_path = dir_path.join(filename); + let dir = util::test_file_directory(); - // println!("Path: {:?}", full_path); - - // assert!(full_path.exists(), "Path does not exists {:?}", full_path); match file_exists(&dir, &filename) { Ok(_) => { - // let filepath = full_path.display().to_string(); let filepath = get_full_path(&dir, &filename).unwrap(); match meta_next::get_meta(meta_type::Type::Title, &filepath) { -- 2.43.0