[][src]Struct kserd::nav::Navigator

pub struct Navigator<'a, 'k> { /* fields omitted */ }

A structure to efficiently navigate through a Kserd.

When trying to interrogate a Kserd it can become cumbersome to loop through nested children. The Navigator provides a structure around a root Kserd which then allows for efficient looping through the structure.

When a Navigator is constructed it flattens the tree structure into a depth-first vector, wrapping each Kserd into a Node which contains navigational information such are parent or children.

Methods

impl<'a, 'k> Navigator<'a, 'k>[src]

pub fn new(root: &'a Kserd<'k>) -> Self[src]

Construct a new Navigator.

Navigator borrows a Kserd as a root. It flattens the structure into a vector using a depth-first approach, and creates navigational links between Kserd nodes like parent and children.

pub fn root<'nav: 'node, 'node>(&'nav self) -> Node<'a, 'k, 'nav, 'node>[src]

The root node, that same as the Kserd which was seeded as the root.

Same as index 0.

Example

use kserd::nav::*;

let kserd = Kserd::new_num(3.14);
let nav = Navigator::new(&kserd);

assert_eq!(nav.root().index(), 0);
assert_eq!(nav.root().kserd(), &kserd);

pub fn get<'nav: 'node, 'node>(
    &'nav self,
    index: usize
) -> Option<Node<'a, 'k, 'nav, 'node>>
[src]

Get a node at an index.

The Navigator flattens the tree structure into a vector using a depth-first approach. Use the index to quickly obtain a node.

Example

use kserd::nav::*;

let kserd = Kserd::new_map(vec![
    (Kserd::new_num(1), Kserd::new_num(2)),
    (Kserd::new_num(3), Kserd::new_num(4)),
]);

let nav = Navigator::new(&kserd);

let get = |idx| nav.get(idx).map(|x| x.kserd());

assert_eq!(get(1), Some(&Kserd::new_num(1)));
assert_eq!(get(2), Some(&Kserd::new_num(2)));
assert_eq!(get(3), Some(&Kserd::new_num(3)));
assert_eq!(get(4), Some(&Kserd::new_num(4)));

pub fn len(&self) -> usize[src]

The number of nodes.

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

The navigator has no nodes.

Important traits for NodeIter<'a, 'k, 'nav>
pub fn nodes<'nav>(&'nav self) -> NodeIter<'a, 'k, 'nav>[src]

Iterate over the nodes in depth-first order.

Example

use kserd::nav::*;

let kserd = Kserd::new_map(vec![
    (Kserd::new_num(1), Kserd::new_num(2)),
    (Kserd::new_num(3), Kserd::new_num(4)),
]);

let nav = Navigator::new(&kserd);

let mut nodes = nav.nodes();

let mut next = || nodes.next().map(|x| x.kserd());

next(); // skip the root (hard to equality check)

assert_eq!(next(), Some(&Kserd::new_num(1)));
assert_eq!(next(), Some(&Kserd::new_num(2)));
assert_eq!(next(), Some(&Kserd::new_num(3)));
assert_eq!(next(), Some(&Kserd::new_num(4)));

Trait Implementations

impl<'a, 'k> AsRef<Navigator<'a, 'k>> for Formatter<'a, 'k>[src]

Auto Trait Implementations

impl<'a, 'k> RefUnwindSafe for Navigator<'a, 'k>

impl<'a, 'k> Send for Navigator<'a, 'k>

impl<'a, 'k> Sync for Navigator<'a, 'k>

impl<'a, 'k> Unpin for Navigator<'a, 'k> where
    'k: 'a, 

impl<'a, 'k> UnwindSafe for Navigator<'a, 'k>

Blanket Implementations

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

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, 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.