better structure

This commit is contained in:
2024-10-15 20:19:35 +02:00
parent 9e013f737f
commit d3b24cb3a8
4 changed files with 154 additions and 27 deletions
+11 -2
View File
@@ -2,7 +2,6 @@ use crate::{Component, ComponentParams};
#[derive(Debug)] #[derive(Debug)]
pub struct Div { pub struct Div {
pub test: i32,
pub children: Vec<Box<dyn Component>>, pub children: Vec<Box<dyn Component>>,
} }
@@ -10,7 +9,17 @@ impl Component for Div {}
pub fn div(params: ComponentParams) -> Div { pub fn div(params: ComponentParams) -> Div {
Div { Div {
test: 0,
children: params.children, children: params.children,
} }
} }
#[derive(Debug)]
pub struct Text {
pub text: String,
}
impl Component for Text {}
pub fn text(params: ComponentParams) -> Text {
Text { text: params.expr }
}
+6 -1
View File
@@ -3,8 +3,13 @@ use std::fmt::Debug;
pub mod components; pub mod components;
pub mod macros; pub mod macros;
pub trait Component: Debug {} pub trait Component: Debug {
fn render(&mut self) -> String {
String::new()
}
}
pub struct ComponentParams { pub struct ComponentParams {
pub children: Vec<Box<dyn Component>>, pub children: Vec<Box<dyn Component>>,
pub expr: String,
} }
+106 -18
View File
@@ -1,31 +1,119 @@
#[macro_export] #[macro_export]
macro_rules! __oml { macro_rules! __oml {
($( ($tag:ident $($k:ident = $v:expr)*) {$($inner:tt)*} )+) => {{ () => { Vec::new() };
let mut comps: Vec<Box<dyn Component>> = Vec::new();
$( // Props, With components (PC)
// comps.push(Box::new($tag(ComponentParams { children: osui::__oml!($($inner)*) }))); ($tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*}) => {{
comps.push(oml!(($tag $($k = $v)*) {$($inner)*})); vec![oml!($tag ($($k = $v),*) {$($inner)*})]
)* }};
($tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($($k = $v),*) {$($inner)*}));
comps
}};
// Components (C)
($tag:ident {$($inner:tt)*}) => {{
vec![oml!($tag {$($inner)*})]
}};
($tag:ident {$($inner:tt)*} $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag {$($inner)*}));
comps
}};
// Expression, With components (EC)
($tag:ident ($expr:expr; $($k:ident = $v:expr),*) {$($inner:tt)*}) => {{
vec![oml!($tag ($expr; $($k = $v),*) {$($inner)*})]
}};
($tag:ident ($expr:expr; $($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($expr; $($k = $v),*) {$($inner)*}));
comps
}};
// Expression (E)
($tag:ident ($expr:expr; $($k:ident = $v:expr),*)) => {{
vec![oml!($tag ($expr; $($k = $v),*))]
}};
($tag:ident ($expr:expr; $($k:ident = $v:expr),*) $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($expr; $($k = $v),*)));
comps
}};
// Props (P)
($tag:ident ($($k:ident = $v:expr),*)) => {{
vec![oml!($tag ($($k = $v),*))]
}};
($tag:ident ($($k:ident = $v:expr),*) $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($($k = $v),*)));
comps
}};
// Pre-Defined (D)
($expr:expr;) => {{
vec![oml!($expr;)]
}};
($expr:expr; $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+);
comps.insert(0, oml!($expr));
comps comps
}}; }};
() => { Vec::new() };
} }
#[macro_export] #[macro_export]
macro_rules! oml { macro_rules! oml {
( ($tag:ident) {} ) => { // Props (P)
Box::new($tag(ComponentParams { children: Vec::new() })) ($tag:ident ($($k:ident = $v:expr),*)) => {{
}; let mut c = $tag(osui::ComponentParams { children: Vec::new(), expr: String::new() });
( ($tag:ident) {$($inner:tt)*} ) => {
Box::new($tag(ComponentParams { children: osui::__oml!($($inner)*) }))
};
( ($tag:ident $($k:ident = $v:expr)+ ) {$($inner:tt)*} ) => {{
let mut c = $tag(ComponentParams { children: osui::__oml!($($inner)*) });
$( $(
c.$k = $v; c.$k = $v;
)+ )*
Box::new(c) Box::new(c)
}}; }};
// Expression, With components (EC)
( $tag:ident ($expr:expr; $($k:ident = $v:expr),* ) {$($inner:tt)*} ) => {{
let mut c = $tag(osui::ComponentParams { children: osui::__oml!($($inner)*), expr: String::from($expr) });
$(
c.$k = $v;
)*
Box::new(c)
}};
// Expression (E)
( $tag:ident ($expr:expr; $($k:ident = $v:expr),*) ) => {{
let mut c = $tag(osui::ComponentParams { children: Vec::new(), expr: String::from($expr) });
$(
c.$k = $v;
)*
Box::new(c)
}};
// Components (C)
( $tag:ident {$($inner:tt)*} ) => {
Box::new($tag(osui::ComponentParams { children: osui::__oml!($($inner)*), expr: String::new() }))
};
// Props, With components (PC)
( $tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} ) => {{
let mut c = $tag(osui::ComponentParams { children: osui::__oml!($($inner)*), expr: String::new() });
$(
c.$k = $v;
)*
Box::new(c)
}};
// Pre-Defined (D)
($expr:expr;) => {{
$expr
}};
} }
+31 -6
View File
@@ -1,11 +1,36 @@
use osui::{components::div, oml, Component, ComponentParams}; use osui::{
components::{div, text, Div},
oml, ComponentParams,
};
fn main() { fn main() {
let doc = oml!( let some_text = app(ComponentParams {
(div) { children: vec![],
(div) {} expr: "".to_string(),
} });
); let doc = oml!(div { some_text; });
println!("{:?}", doc); println!("{:?}", doc);
} }
fn app(_: ComponentParams) -> Box<Div> {
oml!(
// Components (C)
div {
// Props, With components (PC)
div () {
// Props (P)
text(/* Properties go here, Example: x = 20 y = 5 */)
}
// Expression (E)
text("Hello";)
// Expression, With components (EC)
text("World!";) {
// Components in here
}
}
)
}