diff --git a/automation_devices/src/hue_bridge.rs b/automation_devices/src/hue_bridge.rs index b2bc1e0..b08ab51 100644 --- a/automation_devices/src/hue_bridge.rs +++ b/automation_devices/src/hue_bridge.rs @@ -36,7 +36,7 @@ pub struct Config { crate::register_type!(Config); #[derive(Debug, Clone, Device)] -#[device(add_methods(Self::add_methods))] +#[device(add_methods = Self::add_methods)] pub struct HueBridge { config: Config, } diff --git a/automation_devices/src/ntfy.rs b/automation_devices/src/ntfy.rs index 4370df7..e873f2b 100644 --- a/automation_devices/src/ntfy.rs +++ b/automation_devices/src/ntfy.rs @@ -90,7 +90,7 @@ pub struct Config { crate::register_type!(Config); #[derive(Debug, Clone, Device)] -#[device(add_methods(Self::add_methods))] +#[device(add_methods = Self::add_methods)] pub struct Ntfy { config: Config, } diff --git a/automation_devices/src/presence.rs b/automation_devices/src/presence.rs index 2aa8a1b..72391ab 100644 --- a/automation_devices/src/presence.rs +++ b/automation_devices/src/presence.rs @@ -39,7 +39,7 @@ pub struct State { } #[derive(Debug, Clone, Device)] -#[device(add_methods(Self::add_methods))] +#[device(add_methods = Self::add_methods)] pub struct Presence { config: Config, state: Arc>, diff --git a/automation_macro/src/device.rs b/automation_macro/src/device.rs index 48675f6..f541552 100644 --- a/automation_macro/src/device.rs +++ b/automation_macro/src/device.rs @@ -4,6 +4,7 @@ use proc_macro2::TokenStream as TokenStream2; use quote::{ToTokens, quote}; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; +use syn::spanned::Spanned; use syn::{Attribute, DeriveInput, Token, parenthesized}; enum Attr { @@ -11,25 +12,25 @@ enum Attr { AddMethods(AddMethodsAttr), } -impl Parse for Attr { - fn parse(input: ParseStream) -> syn::Result { - let ident: syn::Ident = input.parse()?; - - let attr; - _ = parenthesized!(attr in input); - - let attr = match ident.to_string().as_str() { - "traits" => Attr::Trait(attr.parse()?), - "add_methods" => Attr::AddMethods(attr.parse()?), - _ => { - return Err(syn::Error::new( - ident.span(), - "Expected 'traits' or 'add_methods'", - )); +impl Attr { + fn parse(attr: &Attribute) -> syn::Result { + let mut parsed = None; + attr.parse_nested_meta(|meta| { + if meta.path.is_ident("traits") { + let input; + _ = parenthesized!(input in meta.input); + parsed = Some(Attr::Trait(input.parse()?)); + } else if meta.path.is_ident("add_methods") { + let value = meta.value()?; + parsed = Some(Attr::AddMethods(value.parse()?)); + } else { + return Err(syn::Error::new(meta.path.span(), "Unknown attribute")); } - }; - Ok(attr) + Ok(()) + })?; + + Ok(parsed.expect("Parsed should be set")) } } @@ -249,7 +250,7 @@ pub fn device(input: DeriveInput) -> TokenStream2 { .attrs .iter() .filter(|attr| attr.path().is_ident("device")) - .map(Attribute::parse_args) + .map(Attr::parse) .try_collect::>() { Ok(attr) => Implementations::from_attr(attr, input.ident), diff --git a/automation_macro/src/lib.rs b/automation_macro/src/lib.rs index 162ff30..43c6e8a 100644 --- a/automation_macro/src/lib.rs +++ b/automation_macro/src/lib.rs @@ -64,7 +64,7 @@ pub fn lua_serialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream /// ``` /// It can then be registered with: /// ```rust -/// #[device(add_methods(top_secret))] +/// #[device(add_methods = top_secret)] /// ``` #[proc_macro_derive(Device, attributes(device))] pub fn device(input: proc_macro::TokenStream) -> proc_macro::TokenStream {