Expand description
Derive macro for captval::Captval
Derives the Captval trait requirements for a struct such as in the example below of a contact form providing the captval response token, the client ip address of the client and the captval service sitekey.
use captval::Captval;
#[derive(Captval)]
pub struct ContactForm {
name: String,
phone: String,
email: String,
message: String,
#[captcha]
captval: String,
#[remoteip]
ip: String,
#[sitekey]
key: String,
}
The derive macro provides code such as the following:
impl Captval for ContactForm {
fn valid_response(
&self,
secret: &str,
uri: Option<String>,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = Result<captval::Response,
captval::Error>,
>,
>,
> {
let mut client = captval::Client::new();
if let Some(u) = uri {
match client.set_url(&u) {
Ok(c) => client = c,
Err(e) => {
return Box::pin(async { Err(e) });
}
};
};
#[allow(unused_mut)]
let mut captcha;
match captval::Captcha::new(&self.captval) {
Ok(c) => captcha = c,
Err(e) => {
return Box::pin(async { Err(e) });
}
};
match captcha.set_remoteip(&self.ip) {
Ok(c) => captcha = c,
Err(e) => {
return Box::pin(async { Err(e) });
}
};
match captcha.set_sitekey(&self.key) {
Ok(c) => captcha = c,
Err(e) => {
return Box::pin(async { Err(e) });
}
};
let request;
match captval::Request::new(&secret, captcha) {
Ok(r) => request = r,
Err(e) => {
return Box::pin(async { Err(e) });
}
};
Box::pin(client.verify(request))
}
}
Derive Macros§
- Captval
- Derive the Captval trait for a struct.