Struct rocket::request::FormItems [] [src]

pub struct FormItems<'f> { /* fields omitted */ }

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

Note: The returned key/value pairs are not URL decoded. To URL decode the raw strings, use String::from_form_value:

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

let form_string = "greeting=Hello%2C+Mark%21&username=jake%2Fother";
for (key, value) in FormItems::from(form_string) {
    let decoded_value = String::from_form_value(value);
    match key {
        "greeting" => assert_eq!(decoded_value, Ok("Hello, Mark!".into())),
        "username" => assert_eq!(decoded_value, Ok("jake/other".into())),
        _ => unreachable!()
    }
}

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 exhausted; 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:

  • A single trailing & character is allowed.
  • Empty values are allowed.

Examples

FormItems can be used directly as an iterator:

use rocket::request::FormItems;

// prints "greeting = hello" then "username = jake"
let form_string = "greeting=hello&username=jake";
for (key, value) in FormItems::from(form_string) {
    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";
let mut items = FormItems::from(form_string);
assert_eq!(items.next(), Some(("greeting", "hello")));
assert_eq!(items.next(), Some(("username", "jake")));
assert_eq!(items.next(), None);
assert!(items.completed());

Methods

impl<'f> FormItems<'f>
[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);

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.exhausted(), 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.exhausted(), false);
assert_eq!(items.completed(), false);

Retrieves the original string being parsed by this iterator. The string returned by this method does not change, regardless of the status of the iterator.

Example

use rocket::request::FormItems;

let form_string = "a=b&c=d";
let mut items = FormItems::from(form_string);
assert_eq!(items.inner_str(), form_string);

assert!(items.next().is_some());
assert_eq!(items.inner_str(), form_string);

assert!(items.next().is_some());
assert_eq!(items.inner_str(), form_string);

assert!(items.next().is_none());
assert_eq!(items.inner_str(), form_string);

Trait Implementations

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

Returns an iterator over the key/value pairs in the x-www-form-urlencoded form string.

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

The type of the elements being iterated over.

Advances the iterator and returns the next value. Read more

Returns the bounds on the remaining length of the iterator. Read more

Consumes the iterator, counting the number of iterations and returning it. Read more

Consumes the iterator, returning the last element. Read more

Returns the nth element of the iterator. Read more

Takes two iterators and creates a new iterator over both in sequence. Read more

'Zips up' two iterators into a single iterator of pairs. Read more

Takes a closure and creates an iterator which calls that closure on each element. Read more

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

Creates an iterator that both filters and maps. Read more

Creates an iterator which gives the current iteration count as well as the next value. Read more

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

Creates an iterator that [skip()]s elements based on a predicate. Read more

Creates an iterator that yields elements based on a predicate. Read more

Creates an iterator that skips the first n elements. Read more

Creates an iterator that yields its first n elements. Read more

An iterator adaptor similar to [fold()] that holds internal state and produces a new iterator. Read more

Creates an iterator that works like map, but flattens nested structure. Read more

Creates an iterator which ends after the first [None]. Read more

Do something with each element of an iterator, passing the value on. Read more

Borrows an iterator, rather than consuming it. Read more

Transforms an iterator into a collection. Read more

Consumes an iterator, creating two collections from it. Read more

An iterator adaptor that applies a function, producing a single, final value. Read more

Tests if every element of the iterator matches a predicate. Read more

Tests if any element of the iterator matches a predicate. Read more

Searches for an element of an iterator that satisfies a predicate. Read more

Searches for an element in an iterator, returning its index. Read more

Searches for an element in an iterator from the right, returning its index. Read more

Returns the maximum element of an iterator. Read more

Returns the minimum element of an iterator. Read more

Returns the element that gives the maximum value from the specified function. Read more

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

Returns the element that gives the minimum value from the specified function. Read more

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

Reverses an iterator's direction. Read more

Converts an iterator of pairs into a pair of containers. Read more

Creates an iterator which [clone()]s all of its elements. Read more

Repeats an iterator endlessly. Read more

Sums the elements of an iterator. Read more

Iterates over the entire iterator, multiplying all the elements Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Determines if the elements of this Iterator are equal to those of another. Read more

Determines if the elements of this Iterator are unequal to those of another. Read more

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more