rumtk_web/components/
mod.rs1use crate::utils::ComponentFunction;
24use rumtk_core::cache::{new_cache, LazyRUMCache, LazyRUMCacheValue};
25use rumtk_core::strings::RUMString;
26use rumtk_core::{rumtk_cache_get, rumtk_cache_push};
27
28pub mod app_body;
30pub mod app_head;
31pub mod app_shell;
32
33mod contact_button;
35mod contact_card;
36pub mod div;
37mod footer;
38pub mod form;
39mod formatted_label;
40mod header;
41mod info_card;
42mod item_card;
43mod label;
44mod logo;
45mod main;
46mod navlink;
47mod portrait_card;
48mod script;
49mod socials;
50mod spacer;
51mod text_card;
52mod title;
53
54pub type ComponentCache = LazyRUMCache<RUMString, ComponentFunction>;
55pub type UserComponentItem<'a> = (&'a str, ComponentFunction);
56pub type UserComponents<'a> = Vec<UserComponentItem<'a>>;
57pub type UserComponentCacheItem = LazyRUMCacheValue<ComponentFunction>;
58
59static mut COMPONENT_CACHE: ComponentCache = new_cache();
60static DEFAULT_COMPONENT: ComponentFunction = div::div;
61
62pub fn register_component(name: &str, component_fxn: ComponentFunction) {
63 let key = RUMString::from(name);
64 let _ = rumtk_cache_push!(&raw mut COMPONENT_CACHE, &key, &component_fxn);
65}
66
67pub fn get_component(name: &str) -> UserComponentCacheItem {
68 rumtk_cache_get!(
69 &raw mut COMPONENT_CACHE,
70 &RUMString::from(name),
71 &DEFAULT_COMPONENT
72 )
73}
74
75pub fn init_components(user_components: &UserComponents) {
76 register_component("logo", logo::logo);
78 register_component("info_card", info_card::info_card);
79 register_component("portrait_card", portrait_card::portrait_card);
80 register_component("title", title::title);
81 register_component("footer", footer::footer);
82 register_component("main", main::main);
83 register_component("header", header::header);
84 register_component("contact_card", contact_card::contact_card);
85 register_component("contact_button", contact_button::contact_button);
86 register_component("socials", socials::socials);
87 register_component("item_card", item_card::item_card);
88 register_component("navlink", navlink::navlink);
89 register_component("label", label::label);
90 register_component("formatted_label", formatted_label::formatted_label);
91 register_component("text_card", text_card::text_card);
92 register_component("form", form::form::form);
93 register_component("spacer", spacer::spacer);
94 register_component("script", script::script);
95 register_component("div", div::div);
96
97 for itm in user_components {
99 let (name, value) = itm;
100 register_component(name, *value);
101 }
102}
103
104#[macro_export]
105macro_rules! rumtk_web_register_component {
106 ( $key:expr, $fxn:expr ) => {{
107 use $crate::components::register_component;
108 register_component($key, $fxn)
109 }};
110}
111
112#[macro_export]
113macro_rules! rumtk_web_get_component {
114 ( $key:expr ) => {{
115 use $crate::components::get_component;
116 get_component($key)
117 }};
118}
119
120#[macro_export]
121macro_rules! rumtk_web_init_components {
122 ( $components:expr ) => {{
123 use $crate::components::init_components;
124 init_components($components)
125 }};
126}