From d3b24cb3a8422bbf847d6631464a4c957b52fea4 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 15 Oct 2024 20:19:35 +0200 Subject: [PATCH] better structure --- src/components.rs | 13 ++++- src/lib.rs | 7 ++- src/macros.rs | 124 +++++++++++++++++++++++++++++++++++++++------- src/main.rs | 37 +++++++++++--- 4 files changed, 154 insertions(+), 27 deletions(-) diff --git a/src/components.rs b/src/components.rs index 58cfa7e..e65b5b3 100644 --- a/src/components.rs +++ b/src/components.rs @@ -2,7 +2,6 @@ use crate::{Component, ComponentParams}; #[derive(Debug)] pub struct Div { - pub test: i32, pub children: Vec>, } @@ -10,7 +9,17 @@ impl Component for Div {} pub fn div(params: ComponentParams) -> Div { Div { - test: 0, 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 } +} diff --git a/src/lib.rs b/src/lib.rs index f0ddc8f..124f3b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,13 @@ use std::fmt::Debug; pub mod components; pub mod macros; -pub trait Component: Debug {} +pub trait Component: Debug { + fn render(&mut self) -> String { + String::new() + } +} pub struct ComponentParams { pub children: Vec>, + pub expr: String, } diff --git a/src/macros.rs b/src/macros.rs index e263e7c..e120f0c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,31 +1,119 @@ #[macro_export] macro_rules! __oml { - ($( ($tag:ident $($k:ident = $v:expr)*) {$($inner:tt)*} )+) => {{ - let mut comps: Vec> = Vec::new(); - $( - // comps.push(Box::new($tag(ComponentParams { children: osui::__oml!($($inner)*) }))); - comps.push(oml!(($tag $($k = $v)*) {$($inner)*})); - )* + () => { Vec::new() }; + + // Props, With components (PC) + ($tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*}) => {{ + vec![oml!($tag ($($k = $v),*) {$($inner)*})] + }}; + + ($tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{ + let mut comps: Vec> = 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> = 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> = 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> = 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> = 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> = osui::__oml!($($rest)+); + comps.insert(0, oml!($expr)); comps }}; - () => { Vec::new() }; } #[macro_export] macro_rules! oml { - ( ($tag:ident) {} ) => { - Box::new($tag(ComponentParams { children: Vec::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)*) }); + // Props (P) + ($tag:ident ($($k:ident = $v:expr),*)) => {{ + let mut c = $tag(osui::ComponentParams { children: Vec::new(), expr: String::new() }); $( c.$k = $v; - )+ + )* 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 + }}; } diff --git a/src/main.rs b/src/main.rs index 0192c4e..aa083f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,36 @@ -use osui::{components::div, oml, Component, ComponentParams}; +use osui::{ + components::{div, text, Div}, + oml, ComponentParams, +}; fn main() { - let doc = oml!( - (div) { - (div) {} - } - ); + let some_text = app(ComponentParams { + children: vec![], + expr: "".to_string(), + }); + let doc = oml!(div { some_text; }); println!("{:?}", doc); } + +fn app(_: ComponentParams) -> Box
{ + 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 + } + } + ) +}