diff --git a/src/model/metadata.rs b/src/model/metadata.rs index 335df67..702a1d0 100644 --- a/src/model/metadata.rs +++ b/src/model/metadata.rs @@ -1,6 +1,7 @@ use crate::*; use std::fs::File; use std::io::{BufReader, Read}; +use std::borrow::Borrow; use byte_unit::{Byte, Unit::MB}; use lofty::{file::TaggedFileExt, probe::Probe, tag::{Accessor, Tag, TagItem, ItemKey, ItemValue}}; @@ -16,16 +17,7 @@ pub enum Metadata { hash: Arc, size: Arc, tag: Option>, - artist: Option>, - album: Option>, - track: Option, - title: Option>, - date: Option>, - year: Option, - people: Option>>, - publisher: Option>, - key: Option>, - bpm: Option>, + new_tag: Option>, }, Image { invalid: bool, @@ -54,19 +46,10 @@ impl Metadata { let size = Byte::from_u64(data.len() as u64).get_adjusted_unit(MB); Ok(Self::Music { hash, - size: format!("{:#>8.2}", size).into(), - artist: tag.map(|t|t.artist().map(|t|t.into())).flatten(), - album: tag.map(|t|t.album().map(|t|t.into())).flatten(), - track: tag.map(|t|t.track().map(|t|t.into())).flatten(), - title: tag.map(|t|t.title().map(|t|t.into())).flatten(), - year: tag.map(|t|t.year().map(|t|t.into())).flatten(), - date: None, - people: None, - publisher: None, - key: None, - bpm: None, - invalid: false, - tag: tag.map(|t|t.clone().into()), + size: format!("{:#>8.2}", size).into(), + tag: tag.map(|t|t.clone().into()), + new_tag: tag.map(|t|t.clone().into()), + invalid: false, }) } else { Self::new_fallback(path) @@ -132,74 +115,77 @@ impl Metadata { } } -macro_rules! metadata_get_string { - ($name:ident) => { +macro_rules! metadata_field { + (string: $get:ident $set:ident $key:expr) => { 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 { - pub fn $name (&mut self, value: &impl AsRef) -> Option { - if let Metadata::Music { $field, .. } = self - && Some(value.as_ref()) != $field.as_deref() - { - *$field = Some(value.as_ref().into()); - return Some(TagItem::new($key, ItemValue::Text(value.as_ref().into()))) + pub fn $get (&self) -> Option> { + if let Metadata::Music { tag: Some(tag), .. } = self { + return tag.$get().map(|t|t.into()) } None } - } - } -} - -macro_rules! metadata_set_number { - ($name:ident, $field:ident, $key:expr) => { - impl Metadata { - pub fn $name (&mut self, value: &impl AsRef) -> Option { - if let Metadata::Music { $field, .. } = self - && let Ok(value) = value.as_ref().trim().parse::() - && Some(value) != *$field - { - *$field = Some(value); - return Some(TagItem::new($key, ItemValue::Text(format!("{value}")))) + pub fn $set (&mut self, value: &impl AsRef) -> Option { + if let Metadata::Music { tag: Some(old_tag), new_tag, .. } = self { + if old_tag.$get().is_none() { + } + if let Some(old_value) = old_tag.$get() { + if old_value != value.as_ref() { + } + } } + //match self { + //Metadata::Music { tag: Some(tag), new_tag, .. } => { + //} + //_ => None + //} + //let old_tag = &mut self.tag; + //let new_tag = &mut self.new_tag; + //if let Metadata::Music { tag: Some(tag), .. } = self + //&& Some(value.as_ref()) != tag.$get().as_deref() + //{ + //*$field = Some(value.as_ref().into()); + //return Some(TagItem::new($key, ItemValue::Text(value.as_ref().into()))) + //} None } } - } + }; + (number: $get:ident $set:ident $key:expr) => { + impl Metadata { + pub fn $get (&self) -> Option> { + if let Metadata::Music { tag: Some(tag), .. } = self { + return tag.$get().map(|t|format!("{t}").into()) + } + None + } + pub fn $set (&mut self, value: &impl AsRef) -> Option { + if let Ok(value) = value.as_ref().trim().parse::() { + if let Metadata::Music { tag: Some(old_tag), new_tag, .. } = self { + if old_tag.$get().is_none() { + } + if let Some(old_value) = old_tag.$get() { + if old_value != value { + } + } + } + } + //let old_tag = &mut self.tag; + //let new_tag = &mut self.new_tag; + //if let Metadata::Music { tag: Some(tag), .. } = self + //&& let Ok(value) = value.as_ref().trim().parse::() + //&& Some(value) != *$field + //{ + //*$field = Some(value); + //return Some(TagItem::new($key, ItemValue::Text(format!("{value}")))) + //} + None + } + } + }; } -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); +metadata_field!(string: artist set_artist ItemKey::TrackArtist); +metadata_field!(number: year set_year ItemKey::Year); +metadata_field!(string: album set_album ItemKey::AlbumTitle); +metadata_field!(number: track set_track ItemKey::TrackNumber); +metadata_field!(string: title set_title ItemKey::TrackTitle);