diff --git a/src/model/metadata.rs b/src/model/metadata.rs index 6896dd2..8a8a453 100644 --- a/src/model/metadata.rs +++ b/src/model/metadata.rs @@ -12,28 +12,28 @@ use lofty::{ pub enum Metadata { Directory { - hash_file: Option<()>, + hash_file: Option<()>, catalog_file: Option<()>, - artist_file: Option<()>, + artist_file: Option<()>, release_file: Option<()>, }, Music { - invalid: bool, - hash: Arc, - size: Arc, - tag: Option>, - new_tag: Option>, + invalid: bool, + hash: Arc, + size: Arc, + original_tag: Option>, + modified_tag: Option>, }, Image { - invalid: bool, - hash: Arc, - size: Arc, - title: Option, - author: Option, + invalid: bool, + hash: Arc, + size: Arc, + title: Option, + author: Option, }, Unknown { - hash: Arc, - size: Arc, + hash: Arc, + size: Arc, } } @@ -55,9 +55,9 @@ impl Metadata { Ok(Self::Music { hash, size: format!("{:#>8.2}", size).into(), - tag: tag.map(|t|t.clone().into()), - new_tag: tag.map(|t|t.clone().into()), invalid: false, + original_tag: tag.map(|t|t.clone().into()), + modified_tag: tag.map(|t|t.clone().into()), }) } else { Self::new_fallback(path) @@ -123,31 +123,55 @@ impl Metadata { } } -macro_rules! metadata_field { +macro_rules! music_tag_field { (string: $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|t.into()) + if let Metadata::Music { original_tag, modified_tag, .. } = self { + return modified_tag + .as_ref() + .map(|tag|tag.$get().map(|t|t.into())) + .flatten() + .or_else(||original_tag + .as_ref() + .map(|tag|tag.$get().map(|t|t.into())) + .flatten()) } None } 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 Metadata::Music { original_tag, modified_tag, .. } = self { + let value = value.as_ref().trim(); + match (value.len(), original_tag, modified_tag) { + (0, Some(old_tag), Some(new_tag)) => { + }, + (_, Some(old_tag), Some(new_tag)) => { + }, + (0, Some(old_tag), None) => { + }, + (_, Some(old_tag), None) => { + }, + (0, None, Some(new_tag)) => { + }, + (_, None, Some(new_tag)) => { + }, + (0, None, None) => { + unimplemented!("specifying new tag type") + }, + (_, None, None) => { + unimplemented!("specifying new tag type") + }, } - if let Some(old_value) = old_tag.$get() { - if old_value != value.as_ref() { - } - } - } - //match self { - //Metadata::Music { tag: Some(tag), new_tag, .. } => { + //let value = value.as_ref().trim(); + //if value.len() > 0 { + //if old_tag.$get().is_none() { + //old_tag.$set(value) + //} + //if let Some(old_value) = old_tag.$get() { + //} + //} else { //} - //_ => 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() //{ @@ -161,24 +185,57 @@ macro_rules! metadata_field { (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()) + if let Metadata::Music { original_tag, modified_tag, .. } = self { + return modified_tag + .as_ref() + .map(|tag|tag.$get().map(|t|format!("{t}").into())) + .flatten() + .or_else(||original_tag + .as_ref() + .map(|tag|tag.$get().map(|t|format!("{t}").into())) + .flatten()) } 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 { - } - } + if let Metadata::Music { original_tag, modified_tag, .. } = self { + let value = value.as_ref().trim(); + match (value.len(), original_tag, modified_tag) { + (0, Some(old_tag), Some(new_tag)) => { + }, + (_, Some(old_tag), Some(new_tag)) => { + }, + (0, Some(old_tag), None) => { + }, + (_, Some(old_tag), None) => { + }, + (0, None, Some(new_tag)) => { + }, + (_, None, Some(new_tag)) => { + }, + (0, None, None) => { + unimplemented!("specifying new tag type") + }, + (_, None, None) => { + unimplemented!("specifying new tag type") + }, } } - //let old_tag = &mut self.tag; - //let new_tag = &mut self.new_tag; + None + //let value = value.as_ref().trim(); + //if value.len() > 0 { + //if let Ok(value) = value.parse::() { + //if let Metadata::Music { + //tag: Some(old_tag), new_tag: Some(new_tag), .. + //} = self { + //if old_tag.$get().is_none() { + //} + //if let Some(old_value) = old_tag.$get() { + //} + //} + //} + //} else { + //} //if let Metadata::Music { tag: Some(tag), .. } = self //&& let Ok(value) = value.as_ref().trim().parse::() //&& Some(value) != *$field @@ -186,14 +243,13 @@ macro_rules! metadata_field { //*$field = Some(value); //return Some(TagItem::new($key, ItemValue::Text(format!("{value}")))) //} - None } } }; } -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); +music_tag_field!(string: artist set_artist ItemKey::TrackArtist); +music_tag_field!(number: year set_year ItemKey::Year); +music_tag_field!(string: album set_album ItemKey::AlbumTitle); +music_tag_field!(number: track set_track ItemKey::TrackNumber); +music_tag_field!(string: title set_title ItemKey::TrackTitle);