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