rocket::request

Struct LenientForm

Source
pub struct LenientForm<T>(pub T);
Expand description

A data guard for parsing FromForm types leniently.

This type implements the FromData trait, and like Form, provides a generic means to parse arbitrary structures from incoming form data. Unlike Form, this type uses a lenient parsing strategy: forms that contains a superset of the expected fields (i.e, extra fields) will parse successfully.

§Leniency

A LenientForm<T> will parse successfully from an incoming form if the form contains a superset of the fields in T. Said another way, a LenientForm<T> automatically discards extra fields without error. For instance, if an incoming form contains the fields “a”, “b”, and “c” while T only contains “a” and “c”, the form will parse as LenientForm<T>.

§Usage

The usage of a LenientForm type is equivalent to that of Form, so we defer details to its documentation.

LenientForm implements FromData, so it can be used directly as a target of the data = "<param>" route parameter. For instance, if some structure of type T implements the FromForm trait, an incoming form can be automatically parsed into the T structure with the following route and handler:

use rocket::request::LenientForm;

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

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

§Incoming Data Limits

A LenientForm obeys the same data limits as a Form and defaults to 32KiB. The limit can be increased by setting the limits.forms configuration parameter. For instance, to increase the forms limit to 512KiB for all environments, you may add the following to your Rocket.toml:

[global.limits]
forms = 524288

Tuple Fields§

§0: T

Implementations§

Source§

impl<T> LenientForm<T>

Source

pub fn into_inner(self) -> T

Consumes self and returns the parsed value.

§Example
use rocket::request::LenientForm;

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

#[post("/submit", data = "<form>")]
fn submit(form: LenientForm<MyForm>) -> String {
    form.into_inner().field
}

Trait Implementations§

Source§

impl<T: Debug> Debug for LenientForm<T>

Source§

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

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

impl<T> Deref for LenientForm<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<'f, T: FromForm<'f>> FromData<'f> for LenientForm<T>

Source§

type Error = FormDataError<'f, <T as FromForm<'f>>::Error>

The associated error to be returned when the guard fails.
Source§

type Owned = String

The owned type returned from FromData::transform(). Read more
Source§

type Borrowed = str

The borrowed type consumed by FromData::from_data() when FromData::transform() returns a Transform::Borrowed. Read more
Source§

fn transform( r: &Request<'_>, d: Data, ) -> Transform<Outcome<Self::Owned, Self::Error>>

Transforms data into a value of type Self::Owned. Read more
Source§

fn from_data( _: &Request<'_>, o: Transformed<'f, Self>, ) -> Outcome<Self, Self::Error>

Validates, parses, and converts the incoming request body data into an instance of Self. Read more
Source§

impl<'q, T: FromForm<'q>> FromQuery<'q> for LenientForm<T>

Source§

type Error = <T as FromForm<'q>>::Error

The associated error to be returned if parsing/validation fails.
Source§

fn from_query(q: Query<'q>) -> Result<Self, Self::Error>

Parses and validates an instance of Self from a query or returns an Error if parsing or validation fails.
Source§

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

Source§

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.

Auto Trait Implementations§

§

impl<T> Freeze for LenientForm<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for LenientForm<T>
where T: RefUnwindSafe,

§

impl<T> Send for LenientForm<T>
where T: Send,

§

impl<T> Sync for LenientForm<T>
where T: Sync,

§

impl<T> Unpin for LenientForm<T>
where T: Unpin,

§

impl<T> UnwindSafe for LenientForm<T>
where T: UnwindSafe,

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, I> AsResult<T, I> for T
where I: Input,

Source§

fn as_result(self) -> Result<T, ParseErr<I>>

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, 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.

§

impl<T> IntoCollection<T> for T

§

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

Converts self into a collection.
§

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

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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> Typeable for T
where T: Any,

Source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
Source§

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

Source§

fn vzip(self) -> V