From dab1fcb01b2d6e5f3f8abc105a11c67befe8e5f1 Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Thu, 19 Aug 2021 18:01:53 -0400 Subject: [PATCH] Add getters/setters for common tag items --- Cargo.toml | 8 ++++++-- src/types/tag.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e2a75243..3daf95a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,10 @@ ogg_pager = { version = "0.1.7", optional = true } # Mp4 mp4ameta = {version = "0.11.0", optional = true} # Case insensitive keys (APE/FLAC/Opus/Vorbis) -unicase = { version = "2.6.0", optional = true } +unicase = { version = "2.6.0"} + +# Quick string accessor methods for Tag +paste = { version = "1.0.5", optional = true } # Errors thiserror = "1.0.26" @@ -28,7 +31,7 @@ byteorder = "1.4.3" cfg-if = "1.0.0" [features] -default = ["mp4_atoms", "vorbis_comments", "ape", "id3v1", "id3v2", "aiff_text_chunks", "riff_info_list"] +default = ["mp4_atoms", "vorbis_comments", "ape", "id3v1", "id3v2", "aiff_text_chunks", "riff_info_list", "quick_tag_accessors"] mp4_atoms = [] vorbis_comments = ["ogg_pager"] ape = [] @@ -36,6 +39,7 @@ id3v1 = [] id3v2 = [] aiff_text_chunks = [] riff_info_list = [] +quick_tag_accessors = ["paste"] [dev-dependencies] criterion = { version = "0.3", features = ["html_reports"] } diff --git a/src/types/tag.rs b/src/types/tag.rs index 4f1fa741..bbf489b0 100644 --- a/src/types/tag.rs +++ b/src/types/tag.rs @@ -2,6 +2,35 @@ use super::item::ItemKey; use super::picture::{Picture, PictureType}; use crate::logic::id3::v2::Id3v2Version; +#[cfg(feature = "quick_tag_accessors")] +use paste::paste; + +#[cfg(feature = "quick_tag_accessors")] +macro_rules! common_items { + ($($item_key:ident => $name:tt),+) => { + paste! { + impl Tag { + $( + #[doc = "Gets the " $name] + pub fn $name(&self) -> Option<&str> { + self.get_item_ref(&ItemKey::$item_key).map(|i| i.value()) + } + + #[doc = "Removes the " $name] + pub fn [](&mut self) { + self.retain(|i| i.item_key != ItemKey::$item_key) + } + + #[doc = "Sets the " $name] + pub fn [](&mut self, value: String) { + self.insert_item(TagItem::new(ItemKey::$item_key, ItemValue::Text(value))); + } + )+ + } + } + } +} + /// Represents a tag item (key/value) pub struct TagItem { item_key: ItemKey, @@ -191,6 +220,9 @@ impl Tag { } } +#[cfg(feature = "quick_tag_accessors")] +common_items!(Artist => artist, Title => title, AlbumTitle => album_title, AlbumArtist => album_artist); + /// The tag's format #[derive(Clone, Debug, PartialEq)] pub enum TagType {