rumtk_web/utils/
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 app;
22pub mod conf;
23pub mod defaults;
24pub mod matcher;
25pub mod render;
26pub mod types;
27
28pub use render::*;
29pub use types::*;
30
31#[macro_export]
32macro_rules! rumtk_web_get_text_item {
33    ( $store:expr, $item:expr, $default:expr) => {{
34        match $store.get($item) {
35            Some(x) => x,
36            None => $default,
37        }
38    }};
39}
40
41#[macro_export]
42macro_rules! rumtk_web_get_param_eq {
43    ( $params:expr, $indx:expr, $comparison:expr, $default:expr ) => {{
44        match $params.get($indx) {
45            Some(x) => *x == $comparison,
46            None => $default,
47        }
48    }};
49}
50
51#[macro_export]
52macro_rules! rumtk_web_get_param {
53    ( $params:expr, $indx:expr, $default:expr ) => {{
54        match $params.get($indx) {
55            Some(x) => x.parse().unwrap_or($default),
56            None => $default,
57        }
58    }};
59}
60
61#[macro_export]
62macro_rules! rumtk_web_params_map {
63    ( $params:expr ) => {{
64        use std::collections::HashMap;
65        let mut params = HashMap::<RUMString, RUMString>::with_capacity($params.len());
66
67        for (k, v) in $params.iter() {
68            params.insert(
69                RUMString::from(k.to_string()),
70                RUMString::from(v.to_string()),
71            );
72        }
73        params
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        use $crate::utils::types::{PageFunction, RenderedPageComponents};
82
83        let page = match rumtk_web_get_page!(&$page) {
84            Some(x) => x,
85            None => &(|_| -> RenderedPageComponents { vec![] } as PageFunction),
86        };
87
88        page($app_state.clone())
89    }};
90}
91
92#[macro_export]
93macro_rules! rumtk_web_fetch {
94    ( $matcher:expr ) => {{
95        use axum::extract::{Path, Query, State};
96        use axum::response::Html;
97        use $crate::utils::types::{RouterAppConf, RouterComponents, RouterParams};
98
99        async |Path(path_params): RouterComponents,
100               Query(params): RouterParams,
101               State(state): RouterAppConf|
102               -> Html<String> {
103            match $matcher(path_params, params, state).await {
104                Ok(res) => res,
105                Err(e) => {
106                    error!("{}", e);
107                    return Html(String::default());
108                }
109            }
110        }
111    }};
112}