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) 2025  Luis M. Santos, M.D.
5 * Copyright (C) 2025  Nick Stephenson
6 * Copyright (C) 2025  Ethan Dixon
7 * Copyright (C) 2025  MedicalMasses L.L.C.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23use crate::types::HTMLResult;
24use crate::RUMWebRedirect;
25use rumtk_core::strings::rumtk_format;
26
27pub fn rumtk_web_render_html<T: askama::Template>(template: T, url: RUMWebRedirect) -> HTMLResult {
28    let result = template.render();
29    match result {
30        Ok(html) => Ok(url.into_web_response(Some(html))),
31        Err(e) => {
32            let tn = std::any::type_name::<T>();
33            Err(rumtk_format!("Template {tn} render failed: {e:?}"))
34        }
35    }
36}
37
38pub fn rumtk_web_redirect(url: RUMWebRedirect) -> HTMLResult {
39    Ok(url.into_web_response(Some(String::default())))
40}
41
42#[macro_export]
43macro_rules! rumtk_web_render_component {
44    ( $component_fxn:expr ) => {{
45        use rumtk_core::strings::{RUMString, RUMStringConversions};
46        match $component_fxn() {
47            Ok(x) => x.to_rumstring(),
48            _ => RUMString::default(),
49        }
50    }};
51    ( $component_fxn:expr, $app_state:expr ) => {{
52        use rumtk_core::strings::{RUMString, RUMStringConversions};
53        match $component_fxn($app_state.clone()) {
54            Ok(x) => x.to_rumstring(),
55            _ => RUMString::default(),
56        }
57    }};
58    ( $component:expr, $params:expr, $app_state:expr ) => {{
59        use rumtk_core::strings::{RUMString, RUMStringConversions};
60        use $crate::{rumtk_web_get_component, rumtk_web_params_map};
61
62        let component = rumtk_web_get_component!($component);
63
64        match component(&[""], &rumtk_web_params_map!($params), $app_state.clone()) {
65            Ok(x) => x.to_rumstring(),
66            _ => RUMString::default(),
67        }
68    }};
69}
70
71#[macro_export]
72macro_rules! rumtk_web_render_html {
73    ( $page:expr ) => {{
74        use $crate::utils::{rumtk_web_render_html, types::HTMLResult, RUMWebRedirect};
75
76        let closure = || -> HTMLResult { rumtk_web_render_html($page, RUMWebRedirect::None) };
77
78        closure()
79    }};
80    ( $page:expr, $redirect_url:expr ) => {{
81        use $crate::utils::{rumtk_web_render_html, types::HTMLResult};
82
83        let closure = || -> HTMLResult { rumtk_web_render_html($page, $redirect_url) };
84
85        closure()
86    }};
87}
88
89#[macro_export]
90macro_rules! rumtk_web_render_redirect {
91    ( $url:expr ) => {{
92        use $crate::utils::{rumtk_web_redirect, types::HTMLResult, RUMWebRedirect};
93
94        let closure = || -> HTMLResult { rumtk_web_redirect($url) };
95
96        closure()
97    }};
98}
99
100///
101///
102/// 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.
103///
104#[macro_export]
105macro_rules! rumtk_web_render_markdown {
106    ( $md:expr ) => {{
107        use pulldown_cmark::{Options, Parser};
108        use rumtk_core::strings::RUMStringConversions;
109
110        let mut options = Options::empty();
111        options.insert(Options::ENABLE_STRIKETHROUGH);
112        options.insert(Options::ENABLE_TASKLISTS);
113        options.insert(Options::ENABLE_MATH);
114        options.insert(Options::ENABLE_TABLES);
115        options.insert(Options::ENABLE_WIKILINKS);
116
117        let input = String::from($md);
118        let parser = Parser::new_ext(&input, options);
119        let mut html_output = String::new();
120        pulldown_cmark::html::push_html(&mut html_output, parser);
121        println!("{}", &html_output);
122
123        html_output.to_rumstring()
124    }};
125}