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

pub fn find_item<'a, T>(item: &'a ItemKind, path: &[T]) -> Option<&'a ItemKind> 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::*;

// 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"]), Some(&ItemKind::UInt(23)));
assert_eq!(find_item(&snapshot, &["a", "x"]), Some(&ItemKind::UInt(23)));
assert_eq!(
    find_item(&snapshot, &["", "a", "x"]),
    Some(&ItemKind::UInt(23))
);

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

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

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

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

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