Struct rocket::form::Strict

source ·
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§

source§

impl<T> Strict<T>

source

pub fn into_inner(self) -> T

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§

source§

impl<T: Debug> Debug for Strict<T>

source§

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

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

impl<T> Deref for Strict<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for Strict<T>

source§

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

Mutably dereferences the value.
source§

impl<T> From<T> for Strict<T>

source§

fn from(val: T) -> Strict<T>

Converts to this type from the input type.
source§

impl<'v, T: FromForm<'v>> FromForm<'v> for Strict<T>

§

type Context = <T as FromForm<'v>>::Context

The form guard’s parsing context.
source§

fn init(opts: Options) -> Self::Context

Initializes and returns the parsing context for Self.
source§

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

Processes the value field field.
source§

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 Self: 'async_trait, 'v: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Processes the data field field.
source§

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

Finalizes parsing. Returns the parsed value when successful or collection of Errors otherwise.
source§

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

Processes the external form or field error _error. Read more
source§

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

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

impl<'f, A, T: FromUriParam<Query, A> + FromForm<'f>> FromUriParam<Query, A> for Strict<T>

§

type Target = <T as FromUriParam<Query, A>>::Target

The resulting type of this conversion.
source§

fn from_uri_param(param: A) -> Self::Target

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.
source§

impl<T: Ord> Ord for Strict<T>

source§

fn cmp(&self, other: &Strict<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq> PartialEq<Strict<T>> for Strict<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd<Strict<T>> for Strict<T>

source§

fn partial_cmp(&self, other: &Strict<T>) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl<T: Eq> Eq for Strict<T>

source§

impl<T> StructuralEq for Strict<T>

source§

impl<T> StructuralPartialEq for Strict<T>

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§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>

§

impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self>

source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

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

impl<T> From<!> for T

const: unstable · source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromFd for Twhere T: From<OwnedFd>,

§

fn from_fd(owned_fd: OwnedFd) -> T

👎Deprecated since 1.0.0: FromFd::from_fd is replaced by From<OwnedFd>::from
Constructs a new instance of Self from the given file descriptor. Read more
§

fn from_into_fd<Owned>(into_owned: Owned) -> Selfwhere Owned: Into<OwnedFd>, Self: Sized + From<OwnedFd>,

Constructs a new instance of Self from the given file descriptor converted from into_owned. Read more
§

impl<T> FromFilelike for Twhere T: From<OwnedFd>,

§

fn from_filelike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given filelike object. Read more
§

fn from_into_filelike<Owned>(owned: Owned) -> Twhere Owned: IntoFilelike,

Constructs a new instance of Self from the given filelike object converted from into_owned. Read more
§

impl<T> FromSocketlike for Twhere T: From<OwnedFd>,

§

fn from_socketlike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given socketlike object.
§

fn from_into_socketlike<Owned>(owned: Owned) -> Twhere Owned: IntoSocketlike,

Constructs a new instance of Self from the given socketlike object converted from into_owned.
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 Twhere U: From<T>,

const: unstable · 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> IntoCollection<T> for T

source§

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

Converts self into a collection.
source§

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

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V

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> Formattable for Twhere T: Deref, <T as Deref>::Target: Formattable,

source§

impl<T> Parsable for Twhere T: Deref, <T as Deref>::Target: Parsable,