rumtk_web/utils/
matcher.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::components::app_shell::app_shell;
24use crate::utils::defaults::DEFAULT_ROBOT_TXT;
25use crate::utils::types::SharedAppState;
26use crate::utils::{HTMLResult, RUMString};
27use crate::{rumtk_web_get_component, RUMWebData};
28use axum::response::Html;
29
30pub async fn default_robots_matcher(
31    _path: Vec<RUMString>,
32    _params: RUMWebData,
33    _state: SharedAppState,
34) -> HTMLResult {
35    Ok(Html::<String>::from(String::from(DEFAULT_ROBOT_TXT)))
36}
37
38pub async fn default_page_matcher(
39    path: Vec<RUMString>,
40    params: RUMWebData,
41    state: SharedAppState,
42) -> HTMLResult {
43    let path_components = match path.first() {
44        Some(x) => x.split('/').collect::<Vec<&str>>(),
45        None => Vec::new(),
46    };
47
48    // Do not minify the page. we saved 0.3KB but transfer went from 5ms to 45ms
49    app_shell(&path_components, &params, state)
50}
51
52pub async fn default_component_matcher(
53    path: Vec<RUMString>,
54    params: RUMWebData,
55    state: SharedAppState,
56) -> HTMLResult {
57    let path_components = match path.first() {
58        Some(x) => x.split('/').collect::<Vec<&str>>(),
59        None => Vec::new(),
60    };
61
62    let component = match path_components.first() {
63        Some(component) => component,
64        None => return Err(RUMString::from("Missing component name to fetch!")),
65    };
66
67    let component = rumtk_web_get_component!(component);
68
69    component(&path_components[1..], &params, state)
70}
71
72#[macro_export]
73macro_rules! rumtk_web_fetch {
74    ( $matcher:expr ) => {{
75        use axum::extract::{Path, Query, State};
76        use axum::response::Html;
77        use $crate::utils::types::{RouterAppState, RouterComponents, RouterParams};
78
79        async |Path(path_params): RouterComponents,
80               Query(params): RouterParams,
81               State(state): RouterAppState|
82               -> Html<String> {
83            match $matcher(path_params, params, state).await {
84                Ok(res) => res,
85                Err(e) => {
86                    error!("{}", e);
87                    return Html(String::default());
88                }
89            }
90        }
91    }};
92}
93
94#[macro_export]
95macro_rules! rumtk_web_post {
96    ( $matcher:expr ) => {{
97        use axum::extract::{Multipart, Path, Query, State};
98        use axum::response::Html;
99        use $crate::utils::types::{RouterAppState, RouterComponents, RouterForm, RouterParams};
100
101        async |Path(path_params): RouterComponents,
102               Query(mut params): RouterParams,
103               State(state): RouterAppState,
104               mut Multipart: RouterForm|
105               -> Html<String> {
106            match $matcher(path_params, params, state).await {
107                Ok(res) => res,
108                Err(e) => {
109                    error!("{}", e);
110                    return Html(String::default());
111                }
112            }
113        }
114    }};
115}