First round of refactoring

This commit is contained in:
2025-04-18 21:17:14 -04:00
parent c80bc7d7e1
commit fb9a790935
2 changed files with 101 additions and 87 deletions

View File

@@ -1,20 +1,5 @@
use lofty::{file::AudioFile, tag::Accessor}; use lofty::{file::AudioFile, tag::Accessor};
use crate::types;
fn get_type(t: types::Type) -> Result<String, std::io::Error> {
match t {
types::Type::Title => Ok("TITLE".to_owned()),
types::Type::Artist => Ok("ARTIST".to_owned()),
types::Type::Album => Ok("ALBUM".to_owned()),
types::Type::AlbumArtist => Ok("ALBUMARTIST".to_owned()),
types::Type::Genre => Ok("GENRE".to_owned()),
types::Type::Date => Ok("DATE".to_owned()),
types::Type::Track => Ok("TRACKNUMBER".to_owned()),
types::Type::Disc => Ok("DISCNUMBER".to_owned()),
}
}
pub mod coverart { pub mod coverart {
use lofty::{file::AudioFile, ogg::OggPictureStorage}; use lofty::{file::AudioFile, ogg::OggPictureStorage};
@@ -151,90 +136,100 @@ pub mod coverart {
} }
} }
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> { pub mod metadata {
match std::fs::File::open(filepath) { use crate::types;
Ok(mut content) => { use lofty::file::AudioFile;
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new()) use lofty::tag::Accessor;
{
Ok(flac_file) => match flac_file.vorbis_comments() { pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
Some(vb) => { match std::fs::File::open(filepath) {
let type_str: String = get_type(t).unwrap(); Ok(mut content) => {
match vb.get(&type_str) { match lofty::flac::FlacFile::read_from(
Some(val) => Ok(val.to_owned()), &mut content,
None => Err(std::io::Error::new( lofty::config::ParseOptions::new(),
std::io::ErrorKind::InvalidData, ) {
"Could not get tag data", Ok(flac_file) => match flac_file.vorbis_comments() {
)), Some(vb) => {
let type_str: String = types::o::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(
None => Err(std::io::Error::new( std::io::ErrorKind::InvalidData,
"No tags found",
)),
},
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
"No tags found", err.to_string(),
)), )),
}, }
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
)),
} }
Err(err) => Err(err),
} }
Err(err) => Err(err),
} }
}
pub fn set_meta( pub fn set_meta(
t: types::Type, t: types::Type,
filepath: &String, filepath: &String,
value: &String, value: &String,
) -> Result<String, std::io::Error> { ) -> Result<String, std::io::Error> {
match std::fs::File::open(filepath) { match std::fs::File::open(filepath) {
Ok(mut content) => { Ok(mut content) => {
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new()) match lofty::flac::FlacFile::read_from(
{ &mut content,
Ok(mut flac_file) => match flac_file.vorbis_comments_mut() { lofty::config::ParseOptions::new(),
Some(vb) => { ) {
let pre_value = value.clone(); Ok(mut flac_file) => match flac_file.vorbis_comments_mut() {
match t { Some(vb) => {
types::Type::Album => { let pre_value = value.clone();
vb.set_album(pre_value); match t {
} types::Type::Album => {
types::Type::AlbumArtist => { vb.set_album(pre_value);
vb.insert(get_type(t).unwrap(), pre_value); }
} types::Type::AlbumArtist => {
types::Type::Artist => { vb.insert(types::o::get_type(t).unwrap(), pre_value);
vb.set_artist(pre_value); }
} types::Type::Artist => {
types::Type::Date => { vb.set_artist(pre_value);
vb.insert(get_type(t).unwrap(), pre_value); }
} types::Type::Date => {
types::Type::Disc => { vb.insert(types::o::get_type(t).unwrap(), pre_value);
vb.set_disk(pre_value.parse().unwrap()); }
} types::Type::Disc => {
types::Type::Genre => { vb.set_disk(pre_value.parse().unwrap());
vb.set_genre(pre_value); }
} types::Type::Genre => {
types::Type::Title => { vb.set_genre(pre_value);
vb.set_title(pre_value); }
} types::Type::Title => {
types::Type::Track => { vb.set_title(pre_value);
vb.set_track(pre_value.parse().unwrap()); }
} types::Type::Track => {
}; vb.set_track(pre_value.parse().unwrap());
}
};
Ok(value.to_owned()) Ok(value.to_owned())
} }
None => Err(std::io::Error::new( None => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"No tags found",
)),
},
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
"No tags found", err.to_string(),
)), )),
}, }
Err(err) => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
)),
} }
Err(err) => Err(err),
} }
Err(err) => Err(err),
} }
} }
@@ -317,6 +312,7 @@ mod tests {
mod get { mod get {
use super::*; use super::*;
use crate::types; use crate::types;
use super::metadata::{get_meta, set_meta};
#[test] #[test]
fn test_get_title() { fn test_get_title() {
@@ -520,6 +516,8 @@ mod tests {
mod set { mod set {
use super::*; use super::*;
use crate::types;
use super::metadata::{get_meta, set_meta};
#[test] #[test]
fn test_set_title() { fn test_set_title() {

View File

@@ -8,3 +8,19 @@ pub enum Type {
Track, Track,
Disc, Disc,
} }
pub mod o {
pub fn get_type(t: super::Type) -> Result<String, std::io::Error> {
match t {
super::Type::Title => Ok("TITLE".to_owned()),
super::Type::Artist => Ok("ARTIST".to_owned()),
super::Type::Album => Ok("ALBUM".to_owned()),
super::Type::AlbumArtist => Ok("ALBUMARTIST".to_owned()),
super::Type::Genre => Ok("GENRE".to_owned()),
super::Type::Date => Ok("DATE".to_owned()),
super::Type::Track => Ok("TRACKNUMBER".to_owned()),
super::Type::Disc => Ok("DISCNUMBER".to_owned()),
}
}
}