rumtk_web/utils/
render.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.
5 * Copyright (C) 2026  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 crate::utils::types::HTMLResult;
22use axum::response::Html;
23use rumtk_core::strings::rumtk_format;
24
25pub fn rumtk_web_render_html<T: askama::Template>(template: T) -> HTMLResult {
26    let result = template.render();
27    match result {
28        Ok(html) => Ok(Html(html)),
29        Err(e) => {
30            let tn = std::any::type_name::<T>();
31            Err(rumtk_format!("Template {tn} render failed: {e:?}"))
32        }
33    }
34}
35
36#[macro_export]
37macro_rules! rumtk_web_render_component {
38    ( $component_fxn:expr ) => {{
39        use rumtk_core::strings::RUMStringConversions;
40        match $component_fxn() {
41            Ok(x) => x.0.to_rumstring(),
42            _ => RUMString::default(),
43        }
44    }};
45    ( $component_fxn:expr, $app_state:expr ) => {{
46        use rumtk_core::strings::RUMStringConversions;
47        match $component_fxn($app_state.clone()) {
48            Ok(x) => x.0.to_rumstring(),
49            _ => RUMString::default(),
50        }
51    }};
52    ( $component:expr, $params:expr, $app_state:expr ) => {{
53        use rumtk_core::strings::RUMStringConversions;
54        use $crate::components::div::div;
55        use $crate::rumtk_web_get_component;
56        use $crate::rumtk_web_params_map;
57        use $crate::utils::types::ComponentFunction;
58        let component = match rumtk_web_get_component!($component) {
59            Some(x) => x,
60            None => &(div as ComponentFunction),
61        };
62
63        match component(&[], &rumtk_web_params_map!($params), $app_state.clone()) {
64            Ok(x) => x.0.to_rumstring(),
65            _ => RUMString::default(),
66        }
67    }};
68}
69
70#[macro_export]
71macro_rules! rumtk_web_render_html {
72    ( $component:expr ) => {{
73        use $crate::utils::{rumtk_web_render_html, types::HTMLResult};
74
75        let closure = || -> HTMLResult { rumtk_web_render_html($component) };
76
77        closure()
78    }};
79}
80
81///
82///
83/// If using raw strings, do not leave an extra line. The first input must have characters or you will get <pre><code> blocks regardless of what you do.
84///
85#[macro_export]
86macro_rules! rumtk_web_render_markdown {
87    ( $md:expr ) => {{
88        use pulldown_cmark::{Options, Parser};
89        use rumtk_core::strings::RUMStringConversions;
90
91        let mut options = Options::empty();
92        options.insert(Options::ENABLE_STRIKETHROUGH);
93        options.insert(Options::ENABLE_TASKLISTS);
94        options.insert(Options::ENABLE_MATH);
95        options.insert(Options::ENABLE_TABLES);
96        options.insert(Options::ENABLE_WIKILINKS);
97
98        let input = String::from($md);
99        let parser = Parser::new_ext(&input, options);
100        let mut html_output = String::new();
101        pulldown_cmark::html::push_html(&mut html_output, parser);
102        println!("{}", &html_output);
103
104        html_output.to_rumstring()
105    }};
106}