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::props::InputProps;
21use crate::utils::types::HTMLResult;
22use crate::{rumtk_web_render_template, RUMWebTemplate};
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
44fn parse_select_data(data: &str) -> Vec<(&str, &str)> {
45    let rows: Vec<&str> = data.split(",").collect();
46    let mut result: SelectOptions = vec![];
47
48    for item in rows {
49        let pair: Vec<&str> = item.split("=").collect();
50
51        let r = match pair.len() {
52            0 => {
53                return result;
54            },
55            1 => {
56                let val = pair[0];
57                (val, val)
58            },
59            _ => {
60                let val = pair[0];
61                let text = pair[1];
62
63                (val, text)
64            }
65        };
66
67        result.push(r);
68    }
69
70    result
71}
72
73pub fn select_element(_element: &str, data: &str, props: InputProps, css_class: &str) -> HTMLResult {
74    let items = parse_select_data(data);
75    let count = match props.multiple {
76        true => items.len(),
77        false => 1,
78    };
79
80    rumtk_web_render_template!(SelectElement {
81        items,
82        props: &props.to_rumstring().replace("\\\\", "\\"),
83        count,
84        css_class,
85    })
86}