pwt-macros: update to syn2
authorDominik Csapak <d.csapak@proxmox.com>
Wed, 4 Oct 2023 07:22:23 +0000 (09:22 +0200)
committerDominik Csapak <d.csapak@proxmox.com>
Thu, 5 Oct 2023 07:43:27 +0000 (09:43 +0200)
* use path() instead of path
* replace parse_meta with parse_nested_meta or require_* (where
  appropriate)
* make has_property_derive return a result (for parse_nested_meta)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
pwt-macros/Cargo.toml
pwt-macros/src/builder.rs
pwt-macros/src/widget.rs

index 8f5b97be56b612ce6f20ee2188caa74a71796981..2b0d825ebe87de74621eb9ed468dfd8efc1b6a3e 100644 (file)
@@ -11,5 +11,5 @@ proc-macro = true
 [dependencies]
 lazy_static = "1"
 proc-macro2 = "1"
-syn = { version = "1" , features = [ "extra-traits" ] }
-quote = "1"
\ No newline at end of file
+syn = { version = "2" , features = [ "extra-traits" ] }
+quote = "1"
index c5e75f5efddda95eb2f943d78fbb7c43017d71c3..3904dae2f9c52900b2cf25d371097576ce4ef96c 100644 (file)
@@ -122,12 +122,12 @@ fn derive_builder(builder: DeriveInput) -> Result<proc_macro2::TokenStream> {
     let mut builder = Vec::new();
     for field in fields.iter_mut() {
         for (i, attr) in field.attrs.iter_mut().enumerate() {
-            if attr.path.is_ident("builder") {
+            if attr.path().is_ident("builder") {
                 let attr = attr.clone();
                 builder.push((field.clone(), attr, BuilderType::Field));
                 field.attrs.remove(i);
                 break;
-            } else if attr.path.is_ident("builder_cb") {
+            } else if attr.path().is_ident("builder_cb") {
                 let attr = attr.clone();
                 builder.push((field.clone(), attr, BuilderType::Callback));
                 field.attrs.remove(i);
@@ -154,9 +154,11 @@ fn derive_builder(builder: DeriveInput) -> Result<proc_macro2::TokenStream> {
             _ => {
                 let mut doc = String::new();
                 for attr in field.attrs {
-                    if attr.path.is_ident("doc") {
-                        if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
-                            if let syn::Lit::Str(text) = meta.lit {
+                    if attr.path().is_ident("doc") {
+                        if let Ok(syn::Expr::Lit(literal)) =
+                            attr.meta.require_name_value().map(|n| &n.value)
+                        {
+                            if let syn::Lit::Str(text) = &literal.lit {
                                 doc.push_str(&text.value());
                                 doc.push('\n');
                             }
@@ -169,45 +171,39 @@ fn derive_builder(builder: DeriveInput) -> Result<proc_macro2::TokenStream> {
             }
         };
 
-        let mut parameter_tokens = attr.tokens.clone().into_iter();
-        let (param_type, convert) = match parameter_tokens.next() {
-            Some(parameters) => {
-                let tokens = match parameters {
-                    proc_macro2::TokenTree::Group(group) => group.stream(),
-                    _ => panic!("invalid syntax"),
-                };
-
-                match builder_type {
-                    BuilderType::Field => {
-                        let options = syn::parse2::<FieldOptions>(tokens)?;
-
-                        let into_fn = options.into_fn;
-                        let into_trait = options.into_trait;
-                        if let Some(default) = options.default_value {
-                            (
-                                quote! {impl #into_trait<Option<#field_type>>},
-                                quote! {#field_ident.#into_fn().unwrap_or(#default)},
-                            )
-                        } else {
-                            (
-                                quote! {impl #into_trait<#field_type>},
-                                quote! {#field_ident.#into_fn()},
-                            )
-                        }
-                    }
-                    BuilderType::Callback => {
-                        let options = syn::parse2::<CallbackOptions>(tokens)?;
-                        let into_fn = options.into_fn;
-                        let into_trait = options.into_trait;
-                        let inner_type = options.inner_type;
+        let (param_type, convert) = if let Ok(list) = attr.meta.require_list() {
+            let tokens = list.tokens.clone();
+            match builder_type {
+                BuilderType::Field => {
+                    let options = syn::parse2::<FieldOptions>(tokens)?;
+
+                    let into_fn = options.into_fn;
+                    let into_trait = options.into_trait;
+                    if let Some(default) = options.default_value {
                         (
-                            quote! {impl #into_trait<#inner_type>},
+                            quote! {impl #into_trait<Option<#field_type>>},
+                            quote! {#field_ident.#into_fn().unwrap_or(#default)},
+                        )
+                    } else {
+                        (
+                            quote! {impl #into_trait<#field_type>},
                             quote! {#field_ident.#into_fn()},
                         )
                     }
                 }
+                BuilderType::Callback => {
+                    let options = syn::parse2::<CallbackOptions>(tokens)?;
+                    let into_fn = options.into_fn;
+                    let into_trait = options.into_trait;
+                    let inner_type = options.inner_type;
+                    (
+                        quote! {impl #into_trait<#inner_type>},
+                        quote! {#field_ident.#into_fn()},
+                    )
+                }
             }
-            None => (quote! { #field_type }, quote! { #field_ident}),
+        } else {
+            (quote! { #field_type }, quote! { #field_ident})
         };
 
         quotes.extend(quote! {
index 646a7e459482acb8e79eab2a739756ece351fb93..5224c2fdba0b281b6d942fd744fab331d6eaa1ae 100644 (file)
@@ -101,33 +101,29 @@ pub(crate) fn handle_widget_struct(setup: &WidgetSetup, input: TokenStream) -> T
         .into()
 }
 
-fn has_property_derive(attrs: &Vec<Attribute>) -> bool {
+fn has_property_derive(attrs: &Vec<Attribute>) -> Result<bool> {
     // SIGH!!
     for attr in attrs {
         if attr.style != syn::AttrStyle::Outer {
             continue;
         }
-        if let Some(ident) = attr.path.get_ident() {
+        if let Some(ident) = attr.path().get_ident() {
             if ident != "derive" {
                 continue;
             }
-            if let Ok(syn::Meta::List(list)) = attr.parse_meta() {
-                for item in list.nested {
-                    if let syn::NestedMeta::Meta(meta) = item {
-                        if let syn::Meta::Path(ref path) = meta {
-                            if let Some(ident) = path.get_ident() {
-                                if ident == "Properties" {
-                                    return true;
-                                }
-                            }
-                        }
-                    }
+            let mut has_properties = false;
+            attr.parse_nested_meta(|nested| {
+                if nested.path.is_ident("Properties") {
+                    has_properties = true;
                 }
-            }
+                Ok(())
+            })?;
+
+            return Ok(has_properties);
         }
     }
 
-    false
+    Ok(false)
 }
 
 fn derive_widget(setup: &WidgetSetup, widget: DeriveInput) -> Result<proc_macro2::TokenStream> {
@@ -141,7 +137,7 @@ fn derive_widget(setup: &WidgetSetup, widget: DeriveInput) -> Result<proc_macro2
 
     let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
 
-    let has_property_derive = has_property_derive(&attrs);
+    let has_property_derive = has_property_derive(&attrs)?;
 
     let pwt: Ident = setup.pwt_crate_name.clone().unwrap_or(format_ident!("pwt"));