rumtk_web/components/Form/
form_utils.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};
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 type FormElements = Vec<RUMString>;
31pub type FormCache = LazyRUMCache<RUMString, FormElements>;
32pub type FormElementBuilder =
33    fn(element: &str, data: &str, props: InputProps, css: &str) -> RUMString;
34pub type FormBuilderFunction = fn(builder: FormElementBuilder) -> FormElements;
35
36static mut form_cache: FormCache = new_cache();
37
38fn new_form_entry(name: &RUMString) -> FormElements {
39    vec![]
40}
41
42fn build_form_element(element: &str, data: &str, props: InputProps, css: &str) -> RUMString {
43    rumtk_web_render_component!(|| -> HTMLResult { form_element(element, data, props, css) })
44}
45
46pub fn register_form_elements(name: &str, element_builder: FormBuilderFunction) -> &FormElements {
47    let key = RUMString::from(name);
48    rumtk_cache_fetch!(&mut form_cache, &key, new_form_entry);
49    let data = element_builder(build_form_element);
50    rumtk_cache_push!(&mut form_cache, &key, &data)
51}
52
53pub fn get_form(name: &str) -> &FormElements {
54    rumtk_cache_fetch!(&mut form_cache, &RUMString::from(name), new_form_entry)
55}