[][src]Struct marked_yaml::types::MarkedMappingNode

pub struct MarkedMappingNode { /* fields omitted */ }

A marked YAML mapping node

Mapping nodes in YAML are defined as a key/value mapping where the keys are unique and always scalars, whereas values may be YAML nodes of any kind.

Because some users of this crate may need to care about insertion order we use linked_hash_map for this.

NOTE: Nodes are considered equal even if they don't come from the same place. i.e. their spans are ignored for equality and hashing

use marked_yaml::{parse_yaml, Marker, Span};
let node = parse_yaml(100, "{foo: bar}").unwrap();
let map = node.as_mapping().unwrap();
assert_eq!(map.span(), &Span::new_with_marks(Marker::new(100, 1, 1), Marker::new(100, 1, 10)));

Methods

impl MarkedMappingNode[src]

pub fn span(&self) -> &Span[src]

Retrieve the Span from this node.

let node = MarkedMappingNode::new_empty(Span::new_blank());
assert_eq!(node.span(), &Span::new_blank());

pub fn span_mut(&mut self) -> &mut Span[src]

Retrieve the Span from this node mutably.

let mut node = MarkedMappingNode::new_empty(Span::new_blank());
node.span_mut().set_start(Some(Marker::new(0, 1, 0)));
assert_eq!(node.span().start(), Some(&Marker::new(0, 1, 0)));

impl MarkedMappingNode[src]

pub fn new_empty(span: Span) -> Self[src]

Create a new empty mapping node

let node = MarkedMappingNode::new_empty(Span::new_blank());

pub fn new(span: Span, value: LinkedHashMap<MarkedScalarNode, Node>) -> Self[src]

Create a new mapping node from the given hash table

let node = MarkedMappingNode::new(Span::new_blank(), LinkedHashMap::new());

pub fn get_node(&self, index: &str) -> Option<&Node>[src]

Get the node for the given string key

If the index is not found then None is returned.

let node = parse_yaml(0, "{key: value}").unwrap();
let map = node.as_mapping().unwrap();
assert_eq!(map.get_node("key")
    .and_then(Node::as_scalar)
    .map(MarkedScalarNode::as_str)
    .unwrap(),
    "value");

pub fn get_scalar(&self, index: &str) -> Option<&MarkedScalarNode>[src]

Get the scalar for the given string key

If the key is not found, or the node for that key is not a scalar node, then None will be returned.

let node = parse_yaml(0, "{key: value}").unwrap();
let map = node.as_mapping().unwrap();
assert_eq!(map.get_scalar("key")
    .map(MarkedScalarNode::as_str)
    .unwrap(),
    "value");

pub fn get_sequence(&self, index: &str) -> Option<&MarkedSequenceNode>[src]

Get the sequence at the given index

If the key is not found, or the node for that key is not a sequence node, then None will be returned.

let node = parse_yaml(0, "{key: [value]}").unwrap();
let map = node.as_mapping().unwrap();
assert_eq!(map.get_sequence("key")
    .and_then(|s| s.get_scalar(0))
    .map(MarkedScalarNode::as_str)
    .unwrap(),
    "value");

pub fn get_mapping(&self, index: &str) -> Option<&MarkedMappingNode>[src]

Get the mapping at the given index

If the key is not found, or the node for that key is not a mapping node, then None will be returned.

let node = parse_yaml(0, "{key: {inner: value}}").unwrap();
let map = node.as_mapping().unwrap();
assert_eq!(map.get_mapping("key")
    .and_then(|m| m.get_scalar("inner"))
    .map(MarkedScalarNode::as_str)
    .unwrap(),
    "value");

Methods from Deref<Target = LinkedHashMap<MarkedScalarNode, Node>>

pub fn reserve(&mut self, additional: usize)[src]

Reserves capacity for at least additional more elements to be inserted into the map. The map may reserve more space to avoid frequent allocations.

Panics

Panics if the new allocation size overflows usize.

pub fn shrink_to_fit(&mut self)[src]

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

pub fn entry(&mut self, k: K) -> Entry<K, V, S>[src]

Gets the given key's corresponding entry in the map for in-place manipulation.

Examples

use linked_hash_map::LinkedHashMap;

let mut letters = LinkedHashMap::new();

for ch in "a short treatise on fungi".chars() {
    let counter = letters.entry(ch).or_insert(0);
    *counter += 1;
}

assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);

pub fn entries(&mut self) -> Entries<K, V, S>[src]

Returns an iterator visiting all entries in insertion order. Iterator element type is OccupiedEntry<K, V, S>. Allows for removal as well as replacing the entry.

Examples

use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);

{
    let mut iter = map.entries();
    let mut entry = iter.next().unwrap();
    assert_eq!(&"a", entry.key());
    *entry.get_mut() = 17;
}

assert_eq!(&17, map.get(&"a").unwrap());

pub fn insert(&mut self, k: K, v: V) -> Option<V>[src]

Inserts a key-value pair into the map. If the key already existed, the old value is returned.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");
assert_eq!(map[&1], "a");
assert_eq!(map[&2], "b");

pub fn contains_key<Q>(&self, k: &Q) -> bool where
    K: Borrow<Q>,
    Q: Eq + Hash + ?Sized
[src]

Checks if the map contains the given key.

pub fn get<Q>(&self, k: &Q) -> Option<&V> where
    K: Borrow<Q>,
    Q: Eq + Hash + ?Sized
