Struct rocket::form::ValueField

source ·
pub struct ValueField<'r> {
    pub name: NameView<'r>,
    pub value: &'r str,
}
Expand description

A form field with a string value.

Rocket preprocesses all form fields into either ValueFields or DataFields. All fields from url-encoded forms, and fields without Content-Types from multipart forms, are preprocessed as a ValueField.

Fields§

§name: NameView<'r>

The (decoded) name of the form field.

§value: &'r str

The (decoded) value of the form field.

Implementations§

source§

impl<'v> ValueField<'v>

source

pub fn parse(field: &'v str) -> Self

Parse a field string, where both the key and value are assumed to be URL-decoded while preserving the = delimiter, into a ValueField.

This implements 3.2, 3.3 of section 5.1 of the WHATWG living standard.

Example
use rocket::form::ValueField;

let parsed = ValueField::parse("a cat=an A+ pet");
assert_eq!(parsed.name, "a cat");
assert_eq!(parsed.value, "an A+ pet");

let parsed = ValueField::parse("a cat is an A+ pet");
assert_eq!(parsed.name, "a cat is an A+ pet");
assert_eq!(parsed.value, "");

let parsed = ValueField::parse("cat.food=yum?");
assert_eq!(parsed.name, "cat");
assert_eq!(parsed.name.source(), "cat.food");
assert_eq!(parsed.value, "yum?");
source

pub fn from_value(value: &'v str) -> Self

Create a ValueField from a value, which is assumed to be URL-decoded. The field name will be empty.

This is equivalent to ValueField::from(("", value)). To create a ValueField from both a name and a value, use ValueField::from((name, value)).

Example
use rocket::form::ValueField;

let parsed = ValueField::from_value("A+=kitten");
assert_eq!(parsed.name, "");
assert_eq!(parsed.value, "A+=kitten");
source

pub fn shift(self) -> Self

Shift the name of self and return self with the shifted name.

See NameView::shift() for the details on name “shifting”.

Example
use rocket::form::ValueField;

let parsed = ValueField::parse("cat.food=yum?");
assert_eq!(parsed.name, "cat");
assert_eq!(parsed.name.source(), "cat.food");
assert_eq!(parsed.name.key_lossy(), "cat");

let shifted = parsed.shift();
assert_eq!(shifted.name, "cat.food");
assert_eq!(shifted.name.key_lossy(), "food");
source

pub fn unexpected(&self) -> Error<'v>

Creates a complete unexpected value field Error from self.

The error will have the following properties:

Example
use rocket::form::ValueField;
use rocket::form::error::{ErrorKind, Entity};

let field = ValueField::parse("cat.food=yum?");
let error = field.unexpected();

assert_eq!(error.name.as_ref().unwrap(), "cat.food");
assert_eq!(error.value.as_ref().unwrap(), "yum?");
assert_eq!(error.kind, ErrorKind::Unexpected);
assert_eq!(error.entity, Entity::ValueField);
source

pub fn missing(&self) -> Error<'v>

Creates a complete missing value field Error from self.

The error will have the following properties:

Example
use rocket::form::ValueField;
use rocket::form::error::{ErrorKind, Entity};

let field = ValueField::parse("cat.food=yum?");
let error = field.missing();

assert_eq!(error.name.as_ref().unwrap(), "cat.food");
assert_eq!(error.value.as_ref().unwrap(), "yum?");
assert_eq!(error.kind, ErrorKind::Missing);
assert_eq!(error.entity, Entity::ValueField);

Trait Implementations§

source§

impl<'r> Clone for ValueField<'r>

source§

fn clone(&self) -> ValueField<'r>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'r> Debug for ValueField<'r>

source§

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

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

impl<'a> From<(&'a str, &'a str)> for ValueField<'a>

source§

fn from((name, value): (&'a str, &'a str)) -> Self

Converts to this type from the input type.
source§

impl<'a, 'b> PartialEq<ValueField<'b>> for ValueField<'a>

source§

fn eq(&self, other: &ValueField<'b>) -> 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.

Auto Trait Implementations§

§

impl<'r> RefUnwindSafe for ValueField<'r>

§

impl<'r> Send for ValueField<'r>

§

impl<'r> Sync for ValueField<'r>

§

impl<'r> Unpin for ValueField<'r>

§

impl<'r> UnwindSafe for ValueField<'r>

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<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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