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};
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 Form;
33mod contact_button;
34mod contact_card;
35pub mod div;
36mod footer;
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 use Form::form_utils::*;
51
52pub type ComponentCache = LazyRUMCache<RUMString, ComponentFunction>;
53pub type UserComponentCacheItem = (RUMString, ComponentFunction);
54pub type UserComponents = Vec<UserComponentCacheItem>;
55
56static mut component_cache: ComponentCache = new_cache();
57
58pub fn register_component(name: &str, component_fxn: ComponentFunction) {
59    let key = RUMString::from(name);
60    rumtk_cache_push!(&mut component_cache, &key, &component_fxn);
61}
62
63pub fn get_component(name: &str) -> Option<&ComponentFunction> {
64    rumtk_cache_get!(&mut component_cache, &RUMString::from(name))
65}
66
67pub fn init_components(user_components: &UserComponents) {
68    /* Register the default library components */
69    register_component("logo", logo::logo);
70    register_component("info_card", info_card::info_card);
71    register_component("portrait_card", portrait_card::portrait_card);
72    register_component("title", title::title);
73    register_component("footer", footer::footer);
74    register_component("navbar", navbar::navbar);
75    register_component("contact_card", contact_card::contact_card);
76    register_component("contact_button", contact_button::contact_button);
77    register_component("socials", socials::socials);
78    register_component("item_card", item_card::item_card);
79    register_component("navlink", navlink::navlink);
80    register_component("label", label::label);
81    register_component("formatted_label", formatted_label::formatted_label);
82    register_component("text_card", text_card::text_card);
83    register_component("form", Form::form::form);
84    register_component("spacer", spacer::spacer);
85    register_component("div", div::div);
86
87    /* Init any user prescribed components */
88    for itm in user_components {
89        let (name, value) = itm;
90        register_component(name, *value);
91    }
92}
93
94#[macro_export]
95macro_rules! rumtk_web_register_component {
96    ( $key:expr, $fxn:expr ) => {{
97        use $crate::components::register_component;
98        register_component($key, $fxn)
99    }};
100}
101
102#[macro_export]
103macro_rules! rumtk_web_get_component {
104    ( $key:expr ) => {{
105        use $crate::components::get_component;
106        get_component($key)
107    }};
108}
109
110#[macro_export]
111macro_rules! rumtk_web_init_components {
112    ( $components:expr ) => {{
113        use $crate::components::init_components;
114        init_components($components)
115    }};
116}