Skip to main content

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. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
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 contact_button;
33mod contact_card;
34mod content_viewer;
35pub mod div;
36mod footer;
37pub mod form;
38mod formatted_label;
39mod header;
40mod info_card;
41mod label;
42mod list;
43mod logo;
44mod main;
45mod navlink;
46mod portrait_card;
47mod script;
48mod socials;
49mod spacer;
50mod text_card;
51mod title;
52mod select;
53mod loader;
54mod job_loader;
55mod container;
56
57pub type ComponentCache = LazyRUMCache<RUMString, ComponentFunction>;
58pub type UserComponentItem<'a> = (&'a str, ComponentFunction);
59pub type UserComponents<'a> = Vec<UserComponentItem<'a>>;
60pub type UserComponentCacheItem = ComponentFunction;
61
62static mut COMPONENT_CACHE: ComponentCache = new_cache();
63static DEFAULT_COMPONENT: ComponentFunction = div::div;
64
65pub fn register_component(name: &str, component_fxn: ComponentFunction) {
66    let key = RUMString::from(name);
67    let _ = rumtk_cache_push!(&raw mut COMPONENT_CACHE, &key, component_fxn);
68}
69
70pub fn get_component(name: &str) -> Option<UserComponentCacheItem> {
71    rumtk_cache_get!(
72        &raw mut COMPONENT_CACHE,
73        &RUMString::from(name)
74    )
75}
76
77pub fn init_components(user_components: Option<UserComponents>) {
78    /* Register the default library components */
79    register_component("logo", logo::logo);
80    register_component("info_card", info_card::info_card);
81    register_component("portrait_card", portrait_card::portrait_card);
82    register_component("title", title::title);
83    register_component("footer", footer::footer);
84    register_component("main", main::main);
85    register_component("header", header::header);
86    register_component("contact_card", contact_card::contact_card);
87    register_component("contact_button", contact_button::contact_button);
88    register_component("socials", socials::socials);
89    register_component("list", list::list);
90    register_component("navlink", navlink::navlink);
91    register_component("label", label::label);
92    register_component("formatted_label", formatted_label::formatted_label);
93    register_component("text_card", text_card::text_card);
94    register_component("form", form::form::form);
95    register_component("spacer", spacer::spacer);
96    register_component("script", script::script);
97    register_component("loader", loader::loader);
98    register_component("job_loader", job_loader::job_loader);
99    register_component("content_viewer", content_viewer::content_viewer);
100    register_component("container", container::container);
101    register_component("select", select::select);
102    register_component("div", div::div);
103
104    /* Init any user prescribed components */
105    for itm in user_components.unwrap_or_default() {
106        let (name, value) = itm;
107        register_component(name, value);
108    }
109}
110
111#[macro_export]
112macro_rules! rumtk_web_register_component {
113    ( $key:expr, $fxn:expr ) => {{
114        use $crate::components::register_component;
115        register_component($key, $fxn)
116    }};
117}
118
119#[macro_export]
120macro_rules! rumtk_web_get_component {
121    ( $key:expr ) => {{
122        use $crate::components::get_component;
123        get_component($key)
124    }};
125}
126
127#[macro_export]
128macro_rules! rumtk_web_init_components {
129    ( $components:expr ) => {{
130        use $crate::components::init_components;
131        init_components($components)
132    }};
133}