Use `parse_quote!()` to build complex syn::* objects
[ldk-c-bindings] / c-bindings-gen / src / types.rs
index 7e4eee3798fc8643caf2d707a37eefbeee8ddf1b..85fd01c9e6bf2e7efdb90248e47ad1d8e5dad409 100644 (file)
@@ -15,6 +15,7 @@ use std::hash;
 use crate::blocks::*;
 
 use proc_macro2::{TokenTree, Span};
+use syn::parse_quote;
 
 // The following utils are used purely to build our known types maps - they break down all the
 // types we need to resolve to include the given object, and no more.
@@ -780,10 +781,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::Duration" => Some("u64"),
                        "std::io::Error" => Some("crate::c_types::IOError"),
 
-                       "bitcoin::secp256k1::key::PublicKey" => Some("crate::c_types::PublicKey"),
+                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"
+                               => Some("crate::c_types::PublicKey"),
                        "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature"),
-                       "bitcoin::secp256k1::key::SecretKey" if is_ref  => Some("*const [u8; 32]"),
-                       "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some("crate::c_types::SecretKey"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if is_ref  => Some("*const [u8; 32]"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if !is_ref => Some("crate::c_types::SecretKey"),
                        "bitcoin::secp256k1::Error" if !is_ref => Some("crate::c_types::Secp256k1Error"),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
@@ -844,12 +848,16 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "std::time::Duration" => Some("std::time::Duration::from_secs("),
 
-                       "bitcoin::secp256k1::key::PublicKey" if is_ref => Some("&"),
-                       "bitcoin::secp256k1::key::PublicKey" => Some(""),
+                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"
+                               if is_ref => Some("&"),
+                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"
+                               => Some(""),
                        "bitcoin::secp256k1::Signature" if is_ref => Some("&"),
                        "bitcoin::secp256k1::Signature" => Some(""),
-                       "bitcoin::secp256k1::key::SecretKey" if is_ref => Some("&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *"),
-                       "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some(""),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if is_ref => Some("&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if !is_ref => Some(""),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("&::bitcoin::blockdata::script::Script::from(Vec::from("),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
                        "bitcoin::blockdata::transaction::Transaction" if is_ref => Some("&"),
@@ -899,10 +907,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "std::time::Duration" => Some(")"),
 
-                       "bitcoin::secp256k1::key::PublicKey" => Some(".into_rust()"),
+                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"
+                               => Some(".into_rust()"),
                        "bitcoin::secp256k1::Signature" => Some(".into_rust()"),
-                       "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some(".into_rust()"),
-                       "bitcoin::secp256k1::key::SecretKey" if is_ref => Some("}[..]).unwrap()"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if !is_ref => Some(".into_rust()"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if is_ref => Some("}[..]).unwrap()"),
                        "bitcoin::blockdata::script::Script" if is_ref => Some(".to_slice()))"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_rust())"),
                        "bitcoin::blockdata::transaction::Transaction" => Some(".into_bitcoin()"),
@@ -973,10 +984,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::Duration" => Some(""),
                        "std::io::Error" if !is_ref => Some("crate::c_types::IOError::from_rust("),
 
-                       "bitcoin::secp256k1::key::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
+                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"
+                               => Some("crate::c_types::PublicKey::from_rust(&"),
                        "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature::from_rust(&"),
-                       "bitcoin::secp256k1::key::SecretKey" if is_ref  => Some(""),
-                       "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if is_ref => Some(""),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
                        "bitcoin::secp256k1::Error" if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(""),
@@ -1032,10 +1046,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::Duration" => Some(".as_secs()"),
                        "std::io::Error" if !is_ref => Some(")"),
 
-                       "bitcoin::secp256k1::key::PublicKey" => Some(")"),
+                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"
+                               => Some(")"),
                        "bitcoin::secp256k1::Signature" => Some(")"),
-                       "bitcoin::secp256k1::key::SecretKey" if !is_ref => Some(")"),
-                       "bitcoin::secp256k1::key::SecretKey" if is_ref => Some(".as_ref()"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if !is_ref => Some(")"),
+                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
+                               if is_ref => Some(".as_ref()"),
                        "bitcoin::secp256k1::Error" if !is_ref => Some(")"),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
@@ -1441,15 +1458,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                let tail_str = split.next().unwrap();
                                assert!(split.next().is_none());
                                let len = &tail_str[..tail_str.len() - 1];
-                               Some(syn::Type::Array(syn::TypeArray {
-                                               bracket_token: syn::token::Bracket { span: Span::call_site() },
-                                               elem: Box::new(syn::Type::Path(syn::TypePath {
-                                                       qself: None,
-                                                       path: syn::Path::from(syn::PathSegment::from(syn::Ident::new("u8", Span::call_site()))),
-                                               })),
-                                               semi_token: syn::Token!(;)(Span::call_site()),
-                                               len: syn::Expr::Lit(syn::ExprLit { attrs: Vec::new(), lit: syn::Lit::Int(syn::LitInt::new(len, Span::call_site())) }),
-                                       }))
+                               Some(parse_quote!([u8; #len]))
                        } else { None }
                } else { None }
        }
@@ -2381,15 +2390,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                self.check_create_container(mangled_container, "Vec", vec![&*r.elem], generics, false)
                                        } else { false }
                                } else if let syn::Type::Tuple(_) = &*s.elem {
-                                       let mut args = syn::punctuated::Punctuated::new();
+                                       let mut args = syn::punctuated::Punctuated::<_, syn::token::Comma>::new();
                                        args.push(syn::GenericArgument::Type((*s.elem).clone()));
                                        let mut segments = syn::punctuated::Punctuated::new();
-                                       segments.push(syn::PathSegment {
-                                               ident: syn::Ident::new("Vec", Span::call_site()),
-                                               arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
-                                                       colon2_token: None, lt_token: syn::Token![<](Span::call_site()), args, gt_token: syn::Token![>](Span::call_site()),
-                                               })
-                                       });
+                                       segments.push(parse_quote!(Vec<#args>));
                                        self.write_c_type_intern(w, &syn::Type::Path(syn::TypePath { qself: None, path: syn::Path { leading_colon: None, segments } }), generics, false, is_mut, ptr_for_ref)
                                } else { false }
                        },