pub struct HcaptchaCaptcha { /* private fields */ }
Expand description

Capture the Hcaptcha data coming from the client.

Implementations§

source§

impl HcaptchaCaptcha

source

pub fn new(response: &str) -> Result<Self, HcaptchaError>

Create a new HcaptchaCaptcha from a response string slice.

§Input

response - The response token from the client

§Output

The HcaptchaCaptcha is returned if the input is valid. A HcaptchaError is returned if the validation fails.

§Example

Create HcaptchaCaptcha from response key.

    use hcaptcha::HcaptchaCaptcha;
    // Get the body JSON string from the event.
    let body_str = e.body.unwrap_or_else(|| "".to_owned());
    // Get the form data from the body string.
    let form: Form = serde_json::from_str(&body_str)?;

    let captcha = HcaptchaCaptcha::new(&form.response)?;
§Logging

If the tracing feature is enabled a debug level span is set for the method.

source

pub fn set_remoteip(&mut self, remoteip: &str) -> Result<Self, HcaptchaError>

Update the remoteip field in HcaptchaCaptcha.

§Input

remoteip - The response token from the client

§Output

If the remoteip string is empty the field is set to None. If the remoteip string is a valid v4 or v6 ip address the field is set to Some(remoteip). If the remoteip string is invalid a HcaptchaError is returned.

§Example
    use hcaptcha::HcaptchaCaptcha;
    let remoteip = get_remoteip_address();

    let captcha = HcaptchaCaptcha::new(&form.response)?
                    .set_remoteip(&remoteip)?;

    assert_some!(captcha.remoteip());
    if let Some(sk) = captcha.remoteip() {
            assert_eq!(remoteip, sk.to_string());
    };
§Logging

If the tracing feature is enabled a debug level span is set for the method.

source

pub fn set_sitekey(&mut self, sitekey: &str) -> Result<Self, HcaptchaError>

Update the remoteip field in HcaptchaCaptcha.

§Input

sitekey - The response token from the client

§Output

If the sitekey string is empty the field is set to None. If the sitekey string is a valid uuid the field is set to Some(sitekey). If the sitekey string is invalid a HcaptchaError is returned.

§Example
    use hcaptcha::HcaptchaCaptcha;
    // Get the body JSON string from the event.
    let body_str = e.body.unwrap_or_else(|| "".to_owned());
    // Get the form data from the body string.
    let form: Form = serde_json::from_str(&body_str)?;
    let sitekey = get_sitekey();

    let captcha = HcaptchaCaptcha::new(&form.response)?
                    .set_sitekey(&sitekey)?;

    assert_some!(captcha.sitekey());
    if let Some(sk) = captcha.sitekey() {
            assert_eq!(sitekey, sk.to_string());
    };
§Logging

If the tracing feature is enabled a debug level span is set for the method.

source

pub fn response(self) -> HcaptchaClientResponse

Return the value of the response field.

§Output

A string containing the value of the response field.

§Example
    use hcaptcha::HcaptchaCaptcha;
    let (response, captcha) = get_captcha();

    assert_eq!(response, captcha.response().to_string());
§Logging

If the tracing feature is enabled a debug level span is set for the method.

source

pub fn remoteip(&self) -> Option<HcaptchaRemoteip>

Get the value of the remoteip field.

§Output

An Option enum containing the value of the remoteip in the Some variant or a None variant if the value is not set.

§Example
    use hcaptcha::HcaptchaCaptcha;
    let (remoteip, captcha) = get_captcha();
     
    let value = captcha.remoteip();
    assert_some!(&value);

    if let Some(v) = value {
        assert_eq!(remoteip, v.to_string());
    }
§Logging

If the tracing feature is enabled a debug level span is set for the method.

source

pub fn sitekey(&self) -> Option<HcaptchaSitekey>

Get the value of the sitekey field.

§Output

An Option enum containing the value of the sitekey in the Some variant or a None variant if the value is not set.

§Example
    use hcaptcha::HcaptchaCaptcha;
    let (sitekey, captcha) = get_captcha();

    let value = captcha.sitekey();
    assert_some!(&value);

    if let Some(v) = value {
        assert_eq!(sitekey, v.to_string());
    };
§Logging

If the tracing feature is enabled a debug level span is set for the method.

Trait Implementations§

source§

impl Clone for HcaptchaCaptcha

source§

fn clone(&self) -> HcaptchaCaptcha

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for HcaptchaCaptcha

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for HcaptchaCaptcha

source§

fn default() -> HcaptchaCaptcha

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for HcaptchaCaptcha

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for HcaptchaCaptcha

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,