some fixes and changes
This commit is contained in:
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "osui-element"
|
name = "osui-element"
|
||||||
version = "0.1.21"
|
version = "0.1.22"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "The element attribute for defining elements in OSUI"
|
description = "The element attribute for defining elements in OSUI"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
@@ -9,6 +9,10 @@ license = "Apache-2.0"
|
|||||||
proc-macro = true
|
proc-macro = true
|
||||||
path = "./lib.rs"
|
path = "./lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
path = "./main.rs"
|
||||||
|
name = "osui-element"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1.0.89"
|
proc-macro2 = "1.0.89"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use proc_macro2::Span;
|
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{
|
use syn::{
|
||||||
parse::Parser, parse_macro_input, DeriveInput, FnArg, ItemFn, Lifetime, LifetimeParam,
|
parse::Parser, parse_macro_input, DeriveInput, FnArg, GenericParam, ItemFn, Lifetime,
|
||||||
PatIdent, PatType, Type, TypeReference,
|
LifetimeParam, PatIdent, PatType,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
@@ -101,7 +100,20 @@ pub fn component(_args: TokenStream, input: TokenStream) -> TokenStream {
|
|||||||
syn::ReturnType::Default => syn::parse_quote! { Element },
|
syn::ReturnType::Default => syn::parse_quote! { Element },
|
||||||
syn::ReturnType::Type(_, t) => t,
|
syn::ReturnType::Type(_, t) => t,
|
||||||
};
|
};
|
||||||
let mut fn_lifetime = false;
|
|
||||||
|
let lifetimes: Vec<_> = input_fn
|
||||||
|
.sig
|
||||||
|
.generics
|
||||||
|
.params
|
||||||
|
.iter()
|
||||||
|
.filter_map(|param| {
|
||||||
|
if let GenericParam::Lifetime(lifetime) = param {
|
||||||
|
Some(&lifetime.lifetime)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let mut struct_fields = Vec::new();
|
let mut struct_fields = Vec::new();
|
||||||
|
|
||||||
@@ -109,32 +121,20 @@ pub fn component(_args: TokenStream, input: TokenStream) -> TokenStream {
|
|||||||
if let FnArg::Typed(PatType { pat, ty, .. }) = arg {
|
if let FnArg::Typed(PatType { pat, ty, .. }) = arg {
|
||||||
if let syn::Pat::Ident(PatIdent { ident, .. }) = &**pat {
|
if let syn::Pat::Ident(PatIdent { ident, .. }) = &**pat {
|
||||||
let field_name = ident.clone();
|
let field_name = ident.clone();
|
||||||
let mut field_type = *(ty.clone());
|
let field_type = ty.clone();
|
||||||
|
|
||||||
if let Type::Reference(TypeReference { lifetime, .. }) = &mut field_type {
|
|
||||||
fn_lifetime = true;
|
|
||||||
*lifetime = Some(Lifetime::new("'a", Span::call_site()));
|
|
||||||
}
|
|
||||||
|
|
||||||
struct_fields.push(quote! { pub #field_name: #field_type });
|
struct_fields.push(quote! { pub #field_name: #field_type });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let lifetime = if fn_lifetime {
|
|
||||||
quote! {<'a>}
|
|
||||||
} else {
|
|
||||||
quote! {}
|
|
||||||
};
|
|
||||||
|
|
||||||
let expanded = quote! {
|
let expanded = quote! {
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
#visibility struct #fn_name #lifetime {
|
#visibility struct #fn_name<#(#lifetimes),*> {
|
||||||
#(#struct_fields),*
|
#(#struct_fields),*
|
||||||
}
|
}
|
||||||
|
|
||||||
impl #lifetime Component for #lifetime #fn_name #lifetime {
|
impl <#(#lifetimes),*> Component for #fn_name <#(#lifetimes),*> {
|
||||||
fn create_element(self) -> #return_type {
|
fn create_element(self) -> #return_type {
|
||||||
#code
|
#code
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user