From e19e0bfe77f1fc9a7b4f01e9ac77615ab47748ec Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 12 Apr 2025 14:59:07 -0400 Subject: [PATCH] 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); } } - */ } }