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.
5 * Copyright (C) 2025  MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21use rumtk_core::cache::{new_cache, LazyRUMCache, LazyRUMCacheValue};
22use rumtk_core::strings::RUMString;
23use rumtk_core::{rumtk_cache_fetch, rumtk_cache_push};
24
25use crate::components::form::form_element::form_element;
26use crate::components::form::props::InputProps;
27use crate::rumtk_web_render_component;
28use crate::utils::HTMLResult;
29
30pub mod form;
31pub mod form_element;
32pub mod props;
33
34pub type FormElements = Vec<RUMString>;
35pub type FormCache = LazyRUMCache<RUMString, FormElements>;
36pub type FormElementBuilder =
37    fn(element: &str, data: &str, props: InputProps, css: &str) -> RUMString;
38pub type FormBuilderFunction = fn(builder: FormElementBuilder) -> FormElements;
39pub type FormCacheItem = LazyRUMCacheValue<FormElements>;
40
41static mut FORM_CACHE: FormCache = new_cache();
42static DEFAULT_FORMELEMENTS: FormElements = vec![];
43
44fn new_form_entry(_name: &RUMString) -> FormElements {
45    DEFAULT_FORMELEMENTS.clone()
46}
47
48fn build_form_element(element: &str, data: &str, props: InputProps, css: &str) -> RUMString {
49    rumtk_web_render_component!(|| -> HTMLResult { form_element(element, data, props, css) })
50}
51
52pub fn register_form_elements(name: &str, element_builder: FormBuilderFunction) -> FormCacheItem {
53    let key = RUMString::from(name);
54    let _ = rumtk_cache_fetch!(&raw mut FORM_CACHE, &key, new_form_entry);
55    let data = element_builder(build_form_element);
56    rumtk_cache_push!(&raw mut FORM_CACHE, &key, &data)
57}
58
59pub fn get_form(name: &str) -> FormCacheItem {
60    rumtk_cache_fetch!(&raw mut FORM_CACHE, &RUMString::from(name), new_form_entry)
61}
62
63///
64/// This is an API macro for defining a form that can be used to render it later in your web pages.
65///
66#[macro_export]
67macro_rules! rumtk_web_add_form {
68    ( $name:expr, $build_fxn:expr ) => {{
69        use $crate::components::form::register_form_elements;
70
71        register_form_elements($name, $build_fxn)
72    }};
73}
74
75///
76/// This is an API macro to get the list of rendered elements that will be fed into the form shell
77/// to render your form in your web page.
78///
79#[macro_export]
80macro_rules! rumtk_web_get_form {
81    ( $name:expr ) => {{
82        use $crate::components::form::get_form;
83
84        get_form($name)
85    }};
86}