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>
impl<T> Strict<T>
sourcepub fn into_inner(self) -> T
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<'v, T: FromForm<'v>> FromForm<'v> for Strict<T>
impl<'v, T: FromForm<'v>> FromForm<'v> for Strict<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 Strict<T>
impl<'f, A, T: FromUriParam<Query, A> + FromForm<'f>> FromUriParam<Query, A> for Strict<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 Strict<T>
impl<T: Ord> Ord for Strict<T>
source§impl<T: PartialEq> PartialEq<Strict<T>> for Strict<T>
impl<T: PartialEq> PartialEq<Strict<T>> for Strict<T>
source§impl<T: PartialOrd> PartialOrd<Strict<T>> for Strict<T>
impl<T: PartialOrd> PartialOrd<Strict<T>> for Strict<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 Strict<T>
impl<T> StructuralEq for Strict<T>
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§
§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
.