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 */
21pub mod index;
22
23use crate::utils::PageFunction;
24use rumtk_core::cache::{new_cache, LazyRUMCache, LazyRUMCacheValue};
25use rumtk_core::strings::RUMString;
26use rumtk_core::{rumtk_cache_get, rumtk_cache_push};
27
28pub type PageCache = LazyRUMCache<RUMString, PageFunction>;
29pub type PageItem<'a> = (&'a str, PageFunction);
30pub type UserPages<'a> = Vec<PageItem<'a>>;
31pub type PageCacheItem = LazyRUMCacheValue<PageFunction>;
32
33static mut PAGE_CACHE: PageCache = new_cache();
34static DEFAULT_PAGE: PageFunction = index::index;
35
36pub fn register_page(name: &str, component_fxn: PageFunction) {
37    let key = RUMString::from(name);
38    let _ = rumtk_cache_push!(&raw mut PAGE_CACHE, &key, &component_fxn);
39}
40
41pub fn get_page(name: &str) -> PageCacheItem {
42    rumtk_cache_get!(&raw mut PAGE_CACHE, &RUMString::from(name), &DEFAULT_PAGE)
43}
44
45pub fn init_pages(user_components: &UserPages) {
46    /* Init any user prescribed components */
47    for itm in user_components {
48        let (name, value) = itm;
49        register_page(name, *value);
50    }
51}
52
53#[macro_export]
54macro_rules! rumtk_web_register_page {
55    ( $key:expr, $fxn:expr ) => {{
56        use $crate::pages::register_page;
57        register_page($key, $fxn)
58    }};
59}
60
61#[macro_export]
62macro_rules! rumtk_web_get_page {
63    ( $key:expr ) => {{
64        use $crate::pages::get_page;
65        get_page($key)
66    }};
67}
68
69#[macro_export]
70macro_rules! rumtk_web_init_pages {
71    ( $pages:expr ) => {{
72        use $crate::pages::init_pages;
73        init_pages($pages)
74    }};
75}
76
77#[macro_export]
78macro_rules! rumtk_web_collect_page {
79    ( $page:expr, $app_state:expr ) => {{
80        use $crate::rumtk_web_get_page;
81
82        let page = rumtk_web_get_page!(&$page);
83
84        page($app_state.clone())
85    }};
86}