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