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