logo
pub struct Lenient<T>(_);
Expand description

A form guard for parsing form types leniently.

This type implements the FromForm trait and thus can be used as a generic parameter to the Form data guard: Form<Lenient<T>>, where T implements FromForm. Unlike using Form directly, this type uses a lenient parsing strategy.

Lenient Parsing

A Lenient<T> will parse successfully from an incoming form even if the form contains extra or missing fields. If fields are missing, the form field type’s default will be used, if there is one. Extra fields are ignored; only the first is parsed and validated. This is the default strategy for Form.

Usage

Lenient<T> implements FromForm as long as T implements FromForm. As such, Form<Lenient<T>> is a data guard.

Note that Form<T> already parses leniently, so a Form<Lenient<T>> is redundant and equal to Form<T>. Lenient, however, can be used to make otherwise strict parses lenient, for example, in Option<Lenient<T>>:

use rocket::form::Lenient;

#[derive(FromForm)]
struct UserInput {
    // Parses as `Some(false)` when `lenient_inner_option` isn't present.
    // Without `Lenient`, this would otherwise parse as `None`.
    lenient_inner_option: Option<Lenient<bool>>,
}

Implementations

Consumes self and returns the inner value.

Note that since Lenient implements Deref and DerefMut with target T, reading and writing an inner value can be accomplished transparently.

Example
use rocket::form::{Form, Lenient};

#[derive(FromForm)]
struct MyForm {
    field: String,
}

#[post("/submit", data = "<form>")]
fn submit(form: Form<Lenient<MyForm>>) -> String {
    // We can read or mutate a value transparently:
    let field: &str = &form.field;

    // To gain ownership, however, use `into_inner()`:
    form.into_inner().into_inner().field
}

Trait Implementations

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Converts to this type from the input type.

The form guard’s parsing context.

Initializes and returns the parsing context for Self.

Processes the value field field.

Processes the data field field.

Finalizes parsing. Returns the parsed value when successful or collection of Errors otherwise. Read more

Processes the external form or field error _error. Read more

Returns a default value, if any, to use when a value is desired and parsing fails. Read more

The resulting type of this conversion.

Converts a value of type T into a value of type Self::Target. The resulting value of type Self::Target will be rendered into a URI using its UriDisplay implementation. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Converts to this type from the input type.

Returns the argument unchanged.

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

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

Calls U::from(self).

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

Converts self into a collection.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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