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