Skip to main content

rumtk_web/components/html/
link.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 */
20
21use crate::defaults::{DEFAULT_NO_TEXT, PARAMS_SOURCE_URL, PARAMS_TYPE};
22use crate::utils::types::{SharedAppState, URLParams, URLPath};
23use crate::{rumtk_web_get_text_item, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe};
24
25#[derive(RUMWebTemplate, Debug)]
26#[template(
27    source = "
28        <link rel='preload' href='{{url}}' onload='this.rel=\'{{typ}}\'' onerror='this.onerror=null;this.href=\'/static/{{file}}\';'>
29        <noscript><link rel='{{typ}}' href='{{url}}'></noscript>
30    ",
31    ext = "html"
32)]
33pub struct Link<'a> {
34    typ: &'a str,
35    url: &'a str,
36    file: &'a str
37}
38
39impl RUMWebTemplateSafe for Link<'_> {}
40
41pub fn link<'a>(_path_components: URLPath<'a, 'a>, params: URLParams<'a>, state: SharedAppState) -> ComponentResult<Link<'a>> {
42    let typ = rumtk_web_get_text_item!(params, PARAMS_TYPE, DEFAULT_NO_TEXT);
43    let url = rumtk_web_get_text_item!(params, PARAMS_SOURCE_URL, DEFAULT_NO_TEXT);
44
45    let path_components = url.rsplit('/').collect::<Vec<&str>>();
46    let file = match path_components.last() {
47        Some(f) => f,
48        None => DEFAULT_NO_TEXT
49    };
50
51    Ok(Link {
52        typ,
53        url,
54        file
55    })
56}