[src]

Returns the value corresponding to the key in the map.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");
map.insert(2, "c");
map.insert(3, "d");

assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), Some(&"c"));

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V> where
    K: Borrow<Q>,
    Q: Eq + Hash + ?Sized
[src]

Returns the mutable reference corresponding to the key in the map.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");

*map.get_mut(&1).unwrap() = "c";
assert_eq!(map.get(&1), Some(&"c"));

pub fn get_refresh<Q>(&mut self, k: &Q) -> Option<&mut V> where
    K: Borrow<Q>,
    Q: Eq + Hash + ?Sized
[src]

Returns the value corresponding to the key in the map.

If value is found, it is moved to the end of the list. This operation can be used in implemenation of LRU cache.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "d");

assert_eq!(map.get_refresh(&2), Some(&mut "b"));

assert_eq!((&2, &"b"), map.iter().rev().next().unwrap());

pub fn remove<Q>(&mut self, k: &Q) -> Option<V> where
    K: Borrow<Q>,
    Q: Eq + Hash + ?Sized
[src]

Removes and returns the value corresponding to the key from the map.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(2, "a");

assert_eq!(map.remove(&1), None);
assert_eq!(map.remove(&2), Some("a"));
assert_eq!(map.remove(&2), None);
assert_eq!(map.len(), 0);

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

Returns the maximum number of key-value pairs the map can hold without reallocating.

Examples

use linked_hash_map::LinkedHashMap;
let mut map: LinkedHashMap<i32, &str> = LinkedHashMap::new();
let capacity = map.capacity();

pub fn pop_front(&mut self) -> Option<(K, V)>[src]

Removes the first entry.

Can be used in implementation of LRU cache.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_front();
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&2), Some(&20));

pub fn front(&self) -> Option<(&K, &V)>[src]

Gets the first entry.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.front(), Some((&1, &10)));

pub fn pop_back(&mut self) -> Option<(K, V)>[src]

Removes the last entry.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_back();
assert_eq!(map.get(&1), Some(&10));
assert_eq!(map.get(&2), None);

pub fn back(&mut self) -> Option<(&K, &V)>[src]

Gets the last entry.

Examples

use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.back(), Some((&2, &20)));

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

Returns the number of key-value pairs in the map.

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

Returns whether the map is currently empty.

pub fn hasher(&self) -> &S[src]

Returns a reference to the map's hasher.

pub fn clear(&mut self)[src]

Clears the map of all key-value pairs.

pub fn iter(&self) -> Iter<K, V>[src]

Returns a double-ended iterator visiting all key-value pairs in order of insertion. Iterator element type is (&'a K, &'a V)

Examples

use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);

let mut iter = map.iter();
assert_eq!((&"a", &10), iter.next().unwrap());
assert_eq!((&"c", &30), iter.next().unwrap());
assert_eq!((&"b", &20), iter.next().unwrap());
assert_eq!(None, iter.next());

pub fn iter_mut(&mut self) -> IterMut<K, V>[src]

Returns a double-ended iterator visiting all key-value pairs in order of insertion. Iterator element type is (&'a K, &'a mut V)

Examples

use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);

{
    let mut iter = map.iter_mut();
    let mut entry = iter.next().unwrap();
    assert_eq!(&"a", entry.0);
    *entry.1 = 17;
}

assert_eq!(&17, map.get(&"a").unwrap());

pub fn keys(&self) -> Keys<K, V>[src]

Returns a double-ended iterator visiting all key in order of insertion.

Examples

use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);

let mut keys = map.keys();
assert_eq!(&'a', keys.next().unwrap());
assert_eq!(&'c', keys.next().unwrap());
assert_eq!(&'b', keys.next().unwrap());
assert_eq!(None, keys.next());

pub fn values(&self) -> Values<K, V>[src]

Returns a double-ended iterator visiting all values in order of insertion.

Examples

use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);

let mut values = map.values();
assert_eq!(&10, values.next().unwrap());
assert_eq!(&30, values.next().unwrap());
assert_eq!(&20, values.next().unwrap());
assert_eq!(None, values.next());

Trait Implementations

impl Clone for MarkedMappingNode[src]

impl Debug for MarkedMappingNode[src]

impl Deref for MarkedMappingNode[src]

type Target = LinkedHashMap<MarkedScalarNode, Node>

The resulting type after dereferencing.

impl DerefMut for MarkedMappingNode[src]

impl Eq for MarkedMappingNode[src]

impl From<LinkedHashMap<MarkedScalarNode, Node, RandomState>> for MarkedMappingNode[src]

impl From<MarkedMappingNode> for Node[src]

impl From<MarkedMappingNode> for YamlNode[src]

impl<T, U> FromIterator<(T, U)> for MarkedMappingNode where
    T: Into<MarkedScalarNode>,
    U: Into<Node>, 
[src]

fn from_iter<I: IntoIterator<Item = (T, U)>>(iter: I) -> Self[src]

Allow collecting into a mapping node

hashmap.insert("hello", vec!["world".to_string()]);
hashmap.insert("key", vec!["value".to_string()]);
let node: MarkedMappingNode = hashmap.into_iter().collect();

impl Hash for MarkedMappingNode[src]

impl PartialEq<MarkedMappingNode> for MarkedMappingNode[src]

Auto Trait Implementations

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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.