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, OrderedMap};
24pub use rumtk_core::strings::RUMString;
25pub use rumtk_core::strings::{AsStr, RUMStringConversions, StringLike};
26use rumtk_core::types::{RUMHashMap, RUMID};
27use std::fmt::Display;
28use std::ops::Deref;
29use std::sync::Arc;
30
31pub type RUMWebData = RUMHashMap<RUMString, RUMString>;
32pub type URLPath<'a, 'b> = &'a [&'b str];
33pub type AsyncURLPath = Arc<Vec<RUMString>>;
34pub type URLParams<'a> = &'a RUMWebData;
35pub type AsyncURLParams = Arc<RUMWebData>;
36
37/* Responses */
38pub use crate::utils::response::*;
39
40pub type RenderedPageComponents = Vec<RUMString>;
41pub type RenderedPageComponentsResult = RUMResult<RenderedPageComponents>;
42/* Router Match Types */
43pub type RouterComponents = Path<Vec<RUMString>>;
44pub type RouterParams = Query<RUMWebData>;
45pub type RouterForm = Multipart;
46
47/* Config Types */
48pub type PageFunction = fn(SharedAppState) -> RenderedPageComponentsResult;
49pub type PageMap = Map<&'static str, PageFunction>;
50
51/* API Types */
52pub use crate::utils::form_data::{FormBuffer, FormData};
53pub type RouterAPIPath = Path<RUMString>;
54pub type APIPath = RUMString;
55pub type APIFunction = fn(APIPath, RUMWebData, FormData, SharedAppState) -> HTMLResult;
56
57/* Conf Types */
58pub type TextMap = RUMOrderedMap<RUMString, RUMString>;
59pub type NestedTextMap = RUMOrderedMap<RUMString, TextMap>;
60pub type NestedNestedTextMap = RUMOrderedMap<RUMString, NestedTextMap>;
61pub type RootNestedNestedTextMap = RUMOrderedMap<RUMString, NestedNestedTextMap>;
62
63pub type ConstTextMap = OrderedMap<&'static str, &'static str>;
64pub type ConstNestedTextMap = OrderedMap<&'static str, &'static ConstTextMap>;
65pub type ConstNestedNestedTextMap = OrderedMap<&'static str, &'static ConstNestedTextMap>;
66
67pub type PipelineGroup = RUMHashMap<RUMString, RUMCommandLine>;
68
69pub use askama::{Template as RUMWebTemplate, filters::HtmlSafe as RUMWebTemplateSafe};
70
71use rumtk_core::base::RUMResult;
72use rumtk_core::pipelines::pipeline_types::RUMCommandLine;
73use rumtk_core::serde::RUMOrderedMap;
74
75///
76/// ```
77/// use rumtk_web::RUMWebDataProxy;
78///
79/// let data = [("", "")];
80/// let result = RUMWebDataProxy::from(&data);
81///
82/// ```
83///
84#[derive(Debug, Clone)]
85pub struct RUMWebDataProxy(RUMWebData);
86
87impl RUMWebDataProxy {
88    #[inline]
89    pub fn get_inner(&self) -> &RUMWebData {
90        &self.0
91    }
92
93    #[inline]
94    pub fn consume(mut self) -> RUMWebData {
95        self.0
96    }
97
98    pub fn from_params<K, V, const N: usize>(data: &[(K, V); N]) -> Self
99    where
100        K: Display + Sized,
101        V: Display + Sized,
102    {
103        let mut new_params = RUMWebData::with_capacity(data.len());
104
105        for (k, v) in data.iter() {
106            new_params.insert(
107                RUMString::from(k.to_string()),
108                RUMString::from(v.to_string()),
109            );
110        }
111        RUMWebDataProxy(new_params)
112    }
113}
114
115impl Deref for RUMWebDataProxy {
116    type Target = RUMWebData;
117
118    fn deref(&self) -> &Self::Target {
119        &self.0
120    }
121}
122
123impl From<&RUMWebData> for RUMWebDataProxy {
124    fn from(data: &RUMWebData) -> Self {
125        RUMWebDataProxy(data.clone())
126    }
127}
128
129impl<const N: usize> From<&&[(&str, &str); N]> for RUMWebDataProxy {
130    fn from(data: &&[(&str, &str); N]) -> Self {
131        Self::from_params(data)
132    }
133}
134
135impl<const N: usize> From<&[(&str, &str); N]> for RUMWebDataProxy {
136    fn from(data: &[(&str, &str); N]) -> Self {
137        Self::from_params(data)
138    }
139}
140
141impl<const N: usize> From<&[(&str, &RUMString); N]> for RUMWebDataProxy {
142    fn from(data: &[(&str, &RUMString); N]) -> Self {
143        Self::from_params(data)
144    }
145}
146
147impl<const N: usize> From<&[(&str, RUMString); N]> for RUMWebDataProxy {
148    fn from(data: &[(&str, RUMString); N]) -> Self {
149        Self::from_params(data)
150    }
151}
152
153impl<const N: usize> From<&[(&str, RUMID); N]> for RUMWebDataProxy {
154    fn from(data: &[(&str, RUMID); N]) -> Self {
155        Self::from_params(data)
156    }
157}