diff --git a/src/model/metadata.rs b/src/model/metadata.rs index 25ebf79..335df67 100644 --- a/src/model/metadata.rs +++ b/src/model/metadata.rs @@ -15,7 +15,7 @@ pub enum Metadata { invalid: bool, hash: Arc, size: Arc, - tag: Arc>, + tag: Option>, artist: Option>, album: Option>, track: Option, @@ -66,7 +66,7 @@ impl Metadata { key: None, bpm: None, invalid: false, - tag: tag.map(|t|t.clone()).into(), + tag: tag.map(|t|t.clone().into()), }) } else { Self::new_fallback(path) @@ -130,38 +130,37 @@ impl Metadata { _ => None } } - pub fn artist (&self) -> Option> { - match self { - Metadata::Music { artist, .. } => artist.clone(), - _ => None - } - } - pub fn year (&self) -> Option> { - match self { - Metadata::Music { year, .. } => year.map(|t|format!("{t}").into()).clone(), - _ => None - } - } - pub fn album (&self) -> Option> { - match self { - Metadata::Music { album, .. } => album.clone(), - _ => None - } - } - pub fn title (&self) -> Option> { - match self { - Metadata::Music { title, .. } => title.clone(), - _ => None - } - } - pub fn track (&self) -> Option> { - match self { - Metadata::Music { track, .. } => track.map(|t|format!("{t}").into()).clone(), - _ => None +} + +macro_rules! metadata_get_string { + ($name:ident) => { + impl Metadata { + pub fn $name (&self) -> Option> { + match self { + Metadata::Music { tag: Some(tag), .. } => + tag.$name().map(|t|t.into()), + _ => None + } + } } } } +macro_rules! metadata_get_number { + ($name:ident) => { + impl Metadata { + pub fn $name (&self) -> Option> { + match self { + Metadata::Music { tag: Some(tag), .. } => + tag.$name().map(|t|format!("{t}").into()), + _ => None + } + } + } + } +} + + macro_rules! metadata_set_string { ($name:ident, $field:ident, $key:expr) => { impl Metadata { @@ -195,8 +194,12 @@ macro_rules! metadata_set_number { } } -metadata_set_string!(set_artist, artist, ItemKey::TrackArtist); -metadata_set_number!(set_year, year, ItemKey::Year); -metadata_set_string!(set_album, album, ItemKey::AlbumTitle); -metadata_set_number!(set_track, track, ItemKey::TrackNumber); -metadata_set_string!(set_title, title, ItemKey::TrackTitle); +metadata_get_string!(artist); metadata_set_string!(set_artist, artist, ItemKey::TrackArtist); + +metadata_get_number!(year); metadata_set_number!(set_year, year, ItemKey::Year); + +metadata_get_string!(album); metadata_set_string!(set_album, album, ItemKey::AlbumTitle); + +metadata_get_number!(track); metadata_set_number!(set_track, track, ItemKey::TrackNumber); + +metadata_get_string!(title); metadata_set_string!(set_title, title, ItemKey::TrackTitle);