Skip to main content

rumtk_web/components/form/
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. <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 */
21use crate::components::form::form_element::form_element;
22use crate::components::form::form_node::FormNode;
23use crate::components::form::props::InputProps;
24use crate::{rumtk_web_get_config, sanitizer_update_tag_attributes, ComponentResult, SharedAppState};
25use rumtk_core::base::RUMResult;
26use rumtk_core::cache::{new_cache, LazyRUMCache};
27use rumtk_core::strings::RUMString;
28use rumtk_core::{rumtk_cache_fetch, rumtk_cache_push};
29
30pub mod form_element;
31pub mod props;
32pub mod input_element;
33pub mod select_element;
34pub mod form;
35pub mod form_node;
36pub mod captchas;
37
38pub type FormElements = Vec<ComponentResult<FormNode>>;
39pub type FormCache = LazyRUMCache<RUMString, FormElements>;
40pub type FormElementBuilder =
41    fn(element: &str, data: &str, props: InputProps, css: &str) -> ComponentResult<FormNode>;
42pub type FormBuilderFunction = fn(builder: FormElementBuilder, state: &SharedAppState) -> FormElements;
43pub type FormItem<'a> = (&'a str, FormBuilderFunction);
44pub type Forms<'a> = Vec<FormItem<'a>>;
45pub type FormCacheItem = FormElements;
46
47static mut FORM_CACHE: FormCache = new_cache();
48static DEFAULT_FORMELEMENTS: FormElements = vec![];
49
50pub const CAPTCHA_SANITIZER_TAG: &str = "div";
51pub const CAPTCHA_SANITIZER_ATTRIBUTES: &[&str] = &[
52    "data-sitekey",
53    "data-action",
54    "data-callback",
55    "data-expired-callback",
56];
57
58fn new_form_entry() -> RUMResult<FormElements> {
59    Ok(DEFAULT_FORMELEMENTS.clone())
60}
61
62#[inline]
63fn build_form_element(element: &str, data: &str, props: InputProps, css: &str) -> ComponentResult<FormNode> {
64    form_element(element, data, props, css)
65}
66
67pub fn register_form_elements(name: &str, element_builder: &FormBuilderFunction, state: &SharedAppState) {
68    let key = RUMString::from(name);
69    let _ = rumtk_cache_fetch!(&raw mut FORM_CACHE, &key, new_form_entry);
70    let data = element_builder(build_form_element, state);
71    rumtk_cache_push!(&raw mut FORM_CACHE, &key, data);
72}
73
74pub fn register_forms(forms: Option<Forms>, state: &SharedAppState) {
75    if rumtk_web_get_config!(state).flags.enable_captcha {
76        sanitizer_update_tag_attributes(CAPTCHA_SANITIZER_TAG, CAPTCHA_SANITIZER_ATTRIBUTES, true);
77    }
78
79    for (form_name, form_builder) in forms.unwrap_or_default() {
80        register_form_elements(form_name, &form_builder, &state);
81    }
82}
83
84pub fn get_form(name: &str) -> RUMResult<FormCacheItem> {
85    rumtk_cache_fetch!(&raw mut FORM_CACHE, &RUMString::from(name), new_form_entry)
86}
87
88///
89/// This is an API macro for defining a form that can be used to render it later in your web pages.
90///
91#[macro_export]
92macro_rules! rumtk_web_init_forms {
93    ( $forms:expr, $state:expr ) => {{
94        use $crate::components::form::register_forms;
95
96        register_forms($forms, &$state.clone());
97    }};
98}
99
100///
101/// This is an API macro to get the list of rendered elements that will be fed into the form shell
102/// to render your form in your web page.
103///
104#[macro_export]
105macro_rules! rumtk_web_get_form {
106    ( $name:expr ) => {{
107        use $crate::components::form::get_form;
108
109        get_form($name)
110    }};
111}