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§
source§impl<T> Lenient<T>
impl<T> Lenient<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
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§
source§impl<'v, T: FromForm<'v>> FromForm<'v> for Lenient<T>
impl<'v, T: FromForm<'v>> FromForm<'v> for Lenient<T>
source§fn push_value(ctxt: &mut Self::Context, field: ValueField<'v>)
fn push_value(ctxt: &mut Self::Context, field: ValueField<'v>)
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,
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,
field
.source§fn finalize(this: Self::Context) -> Result<'v, Self>
fn finalize(this: Self::Context) -> Result<'v, Self>
Errors
otherwise.source§impl<'f, A, T: FromUriParam<Query, A> + FromForm<'f>> FromUriParam<Query, A> for Lenient<T>
impl<'f, A, T: FromUriParam<Query, A> + FromForm<'f>> FromUriParam<Query, A> for Lenient<T>
§type Target = <T as FromUriParam<Query, A>>::Target
type Target = <T as FromUriParam<Query, A>>::Target
source§fn from_uri_param(param: A) -> Self::Target
fn from_uri_param(param: A) -> Self::Target
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 Lenient<T>
impl<T: Ord> Ord for Lenient<T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq> PartialEq<Lenient<T>> for Lenient<T>
impl<T: PartialEq> PartialEq<Lenient<T>> for Lenient<T>
source§impl<T: PartialOrd> PartialOrd<Lenient<T>> for Lenient<T>
impl<T: PartialOrd> PartialOrd<Lenient<T>> for Lenient<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl<T: Eq> Eq for Lenient<T>
impl<T> StructuralEq for Lenient<T>
impl<T> StructuralPartialEq for Lenient<T>
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<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> FromFd for Twhere
T: From<OwnedFd>,
impl<T> FromFd for Twhere T: From<OwnedFd>,
§impl<T> FromFilelike for Twhere
T: From<OwnedFd>,
impl<T> FromFilelike for Twhere T: From<OwnedFd>,
§fn from_filelike(owned: OwnedFd) -> T
fn from_filelike(owned: OwnedFd) -> T
Self
from the given filelike object. Read more§fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
fn from_into_filelike<Owned>(owned: Owned) -> Twhere Owned: IntoFilelike,
Self
from the given filelike object
converted from into_owned
. Read more§impl<T> FromSocketlike for Twhere
T: From<OwnedFd>,
impl<T> FromSocketlike for Twhere T: From<OwnedFd>,
§fn from_socketlike(owned: OwnedFd) -> T
fn from_socketlike(owned: OwnedFd) -> T
Self
from the given socketlike object.§fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
fn from_into_socketlike<Owned>(owned: Owned) -> Twhere Owned: IntoSocketlike,
Self
from the given socketlike object
converted from into_owned
.