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;
38mod form;
39mod formatted_label;
40mod header;
41mod info_card;
42mod item_card;
43mod label;
44mod logo;
45mod main;
46mod navlink;
47mod portrait_card;
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 = LazyRUMCacheValue<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) -> UserComponentCacheItem {
67    rumtk_cache_get!(
68        &raw mut COMPONENT_CACHE,
69        &RUMString::from(name),
70        &DEFAULT_COMPONENT
71    )
72}
73
74pub fn init_components(user_components: &UserComponents) {
75    /* Register the default library components */
76    register_component("logo", logo::logo);
77    register_component("info_card", info_card::info_card);
78    register_component("portrait_card", portrait_card::portrait_card);
79    register_component("title", title::title);
80    register_component("footer", footer::footer);
81    register_component("main", main::main);
82    register_component("header", header::header);
83    register_component("contact_card", contact_card::contact_card);
84    register_component("contact_button", contact_button::contact_button);
85    register_component("socials", socials::socials);
86    register_component("item_card", item_card::item_card);
87    register_component("navlink", navlink::navlink);
88    register_component("label", label::label);
89    register_component("formatted_label", formatted_label::formatted_label);
90    register_component("text_card", text_card::text_card);
91    register_component("form", form::form::form);
92    register_component("spacer", spacer::spacer);
93    register_component("div", div::div);
94
95    /* Init any user prescribed components */
96    for itm in user_components {
97        let (name, value) = itm;
98        register_component(name, *value);
99    }
100}
101
102#[macro_export]
103macro_rules! rumtk_web_register_component {
104    ( $key:expr, $fxn:expr ) => {{
105        use $crate::components::register_component;
106        register_component($key, $fxn)
107    }};
108}
109
110#[macro_export]
111macro_rules! rumtk_web_get_component {
112    ( $key:expr ) => {{
113        use $crate::components::get_component;
114        get_component($key)
115    }};
116}
117
118#[macro_export]
119macro_rules! rumtk_web_init_components {
120    ( $components:expr ) => {{
121        use $crate::components::init_components;
122        init_components($components)
123    }};
124}