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 socials;
49mod spacer;
50mod text_card;
51mod title;
52
53pub type ComponentCache = LazyRUMCache<RUMString, ComponentFunction>;
54pub type UserComponentItem<'a> = (&'a str, ComponentFunction);
55pub type UserComponents<'a> = Vec<UserComponentItem<'a>>;
56pub type UserComponentCacheItem = LazyRUMCacheValue<ComponentFunction>;
57
58static mut COMPONENT_CACHE: ComponentCache = new_cache();
59static DEFAULT_COMPONENT: ComponentFunction = div::div;
60
61pub fn register_component(name: &str, component_fxn: ComponentFunction) {
62 let key = RUMString::from(name);
63 let _ = rumtk_cache_push!(&raw mut COMPONENT_CACHE, &key, &component_fxn);
64}
65
66pub fn get_component(name: &str) -> UserComponentCacheItem {
67 rumtk_cache_get!(
68 &raw mut COMPONENT_CACHE,
69 &RUMString::from(name),
70 &DEFAULT_COMPONENT
71 )
72}
73
74pub fn init_components(user_components: &UserComponents) {
75 register_component("logo", logo::logo);
77 register_component("info_card", info_card::info_card);
78 register_component("portrait_card", portrait_card::portrait_card);
79 register_component("title", title::title);
80 register_component("footer", footer::footer);
81 register_component("main", main::main);
82 register_component("header", header::header);
83 register_component("contact_card", contact_card::contact_card);
84 register_component("contact_button", contact_button::contact_button);
85 register_component("socials", socials::socials);
86 register_component("item_card", item_card::item_card);
87 register_component("navlink", navlink::navlink);
88 register_component("label", label::label);
89 register_component("formatted_label", formatted_label::formatted_label);
90 register_component("text_card", text_card::text_card);
91 register_component("form", form::form::form);
92 register_component("spacer", spacer::spacer);
93 register_component("div", div::div);
94
95 for itm in user_components {
97 let (name, value) = itm;
98 register_component(name, *value);
99 }
100}
101
102#[macro_export]
103macro_rules! rumtk_web_register_component {
104 ( $key:expr, $fxn:expr ) => {{
105 use $crate::components::register_component;
106 register_component($key, $fxn)
107 }};
108}
109
110#[macro_export]
111macro_rules! rumtk_web_get_component {
112 ( $key:expr ) => {{
113 use $crate::components::get_component;
114 get_component($key)
115 }};
116}
117
118#[macro_export]
119macro_rules! rumtk_web_init_components {
120 ( $components:expr ) => {{
121 use $crate::components::init_components;
122 init_components($components)
123 }};
124}