Struct rocket::form::Lenient[][src]

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: forms that contains a superset of the expected fields (i.e, extra fields) will fail to parse and defaults will not be use for missing fields.

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 {
    lenient_inner_option: Option<Lenient<bool>>,
}

Implementations

impl<T> Lenient<T>[src]

pub fn into_inner(self) -> T[src]

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

impl<T: Debug> Debug for Lenient<T>[src]

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

Formats the value using the given formatter. Read more

impl<T> Deref for Lenient<T>[src]

type Target = T

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Dereferences the value.

impl<T> DerefMut for Lenient<T>[src]

fn deref_mut(&mut self) -> &mut Self::Target[src]

Mutably dereferences the value.

impl<T> From<T> for Lenient<T>[src]

fn from(val: T) -> Lenient<T>[src]

Performs the conversion.

impl<'v, T: FromForm<'v>> FromForm<'v> for Lenient<T>[src]

type Context = T::Context

The form guard’s parsing context.

fn init(opts: Options) -> Self::Context[src]

Initializes and returns the parsing context for Self.

fn push_value(ctxt: &mut Self::Context, field: ValueField<'v>)[src]

Processes the value field field.

fn push_data<'life0, 'life1, 'async_trait>(
    ctxt: &'life0 mut Self::Context,
    field: DataField<'v, 'life1>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 
[src]

Processes the data field field.

fn finalize(this: Self::Context) -> Result<'v, Self>[src]

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

fn push_error(_ctxt: &mut Self::Context, _error: Error<'r>)[src]

Processes the external form or field error _error. Read more

fn default(opts: Options) -> Option<Self>[src]

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

impl<'f, A, T: FromUriParam<Query, A> + FromForm<'f>> FromUriParam<Query, A> for Lenient<T>[src]

type Target = T::Target

The resulting type of this conversion.

fn from_uri_param(param: A) -> Self::Target[src]

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

impl<T: Hash> Hash for Lenient<T>[src]

fn hash<__H: Hasher>(&self, state: &mut __H)[src]

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

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

impl<T: Ord> Ord for Lenient<T>[src]

fn cmp(&self, other: &Lenient<T>) -> Ordering[src]

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

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl<T: PartialEq> PartialEq<Lenient<T>> for Lenient<T>[src]

fn eq(&self, other: &Lenient<T>) -> bool[src]

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

fn ne(&self, other: &Lenient<T>) -> bool[src]

This method tests for !=.

impl<T: PartialOrd> PartialOrd<Lenient<T>> for Lenient<T>[src]

fn partial_cmp(&self, other: &Lenient<T>) -> Option<Ordering>[src]

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

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

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

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

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

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

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

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

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

impl<T: Eq> Eq for Lenient<T>[src]

impl<T> StructuralEq for Lenient<T>[src]

impl<T> StructuralPartialEq for Lenient<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Lenient<T> where
    T: RefUnwindSafe

impl<T> Send for Lenient<T> where
    T: Send

impl<T> Sync for Lenient<T> where
    T: Sync

impl<T> Unpin for Lenient<T> where
    T: Unpin

impl<T> UnwindSafe for Lenient<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

pub fn equivalent(&self, key: &K) -> bool[src]

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

impl<T> From<!> for T[src]

pub fn from(t: !) -> T[src]

Performs the conversion.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

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

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

fn in_current_span(self) -> Instrumented<Self>[src]

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

pub fn into_collection<A>(self) -> SmallVec<A> where
    A: Array<Item = T>, 

Converts self into a collection.

pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
    F: FnMut(T) -> U,
    A: Array<Item = U>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V