Function metrix::snapshot::find_item[][src]

pub fn find_item<'a, T>(item: &'a ItemKind, path: &[T]) -> FindItem<'a> where
    T: AsRef<str>, 

Finds an item in a Snapshot

path are the segments of the path.

Since a Snapshot may contain multiple items with the same name only the first found will be returned.

If a prefix of a path leads to a value that value is returned and the rest of the path is discarded.

Empty segments of a path are ignored.

Example

use metrix::snapshot::*;
use metrix::snapshot::FindItem::*;

// a -> 23
// b -> c -> 42

let inner = ItemKind::Snapshot(Snapshot {
    items: vec![("c".to_string(), ItemKind::UInt(42))],
});

let snapshot = ItemKind::Snapshot(Snapshot {
    items: vec![
        ("a".to_string(), ItemKind::UInt(23)),
        ("b".to_string(), inner.clone()),
    ],
});

assert_eq!(find_item(&snapshot, &["a"]), Found(&ItemKind::UInt(23)));
assert_eq!(find_item(&snapshot, &["a", "x"]), Found(&ItemKind::UInt(23)));
assert_eq!(
    find_item(&snapshot, &["", "a", "x"]),
    Found(&ItemKind::UInt(23))
);

assert_eq!(find_item(&snapshot, &["b"]), Found(&inner));

assert_eq!(find_item(&snapshot, &["b", "c"]), Found(&ItemKind::UInt(42)));
assert_eq!(
    find_item(&snapshot, &["", "b", "", "c"]),
    Found(&ItemKind::UInt(42))
);

assert_eq!(
    find_item(&snapshot, &["b", "c", "x"]),
    Found(&ItemKind::UInt(42))
);

assert_eq!(find_item::<String>(&snapshot, &[]), Found(&snapshot));

assert_eq!(find_item(&snapshot, &[""]), Found(&snapshot));