Skip to main content

rumtk_web/utils/
types.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. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21pub use super::conf::*;
22use axum::extract::{Multipart, Path, Query};
23use phf::Map;
24pub use rumtk_core::strings::RUMString;
25pub use rumtk_core::strings::{AsStr, CompactStringExt, RUMStringConversions, StringLike};
26use rumtk_core::types::{RUMHashMap, RUMID};
27use std::fmt::{Debug, Display};
28use std::sync::Arc;
29
30pub type RUMWebData = RUMHashMap<RUMString, RUMString>;
31pub type URLPath<'a, 'b> = &'a [&'b str];
32pub type AsyncURLPath = Arc<Vec<RUMString>>;
33pub type URLParams<'a> = &'a RUMWebData;
34pub type AsyncURLParams = Arc<RUMWebData>;
35
36/* Responses */
37pub use crate::utils::response::*;
38
39pub type RenderedPageComponents = Vec<RUMString>;
40pub type RenderedPageComponentsResult = RUMResult<RenderedPageComponents>;
41/* Router Match Types */
42pub type RouterComponents = Path<Vec<RUMString>>;
43pub type RouterParams = Query<RUMWebData>;
44pub type RouterForm = Multipart;
45
46/* Config Types */
47pub type ComponentFunction = fn(URLPath, URLParams, SharedAppState) -> HTMLResult;
48pub type PageFunction = fn(SharedAppState) -> RenderedPageComponentsResult;
49pub type ComponentMap = Map<&'static str, ComponentFunction>;
50pub type PageMap = Map<&'static str, PageFunction>;
51
52/* API Types */
53pub use crate::utils::form_data::{FormBuffer, FormData};
54pub type RouterAPIPath = Path<RUMString>;
55pub type APIPath = RUMString;
56pub type APIFunction = fn(APIPath, RUMWebData, FormData, SharedAppState) -> HTMLResult;
57
58pub use askama::Template as RUMWebTemplate;
59use rumtk_core::core::RUMResult;
60
61
62///
63/// ```
64/// use rumtk_web::RUMWebDataProxy;
65///
66/// let data = [("", "")];
67/// let result = RUMWebDataProxy::from(&data);
68///
69/// ```
70///
71pub struct RUMWebDataProxy(RUMWebData);
72
73impl RUMWebDataProxy {
74    pub fn get_inner(&self) -> &RUMWebData {
75        &self.0
76    }
77
78    pub fn from_params<K, V, const N: usize>(data: &[(K, V); N]) -> Self
79    where
80        K: Display + Sized,
81        V: Display + Sized,
82    {
83        let mut new_params = RUMWebData::with_capacity(data.len());
84
85        for (k, v) in data.iter() {
86            new_params.insert(
87                RUMString::from(k.to_string()),
88                RUMString::from(v.to_string()),
89            );
90        }
91        RUMWebDataProxy(new_params)
92    }
93}
94
95impl From<&RUMWebData> for RUMWebDataProxy {
96    fn from(data: &RUMWebData) -> Self {
97        RUMWebDataProxy(data.clone())
98    }
99}
100
101impl<const N: usize> From<&&[(&str, &str); N]> for RUMWebDataProxy {
102    fn from(data: &&[(&str, &str); N]) -> Self {
103        Self::from_params(data)
104    }
105}
106
107impl<const N: usize> From<&[(&str, &str); N]> for RUMWebDataProxy {
108    fn from(data: &[(&str, &str); N]) -> Self {
109        Self::from_params(data)
110    }
111}
112
113impl<const N: usize> From<&[(&str, &RUMString); N]> for RUMWebDataProxy {
114    fn from(data: &[(&str, &RUMString); N]) -> Self {
115        Self::from_params(data)
116    }
117}
118
119impl<const N: usize> From<&[(&str, RUMID); N]> for RUMWebDataProxy {
120    fn from(data: &[(&str, RUMID); N]) -> Self {
121        Self::from_params(data)
122    }
123}