Skip to main content

rumtk_web/components/
formatted_label.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. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21use crate::components::html::{markdown, Markdown};
22use crate::defaults::PARAMS_CONTENTS;
23use crate::utils::defaults::{
24    DEFAULT_NO_TEXT, DEFAULT_TEXT_ITEM, PARAMS_CSS_CLASS, PARAMS_TYPE, SECTION_TEXT,
25};
26use crate::utils::types::{SharedAppState, URLParams, URLPath};
27use crate::utils::DEFAULT_TEXTMAP;
28use crate::{rumtk_web_get_config, rumtk_web_get_config_string, rumtk_web_get_text_item, rumtk_web_params_map, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
29use rumtk_core::strings::RUMString;
30
31#[derive(RUMWebTemplate, Debug, Clone)]
32#[template(
33    source = "
34        {% if custom_css_enabled %}
35            <link href='/static/components/formatted_label.css' rel='stylesheet'>
36        {% endif %}
37        <div class='formatted-label-{{css_class}}'>
38            {{md}}
39        </div>
40    ",
41    ext = "html"
42)]
43pub struct FormattedLabel {
44    md: Markdown,
45    css_class: RUMString,
46    custom_css_enabled: bool,
47}
48
49impl RUMWebTemplateSafe for FormattedLabel {}
50
51pub fn formatted_label<'a>(
52    _path_components: URLPath<'a, 'a>,
53    params: URLParams<'a>,
54    state: SharedAppState,
55) -> ComponentResult<FormattedLabel> {
56    let typ = rumtk_web_get_text_item!(params, PARAMS_TYPE, DEFAULT_TEXT_ITEM);
57    let css_class = rumtk_web_get_text_item!(params, PARAMS_CSS_CLASS, DEFAULT_TEXT_ITEM).to_string();
58
59    let custom_css_enabled = rumtk_web_get_config!(state).flags.custom_css;
60
61    let text_store = rumtk_web_get_config_string!(state, SECTION_TEXT);
62    let itm = rumtk_web_get_text_item!(&text_store, typ, &DEFAULT_TEXTMAP);
63    let desc = rumtk_web_get_text_item!(&itm, "description", DEFAULT_NO_TEXT);
64
65    let md_params = rumtk_web_params_map!([(PARAMS_CONTENTS, desc)]);
66    let html = markdown(
67        _path_components,
68        md_params.get_inner(),
69        state.clone()
70    )?;
71
72    Ok(FormattedLabel {
73        md: html,
74        css_class,
75        custom_css_enabled
76    })
77}