Skip to main content

rumtk_web/components/form/captchas/
mod.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 */
20mod gcaptcha;
21
22use super::captchas::gcaptcha::gcaptcha;
23use crate::components::form::form_node::{FormNode, ToFormNode};
24use crate::utils::types::SharedAppState;
25use crate::{rumtk_web_get_config, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe, DEFAULT_TEXTMAP};
26use rumtk_core::strings::{rumtk_format, RUMString};
27use std::string::ToString;
28
29const DEFAULT_CAPTCHA: &str = "gcaptcha";
30
31#[derive(RUMWebTemplate, Debug, Clone)]
32#[template(
33    source = "
34        {{captcha|safe}}
35    ",
36    ext = "html"
37)]
38pub struct Captcha {
39    captcha: RUMString
40}
41
42impl RUMWebTemplateSafe for Captcha {}
43
44impl ToFormNode<Captcha> for Captcha {}
45
46pub fn captcha<'a>(widget_id: &str, state: SharedAppState) -> ComponentResult<FormNode> {
47    let captcha_config = rumtk_web_get_config!(state).captcha.clone().unwrap_or_else(|| DEFAULT_TEXTMAP.clone());
48    let captcha_type = match captcha_config.get("captcha-type") {
49        Some(typ) => &typ,
50        None => DEFAULT_CAPTCHA
51    };
52    match captcha_type {
53        "gcaptcha" => {
54            let rendered_captcha = gcaptcha(widget_id, &captcha_config, state.clone())?;
55            Ok(Captcha {
56                captcha: rendered_captcha.to_string()
57            }.to_form_node())
58        }
59        _ => Err(rumtk_format!("Captcha <{captcha_type}> not implemented!")),
60    }
61}