macro goodness
This commit is contained in:
@@ -145,9 +145,16 @@ pub fn derive_global_state(input: TokenStream) -> TokenStream {
|
||||
|
||||
#[proc_macro_derive(HasUi)]
|
||||
pub fn derive_has_ui(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as ItemStruct);
|
||||
let name = input.ident;
|
||||
let Some(field) = input.fields.iter().find(|f| f.ty == parse_quote!(Ui<Self>)) else {
|
||||
has_ui(&parse_macro_input!(input))
|
||||
}
|
||||
|
||||
fn has_ui(input: &ItemStruct) -> TokenStream {
|
||||
let name = &input.ident;
|
||||
let Some(field) = input
|
||||
.fields
|
||||
.iter()
|
||||
.find(|f| f.ty == parse_quote!(Ui<Self>) || f.ty == parse_quote!(Ui))
|
||||
else {
|
||||
return Error::new(name.span(), "could not find a Ui<Self> field for HasUi")
|
||||
.into_compile_error()
|
||||
.into();
|
||||
@@ -155,11 +162,11 @@ pub fn derive_has_ui(input: TokenStream) -> TokenStream {
|
||||
let field = &field.ident;
|
||||
quote! {
|
||||
impl HasUi for #name {
|
||||
fn ui_ref(&self) -> &Ui<Self> {
|
||||
fn ui_ref(&self) -> &iris::iris_core::Ui<Self> {
|
||||
&self.#field
|
||||
}
|
||||
|
||||
fn ui(&mut self) -> &mut Ui<Self> {
|
||||
fn ui(&mut self) -> &mut iris::iris_core::Ui<Self> {
|
||||
&mut self.#field
|
||||
}
|
||||
}
|
||||
@@ -167,14 +174,15 @@ pub fn derive_has_ui(input: TokenStream) -> TokenStream {
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(HasUiState)]
|
||||
pub fn derive_has_ui_state(input: TokenStream) -> TokenStream {
|
||||
#[proc_macro_derive(DefaultUiState)]
|
||||
pub fn derive_default_ui_state(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as ItemStruct);
|
||||
let mut output = has_ui(&input);
|
||||
let name = input.ident;
|
||||
let Some(field) = input
|
||||
.fields
|
||||
.iter()
|
||||
.find(|f| f.ty == parse_quote!(UiState<Self>))
|
||||
.find(|f| f.ty == parse_quote!(UiState<Self>) || f.ty == parse_quote!(UiState))
|
||||
else {
|
||||
return Error::new(
|
||||
name.span(),
|
||||
@@ -184,12 +192,15 @@ pub fn derive_has_ui_state(input: TokenStream) -> TokenStream {
|
||||
.into();
|
||||
};
|
||||
let field = &field.ident;
|
||||
quote! {
|
||||
impl HasUiState for #name {
|
||||
fn ui_state(&mut self) -> &mut UiState<Self> {
|
||||
&mut self.#field
|
||||
output.extend::<TokenStream>(
|
||||
quote! {
|
||||
impl HasUiState for #name {
|
||||
fn ui_state(&mut self) -> &mut iris::default::UiState<Self> {
|
||||
&mut self.#field
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.into()
|
||||
.into(),
|
||||
);
|
||||
output
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user