[][src]Enum rocket::request::FormItems

pub enum FormItems<'f> {
    // some variants omitted
}

Iterator over the key/value pairs of a given HTTP form string.

use rocket::request::{FormItems, FromFormValue};

// Using the `key_value_decoded` method of `FormItem`.
let form_string = "greeting=Hello%2C+Mark%21&username=jake%2Fother";
for (key, value) in FormItems::from(form_string).map(|i| i.key_value_decoded()) {
    match &*key {
        "greeting" => assert_eq!(value, "Hello, Mark!".to_string()),
        "username" => assert_eq!(value, "jake/other".to_string()),
        _ => unreachable!()
    }
}

// Accessing the fields of `FormItem` directly, including `raw`.
for item in FormItems::from(form_string) {
    match item.key.as_str() {
        "greeting" => {
            assert_eq!(item.raw, "greeting=Hello%2C+Mark%21");
            assert_eq!(item.value, "Hello%2C+Mark%21");
            assert_eq!(item.value.url_decode(), Ok("Hello, Mark!".into()));
        }
        "username" => {
            assert_eq!(item.raw, "username=jake%2Fother");
            assert_eq!(item.value, "jake%2Fother");
            assert_eq!(item.value.url_decode(), Ok("jake/other".into()));
        }
        _ => unreachable!()
    }
}

Form Items via. FormItem

This iterator returns values of the type FormItem. To access the associated key/value pairs of the form item, either directly access them via the key and value fields, use the FormItem::key_value() method to get a tuple of the raw (key, value), or use the key_value_decoded() method to get a tuple of the decoded (key, value).

Completion

The iterator keeps track of whether the form string was parsed to completion to determine if the form string was malformed. The iterator can be queried for completion via the completed() method, which returns true if the iterator parsed the entire string that was passed to it. The iterator can also attempt to parse any remaining contents via exhaust(); this method returns true if exhaustion succeeded.

This iterator guarantees that all valid form strings are parsed to completion. The iterator attempts to be lenient. In particular, it allows the following oddball behavior:

Additionally, the iterator skips items with both an empty key and an empty value: at least one of the two must be non-empty to be returned from this iterator.

Examples

FormItems can be used directly as an iterator:

use rocket::request::FormItems;

// prints "greeting = hello", "username = jake", and "done = "
let form_string = "greeting=hello&username=jake&done";
for (key, value) in FormItems::from(form_string).map(|item| item.key_value()) {
    println!("{} = {}", key, value);
}

This is the same example as above, but the iterator is used explicitly.

use rocket::request::FormItems;

let form_string = "greeting=hello&username=jake&done";
let mut items = FormItems::from(form_string);

let next = items.next().unwrap();
assert_eq!(next.key, "greeting");
assert_eq!(next.value, "hello");

let next = items.next().unwrap();
assert_eq!(next.key, "username");
assert_eq!(next.value, "jake");

let next = items.next().unwrap();
assert_eq!(next.key, "done");
assert_eq!(next.value, "");

assert_eq!(items.next(), None);
assert!(items.completed());

Implementations

impl<'f> FormItems<'f>[src]

pub fn completed(&self) -> bool[src]

Returns true if the form string was parsed to completion. Returns false otherwise. All valid form strings will parse to completion, while invalid form strings will not.

Example

A valid form string parses to completion:

use rocket::request::FormItems;

let mut items = FormItems::from("a=b&c=d");
let key_values: Vec<_> = items.by_ref().collect();

assert_eq!(key_values.len(), 2);
assert_eq!(items.completed(), true);

In invalid form string does not parse to completion:

use rocket::request::FormItems;

let mut items = FormItems::from("a=b&==d");
let key_values: Vec<_> = items.by_ref().collect();

assert_eq!(key_values.len(), 1);
assert_eq!(items.completed(), false);

pub fn exhaust(&mut self) -> bool[src]

Parses all remaining key/value pairs and returns true if parsing ran to completion. All valid form strings will parse to completion, while invalid form strings will not.

Example

A valid form string can be exhausted:

use rocket::request::FormItems;

let mut items = FormItems::from("a=b&c=d");

assert!(items.next().is_some());
assert_eq!(items.completed(), false);
assert_eq!(items.exhaust(), true);
assert_eq!(items.completed(), true);

An invalid form string cannot be exhausted:

use rocket::request::FormItems;

let mut items = FormItems::from("a=b&=d=");

assert!(items.next().is_some());
assert_eq!(items.completed(), false);
assert_eq!(items.exhaust(), false);
assert_eq!(items.completed(), false);
assert!(items.next().is_none());

Trait Implementations

impl<'f> Debug for FormItems<'f>[src]

impl<'f> From<&'f [FormItem<'f>]> for FormItems<'f>[src]

impl<'f> From<&'f RawStr> for FormItems<'f>[src]

impl<'f> From<&'f str> for FormItems<'f>[src]

impl<'f> Iterator for FormItems<'f>[src]

type Item = FormItem<'f>

The type of the elements being iterated over.

Auto Trait Implementations

impl<'f> RefUnwindSafe for FormItems<'f>

impl<'f> Send for FormItems<'f>

impl<'f> Sync for FormItems<'f>

impl<'f> Unpin for FormItems<'f>

impl<'f> UnwindSafe for FormItems<'f>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, I> AsResult<T, I> for T where
    I: Input, 

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoCollection<T> for T

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<I> IteratorRandom for I where
    I: Iterator
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Typeable for T where
    T: Any

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