updated structure

This commit is contained in:
2024-12-17 10:32:13 +01:00
parent 50e792b754
commit 1b369b8d9a
2 changed files with 23 additions and 91 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "osui-element" name = "osui-element"
version = "0.1.13" version = "0.1.14"
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"
+22 -90
View File
@@ -52,90 +52,31 @@ pub fn element(_args: TokenStream, input: TokenStream) -> TokenStream {
} }
// Generate the impl block with generics if needed // Generate the impl block with generics if needed
let impl_block = if has_generics { let impl_block = quote! {
// Keep the struct's generics (if any) when implementing the trait impl<'a> ElementCore for #struct_name<'a> {
quote! { fn get_element_by_id(&mut self, id: &str) -> Option<&mut Element> {
impl<'a, T> ElementCore for #struct_name<'a, T> { if let Children::Children(children, _) = &mut self.children {
fn get_element_by_id(&mut self, id: &str) -> Option<&mut Element> { for elem in children {
if let Children::Children(children, _) = &mut self.children { if elem.get_id() == id {
for elem in children { return Some(elem);
if elem.get_id() == id { } else if let Some(e) = elem.get_element_by_id(id) {
return Some(elem); return Some(e);
} else if let Some(e) = elem.get_element_by_id(id) {
return Some(e);
}
}
}
None
}
fn set_styling(&mut self, styling: &std::collections::HashMap<StyleName, Style>) {
if let Some(style) = styling.get(&StyleName::Class(self.class.to_string())) {
self.style = style.clone();
} else if let Some(style) = styling.get(&StyleName::Id(self.id.to_string())) {
self.style = style.clone();
} else if let Some(style) =
styling.get(&StyleName::Component(stringify!(#struct_name).to_string()))
{
self.style = style.clone();
}
if let Children::Children(children, _) = &mut self.children {
for child in children {
child.set_styling(styling);
} }
} }
} }
None
fn get_id(&self) -> String {
self.id.to_string()
}
fn get_style(&self) -> &Style {
&self.style
}
} }
}
} else {
// If no generics, don't add a generic `T` to the trait implementation
quote! {
impl<'a> ElementCore for #struct_name<'a> {
fn get_element_by_id(&mut self, id: &str) -> Option<&mut Element> {
if let Children::Children(children, _) = &mut self.children {
for elem in children {
if elem.get_id() == id {
return Some(elem);
} else if let Some(e) = elem.get_element_by_id(id) {
return Some(e);
}
}
}
None
}
fn set_styling(&mut self, styling: &std::collections::HashMap<StyleName, Style>) { fn get_id(&self) -> String {
if let Some(style) = styling.get(&StyleName::Class(self.class.to_string())) { self.id.to_string()
self.style = style.clone(); }
} else if let Some(style) = styling.get(&StyleName::Id(self.id.to_string())) {
self.style = style.clone();
} else if let Some(style) =
styling.get(&StyleName::Component(stringify!(#struct_name).to_string()))
{
self.style = style.clone();
}
if let Children::Children(children, _) = &mut self.children {
for child in children {
child.set_styling(styling);
}
}
}
fn get_id(&self) -> String { fn get_class(&self) -> String {
self.id.to_string() self.class.to_string()
} }
fn get_style(&self) -> &Style { fn get_style(&self) -> &Style {
&self.style &self.style
}
} }
} }
}; };
@@ -158,21 +99,12 @@ pub fn elem_fn(_args: TokenStream, input: TokenStream) -> TokenStream {
proc_macro2::Span::call_site(), proc_macro2::Span::call_site(),
); );
let elem_fn = if ast.generics.params.len() == 1 { let elem_fn = quote! {
quote! { pub fn #func_name<'a>() -> Box<#struct_name<'a>> {
pub fn #func_name<'a>() -> Box<#struct_name<'a>> { Box::new(#struct_name::default())
Box::new(#struct_name::default())
}
}
} else {
quote! {
pub fn #func_name<'a, T>() -> Box<#struct_name<'a, T>> {
Box::new(#struct_name::default())
}
} }
}; };
// Combine the struct and the generated function
let expanded = quote! { let expanded = quote! {
#ast #ast
#elem_fn #elem_fn