mod gcaptcha;
use super::captchas::gcaptcha::gcaptcha;
use crate::components::form::form_node::{FormNode, ToFormNode};
use crate::utils::types::SharedAppState;
use crate::{rumtk_web_get_config, ComponentResult, RUMWebTemplate, RUMWebTemplateSafe, DEFAULT_TEXTMAP};
use rumtk_core::strings::{rumtk_format, RUMString};
use std::string::ToString;
const DEFAULT_CAPTCHA: &str = "gcaptcha";
#[derive(RUMWebTemplate, Debug, Clone)]
#[template(
source = "
{{captcha|safe}}
",
ext = "html"
)]
pub struct Captcha {
captcha: RUMString
}
impl RUMWebTemplateSafe for Captcha {}
impl ToFormNode<Captcha> for Captcha {}
pub fn captcha<'a>(widget_id: &str, state: SharedAppState) -> ComponentResult<FormNode> {
let captcha_config = rumtk_web_get_config!(state).captcha.clone().unwrap_or_else(|| DEFAULT_TEXTMAP.clone());
let captcha_type = match captcha_config.get("captcha-type") {
Some(typ) => &typ,
None => DEFAULT_CAPTCHA
};
match captcha_type {
"gcaptcha" => {
let rendered_captcha = gcaptcha(widget_id, &captcha_config, state.clone())?;
Ok(Captcha {
captcha: rendered_captcha.to_string()
}.to_form_node())
}
_ => Err(rumtk_format!("Captcha <{captcha_type}> not implemented!")),
}
}