You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.8 KiB
Rust
95 lines
2.8 KiB
Rust
use crate::template::Params;
|
|
use crate::util::{define_ident, use_absolute};
|
|
use proc_macro2::TokenStream;
|
|
use quote::{quote, ToTokens};
|
|
|
|
use super::{Body, Component, NamedParam};
|
|
|
|
impl ToTokens for Body {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
let elements = self.elements.iter();
|
|
let code = quote! {
|
|
#(#elements),*
|
|
};
|
|
tokens.extend(code);
|
|
}
|
|
}
|
|
|
|
impl ToTokens for Component {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
let name = &self.name;
|
|
use_absolute! {
|
|
core::convert::Into as Into,
|
|
core::default::Default as Default,
|
|
core::iter::IntoIterator as IntoIterator,
|
|
uwui::backend::TargetBackend as TargetBackend,
|
|
uwui::view::Construct as Construct,
|
|
uwui::view::ConstructGroup as ConstructGroup,
|
|
uwui::view::View as View,
|
|
uwui::view::__internal::IntoNode as IntoNode,
|
|
}
|
|
define_ident! {
|
|
construct_params
|
|
}
|
|
|
|
let params = match self.params.as_ref() {
|
|
Some(p) => match p {
|
|
#[allow(non_snake_case)]
|
|
Params::Anon(params) => quote! {
|
|
#Into::<<#name as #Construct>::Params>::into(
|
|
(#(#Into::<_>::into(#params)),*,)
|
|
)
|
|
},
|
|
Params::Named(params) => quote! {
|
|
{
|
|
let mut #construct_params = <
|
|
<#name as #Construct>::Params as #Default
|
|
>::default();
|
|
#(#construct_params.#params)*
|
|
#construct_params
|
|
}
|
|
},
|
|
},
|
|
None => quote! {
|
|
<<#name as #Construct>::Params as #Default>::default()
|
|
},
|
|
};
|
|
|
|
let initializer = if let Some(body) = self.body.as_ref() {
|
|
let body = body.elements.as_slice();
|
|
quote! {
|
|
< #name as #ConstructGroup::<#TargetBackend> >::construct_group(
|
|
#params,
|
|
#IntoIterator::into_iter([
|
|
#(#body),*
|
|
])
|
|
)
|
|
}
|
|
} else {
|
|
quote! {
|
|
<#name as #Construct>::construct(#params)
|
|
}
|
|
};
|
|
|
|
let initializer = quote! {
|
|
#View::<#TargetBackend>::new(
|
|
#IntoNode::<#TargetBackend>::into_node(
|
|
#initializer
|
|
)
|
|
)
|
|
};
|
|
|
|
tokens.extend(initializer);
|
|
}
|
|
}
|
|
|
|
impl ToTokens for NamedParam {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
let (name, val) = (&self.name, &self.val);
|
|
let code = quote! {
|
|
#name = ::core::convert::Into::<_>::into(#val);
|
|
};
|
|
tokens.extend(code);
|
|
}
|
|
}
|