Add macro for creating simple key-based get/set methods

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2021-07-09 15:13:51 -04:00
parent 547d5412c0
commit 3b8e08ace0
3 changed files with 36 additions and 10 deletions

2
lofty-attr/Cargo.lock generated
View file

@ -4,7 +4,7 @@ version = 3
[[package]]
name = "lofty_attr"
version = "0.1.6"
version = "0.1.7"
dependencies = [
"quote",
"syn",

View file

@ -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"

View file

@ -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:")
.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:")
}