Struct rocket::form::Strict[][src]

pub struct Strict<T>(_);
Expand description

A form guard for parsing form types strictly.

This type implements the FromForm trait and thus can be used as a generic parameter to the Form data guard: Form<Strict<T>>, where T implements FromForm. Unlike using Form directly, this type uses a strict 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.

Strictness

A Strict<T> will parse successfully from an incoming form only if the form contains the exact set of fields in T. Said another way, a Strict<T> will error on missing and/or extra fields. For instance, if an incoming form contains the fields “a”, “b”, and “c” while T only contains “a” and “c”, the form will not parse as Strict<T>.

Usage

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

use rocket::form::{Form, Strict};

#[derive(FromForm)]
struct UserInput {
    value: String
}

#[post("/submit", data = "<user_input>")]
fn submit_task(user_input: Form<Strict<UserInput>>) -> String {
    format!("Your value: {}", user_input.value)
}

Strict can also be used to make individual fields strict while keeping the overall structure and remaining fields lenient:

use rocket::form::{Form, Strict};

#[derive(FromForm)]
struct UserInput {
    required: Strict<bool>,
    uses_default: bool
}

Implementations

impl<T> Strict<T>[src]

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

Consumes self and returns the inner value.

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

Example

use rocket::form::{Form, Strict};

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

#[post("/submit", data = "<form>")]
fn submit(form: Form<Strict<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 Strict<T>[src]

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

Formats the value using the given formatter. Read more

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

type Target = T

The resulting type after dereferencing.

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

Dereferences the value.

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

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

Mutably dereferences the value.

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

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

Performs the conversion.

impl<'v, T: FromForm<'v>> FromForm<'v> for Strict<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 Strict<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: Ord> Ord for Strict<T>[src]

fn cmp(&self, other: &Strict<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<Strict<T>> for Strict<T>[src]

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

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

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

This method tests for !=.

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

fn partial_cmp(&self, other: &Strict<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 Strict<T>[src]

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

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

Auto Trait Implementations

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

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

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

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

impl<T> UnwindSafe for Strict<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