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  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 rumtk_core::cache::{new_cache, LazyRUMCache, LazyRUMCacheValue};
24use rumtk_core::strings::RUMString;
25use rumtk_core::{rumtk_cache_fetch, rumtk_cache_push};
26
27use crate::components::form::form_element::form_element;
28use crate::components::form::props::InputProps;
29use crate::rumtk_web_render_component;
30use crate::utils::HTMLResult;
31
32pub mod form;
33pub mod form_element;
34pub mod props;
35
36pub type FormElements = Vec<RUMString>;
37pub type FormCache = LazyRUMCache<RUMString, FormElements>;
38pub type FormElementBuilder =
39    fn(element: &str, data: &str, props: InputProps, css: &str) -> RUMString;
40pub type FormBuilderFunction = fn(builder: FormElementBuilder) -> FormElements;
41pub type FormCacheItem = LazyRUMCacheValue<FormElements>;
42
43static mut FORM_CACHE: FormCache = new_cache();
44static DEFAULT_FORMELEMENTS: FormElements = vec![];
45
46fn new_form_entry(_name: &RUMString) -> FormElements {
47    DEFAULT_FORMELEMENTS.clone()
48}
49
50fn build_form_element(element: &str, data: &str, props: InputProps, css: &str) -> RUMString {
51    rumtk_web_render_component!(|| -> HTMLResult { form_element(element, data, props, css) })
52}
53
54pub fn register_form_elements(name: &str, element_builder: FormBuilderFunction) -> FormCacheItem {
55    let key = RUMString::from(name);
56    let _ = rumtk_cache_fetch!(&raw mut FORM_CACHE, &key, new_form_entry);
57    let data = element_builder(build_form_element);
58    rumtk_cache_push!(&raw mut FORM_CACHE, &key, &data)
59}
60
61pub fn get_form(name: &str) -> FormCacheItem {
62    rumtk_cache_fetch!(&raw mut FORM_CACHE, &RUMString::from(name), new_form_entry)
63}
64
65///
66/// This is an API macro for defining a form that can be used to render it later in your web pages.
67///
68#[macro_export]
69macro_rules! rumtk_web_add_form {
70    ( $name:expr, $build_fxn:expr ) => {{
71        use $crate::components::form::register_form_elements;
72
73        register_form_elements($name, $build_fxn)
74    }};
75}
76
77///
78/// This is an API macro to get the list of rendered elements that will be fed into the form shell
79/// to render your form in your web page.
80///
81#[macro_export]
82macro_rules! rumtk_web_get_form {
83    ( $name:expr ) => {{
84        use $crate::components::form::get_form;
85
86        get_form($name)
87    }};
88}