Skip to main content

systemprompt_oauth/services/
templating.rs

1use std::collections::HashMap;
2
3#[derive(Copy, Clone, Debug)]
4pub struct TemplateEngine;
5
6impl TemplateEngine {
7    pub fn render(template: &str, context: HashMap<&str, &str>) -> String {
8        let mut result = template.to_string();
9
10        for (key, value) in context {
11            let placeholder = format!("{{{key}}}");
12            let escaped_value = Self::html_escape(value);
13            result = result.replace(&placeholder, &escaped_value);
14        }
15
16        result
17    }
18
19    fn html_escape(input: &str) -> String {
20        input
21            .replace('&', "&amp;")
22            .replace('<', "&lt;")
23            .replace('>', "&gt;")
24            .replace('"', "&quot;")
25            .replace('\'', "&#x27;")
26    }
27
28    pub const fn load_authorize_template() -> &'static str {
29        include_str!("../../templates/authorize.html")
30    }
31
32    pub const fn load_webauthn_oauth_template() -> &'static str {
33        include_str!("../../templates/webauthn_oauth.html")
34    }
35
36    pub const fn load_link_passkey_template() -> &'static str {
37        include_str!("../../templates/link_passkey.html")
38    }
39}