logo
pub struct NameView<'v> { /* private fields */ }
Expand description

A sliding-prefix view into a Name.

A NameView maintains a sliding key view into a Name. The current key (key()) can be shift()ed one key to the right. The Name prefix including the current key can be extracted via as_name() and the prefix not including the current key via parent().

This is best illustrated via an example:

use rocket::form::name::NameView;

// The view begins at the first key. Illustrated: `(a).b[c:d]` where
// parenthesis enclose the current key.
let mut view = NameView::new("a.b[c:d]");
assert_eq!(view.key().unwrap(), "a");
assert_eq!(view.as_name(), "a");
assert_eq!(view.parent(), None);

// Shifted once to the right views the second key: `a.(b)[c:d]`.
view.shift();
assert_eq!(view.key().unwrap(), "b");
assert_eq!(view.as_name(), "a.b");
assert_eq!(view.parent().unwrap(), "a");

// Shifting again now has predictable results: `a.b[(c:d)]`.
view.shift();
assert_eq!(view.key().unwrap(), "c:d");
assert_eq!(view.as_name(), "a.b[c:d]");
assert_eq!(view.parent().unwrap(), "a.b");

// Shifting past the end means we have no further keys.
view.shift();
assert_eq!(view.key(), None);
assert_eq!(view.key_lossy(), "");
assert_eq!(view.as_name(), "a.b[c:d]");
assert_eq!(view.parent().unwrap(), "a.b[c:d]");

view.shift();
assert_eq!(view.key(), None);
assert_eq!(view.as_name(), "a.b[c:d]");
assert_eq!(view.parent().unwrap(), "a.b[c:d]");

Equality

PartialEq, Eq, and Hash all operate on the name prefix including the current key. Only key values are compared; delimiters are insignificant. Again, illustrated via examples:

use rocket::form::name::NameView;

let mut view = NameView::new("a.b[c:d]");
assert_eq!(view, "a");

// Shifted once to the right views the second key: `a.(b)[c:d]`.
view.shift();
assert_eq!(view.key().unwrap(), "b");
assert_eq!(view.as_name(), "a.b");
assert_eq!(view, "a.b");
assert_eq!(view, "a[b]");

// Shifting again now has predictable results: `a.b[(c:d)]`.
view.shift();
assert_eq!(view, "a.b[c:d]");
assert_eq!(view, "a.b.c:d");
assert_eq!(view, "a[b].c:d");
assert_eq!(view, "a[b]c:d");

Implementations

Initializes a new NameView at the first key of name.

Example
use rocket::form::name::NameView;

let mut view = NameView::new("a.b[c:d]");
assert_eq!(view.key().unwrap(), "a");
assert_eq!(view.as_name(), "a");
assert_eq!(view.parent(), None);

Shifts the current key once to the right.

Examples
use rocket::form::name::NameView;

let mut view = NameView::new("a.b[c:d][d.e]");
assert_eq!(view.key().unwrap(), "a");

view.shift();
assert_eq!(view.key().unwrap(), "b");

view.shift();
assert_eq!(view.key().unwrap(), "c:d");

view.shift();
assert_eq!(view.key().unwrap(), "d.e");

Malformed strings can have interesting results:

use rocket::form::name::NameView;

let mut view = NameView::new("a[c.d");
assert_eq!(view.key_lossy(), "a");

view.shift();
assert_eq!(view.key_lossy(), "c.d");

let mut view = NameView::new("a[c[.d]");
assert_eq!(view.key_lossy(), "a");

view.shift();
assert_eq!(view.key_lossy(), "c[.d");

view.shift();
assert_eq!(view.key(), None);

let mut view = NameView::new("foo[c[.d]]");
assert_eq!(view.key_lossy(), "foo");

view.shift();
assert_eq!(view.key_lossy(), "c[.d");

view.shift();
assert_eq!(view.key_lossy(), "]");

view.shift();
assert_eq!(view.key(), None);

Returns the key currently viewed by self if it is non-empty.

                food.bart[bar:foo].blam[0_0][][1000]=some-value
name            |----------------------------------|
non-empty key   |--| |--| |-----|  |--| |-|     |--|
empty key                                  |-|
Example
use rocket::form::name::NameView;

let mut view = NameView::new("a[b]");
assert_eq!(view.key().unwrap(), "a");

view.shift();
assert_eq!(view.key().unwrap(), "b");

view.shift();
assert_eq!(view.key(), None);

Returns the key currently viewed by self, even if it is non-empty.

                food.bart[bar:foo].blam[0_0][][1000]=some-value
name            |----------------------------------|
non-empty key   |--| |--| |-----|  |--| |-|     |--|
empty key                                  |-|
Example
use rocket::form::name::NameView;

let mut view = NameView::new("a[b]");
assert_eq!(view.key_lossy(), "a");

view.shift();
assert_eq!(view.key_lossy(), "b");

view.shift();
assert_eq!(view.key_lossy(), "");

Returns the Name up to and including the current key.

Example
use rocket::form::name::NameView;

let mut view = NameView::new("a[b]");
assert_eq!(view.as_name(), "a");

view.shift();
assert_eq!(view.as_name(), "a[b]");

Returns the Name prior to the current key.

Example
use rocket::form::name::NameView;

let mut view = NameView::new("a[b]");
assert_eq!(view.parent(), None);

view.shift();
assert_eq!(view.parent().unwrap(), "a");

view.shift();
assert_eq!(view.parent().unwrap(), "a[b]");

Returns the underlying Name.

Example
use rocket::form::name::NameView;

let mut view = NameView::new("a[b]");
assert_eq!(view.source(), "a[b]");

view.shift();
assert_eq!(view.source(), "a[b]");

view.shift();
assert_eq!(view.source(), "a[b]");

Trait Implementations

Immutably borrows from an owned value. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts self into a collection.

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more