better structure
This commit is contained in:
+11
-2
@@ -2,7 +2,6 @@ use crate::{Component, ComponentParams};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Div {
|
||||
pub test: i32,
|
||||
pub children: Vec<Box<dyn Component>>,
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
+6
-1
@@ -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<Box<dyn Component>>,
|
||||
pub expr: String,
|
||||
}
|
||||
|
||||
+106
-18
@@ -1,31 +1,119 @@
|
||||
#[macro_export]
|
||||
macro_rules! __oml {
|
||||
($( ($tag:ident $($k:ident = $v:expr)*) {$($inner:tt)*} )+) => {{
|
||||
let mut comps: Vec<Box<dyn Component>> = 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<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
|
||||
}};
|
||||
() => { 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
|
||||
}};
|
||||
}
|
||||
|
||||
+31
-6
@@ -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<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
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user