rumtk_web/components/
mod.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D.
5 * Copyright (C) 2025  MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21use 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
26//AppShell
27pub mod app_body;
28pub mod app_head;
29pub mod app_shell;
30
31// Components
32mod 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 the default library components */
73    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    /* Init any user prescribed components */
92    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}