diff --git a/lofty-attr/Cargo.lock b/lofty-attr/Cargo.lock index 0116c6b2..dd1cb9e1 100644 --- a/lofty-attr/Cargo.lock +++ b/lofty-attr/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "lofty_attr" -version = "0.1.6" +version = "0.1.7" dependencies = [ "quote", "syn", diff --git a/lofty-attr/Cargo.toml b/lofty-attr/Cargo.toml index cbb6651d..4448c726 100644 --- a/lofty-attr/Cargo.toml +++ b/lofty-attr/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "lofty_attr" -version = "0.1.6" +version = "0.1.7" authors = ["Serial <69764315+Serial-ATA@users.noreply.github.com>"] -description = "Macro for Lofty tag struct creation" +description = "Macros for Lofty tag struct creation" license = "MIT OR Apache-2.0" repository = "https://github.com/Serial-ATA/lofty-rs" edition = "2018" diff --git a/lofty-attr/src/lib.rs b/lofty-attr/src/lib.rs index 66d3d700..a8b8f6c4 100644 --- a/lofty-attr/src/lib.rs +++ b/lofty-attr/src/lib.rs @@ -221,8 +221,8 @@ pub fn u16_accessor(input: TokenStream) -> TokenStream { ident = input_str, display = name, ) - .parse() - .expect("Unable to parse u16 accessor:") + .parse() + .expect("Unable to parse u16 accessor:") } #[proc_macro] @@ -243,8 +243,8 @@ pub fn u32_accessor(input: TokenStream) -> TokenStream { ident = input_str, display = name, ) - .parse() - .expect("Unable to parse u32 accessor:") + .parse() + .expect("Unable to parse u32 accessor:") } #[proc_macro] @@ -265,6 +265,32 @@ pub fn i32_accessor(input: TokenStream) -> TokenStream { ident = input_str, display = name, ) - .parse() - .expect("Unable to parse i32 accessor:") -} \ No newline at end of file + .parse() + .expect("Unable to parse i32 accessor:") +} + +/// Used to create simple tag methods for getting/setting/removing based on a key +#[proc_macro] +pub fn get_set_methods(input: TokenStream) -> TokenStream { + let input = input.to_string(); + let mut input_split = input.split(','); + + let name = input_split.next().expect("No identifier provided"); + let key = input_split.next().expect("No key provided"); + + format!( + "fn {ident}(&self) -> Option<&str> {{ + self.get_value({key}) + }} + fn set_{ident}(&mut self, {ident}: &str) {{ + self.set_value({key}, {ident}) + }} + fn remove_{ident}(&mut self) {{ + self.remove_key({key}) + }}", + ident = name, + key = key + ) + .parse() + .expect("Unable to parse getters/setters:") +}