Skip to main content

rumtk_web/components/form/
select_element.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) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20use crate::components::form::form_node::{FormNode, ToFormNode};
21use crate::components::form::props::InputProps;
22use crate::{ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
23
24type SelectOptions<'a> = Vec<(&'a str, &'a str)>;
25
26#[derive(RUMWebTemplate, Debug, Clone)]
27#[template(
28    source = "
29        <select size='{{ count }}' class='{{css_class}}' {{props|safe}}>
30          {% for (item_value, item_text) in items %}
31            <option value='{{item_value}}'>{{item_text}}</option>
32          {% endfor %}
33        </select>
34    ",
35    ext = "html"
36)]
37pub struct SelectElement<'a> {
38    items: SelectOptions<'a>,
39    props: &'a str,
40    count: usize,
41    css_class: &'a str
42}
43
44impl RUMWebTemplateSafe for SelectElement<'_> {}
45
46fn parse_select_data(data: &str) -> Vec<(&str, &str)> {
47    let rows: Vec<&str> = data.split(",").collect();
48    let mut result: SelectOptions = vec![];
49
50    for item in rows {
51        let pair: Vec<&str> = item.split("=").collect();
52
53        let r = match pair.len() {
54            0 => {
55                return result;
56            },
57            1 => {
58                let val = pair[0];
59                (val, val)
60            },
61            _ => {
62                let val = pair[0];
63                let text = pair[1];
64
65                (val, text)
66            }
67        };
68
69        result.push(r);
70    }
71
72    result
73}
74
75impl ToFormNode<SelectElement<'_>> for SelectElement<'_> {}
76
77pub fn select_element(_element: &str, data: &str, props: InputProps, css_class: &str) -> ComponentResult<FormNode> {
78    let items = parse_select_data(data);
79    let count = match props.multiple {
80        true => items.len(),
81        false => 1,
82    };
83
84    Ok(SelectElement {
85        items,
86        props: &props.to_string().replace("\\\\", "\\"),
87        count,
88        css_class,
89    }.to_form_node())
90}