rumtk_web/pages/
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  MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21use crate::utils::PageFunction;
22use rumtk_core::cache::{new_cache, LazyRUMCache};
23use rumtk_core::strings::RUMString;
24use rumtk_core::{rumtk_cache_get, rumtk_cache_push};
25
26pub type PageCache = LazyRUMCache<RUMString, PageFunction>;
27pub type PageItem = (RUMString, PageFunction);
28pub type UserPages = Vec<PageItem>;
29
30static mut page_cache: PageCache = new_cache();
31
32pub fn register_page(name: &str, component_fxn: PageFunction) {
33    let key = RUMString::from(name);
34    rumtk_cache_push!(&mut page_cache, &key, &component_fxn);
35}
36
37pub fn get_page(name: &str) -> Option<&PageFunction> {
38    rumtk_cache_get!(&mut page_cache, &RUMString::from(name))
39}
40
41pub fn init_pages(user_components: &UserPages) {
42    /* Init any user prescribed components */
43    for itm in user_components {
44        let (name, value) = itm;
45        register_page(name, *value);
46    }
47}
48
49#[macro_export]
50macro_rules! rumtk_web_register_page {
51    ( $key:expr, $fxn:expr ) => {{
52        use $crate::pages::register_page;
53        register_page($key, $fxn)
54    }};
55}
56
57#[macro_export]
58macro_rules! rumtk_web_get_page {
59    ( $key:expr ) => {{
60        use $crate::pages::get_page;
61        get_page($key)
62    }};
63}
64
65#[macro_export]
66macro_rules! rumtk_web_init_pages {
67    ( $pages:expr ) => {{
68        use $crate::pages::init_pages;
69        init_pages($pages)
70    }};
71}