rumtk_web/components/form/captchas/
mod.rs1mod 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}