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