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