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::rumtk_web_get_component;
25use crate::utils::defaults::DEFAULT_ROBOT_TXT;
26use crate::utils::types::SharedAppConf;
27use crate::utils::{HTMLResult, RUMString};
28use axum::response::Html;
29use std::collections::HashMap;
30
31pub async fn default_robots_matcher(
32    _path: Vec<RUMString>,
33    _params: HashMap<RUMString, RUMString>,
34    _state: SharedAppConf,
35) -> HTMLResult {
36    Ok(Html::<String>::from(String::from(DEFAULT_ROBOT_TXT)))
37}
38
39pub async fn default_page_matcher(
40    path: Vec<RUMString>,
41    params: HashMap<RUMString, RUMString>,
42    state: SharedAppConf,
43) -> HTMLResult {
44    let path_components = match path.first() {
45        Some(x) => x.split('/').collect::<Vec<&str>>(),
46        None => Vec::new(),
47    };
48
49    // Do not minify the page. we saved 0.3KB but transfer went from 5ms to 45ms
50    app_shell(&path_components, &params, state.clone())
51}
52
53pub async fn default_component_matcher(
54    path: Vec<RUMString>,
55    params: HashMap<RUMString, RUMString>,
56    state: SharedAppConf,
57) -> HTMLResult {
58    let path_components = match path.first() {
59        Some(x) => x.split('/').collect::<Vec<&str>>(),
60        None => Vec::new(),
61    };
62
63    let component = match path_components.first() {
64        Some(component) => component,
65        None => return Err(RUMString::from("Missing component name to fetch!")),
66    };
67
68    let component = rumtk_web_get_component!(component);
69
70    component(&path_components[1..], &params, state.clone())
71}