Struct yaml_peg::Node[][src]

pub struct Node {
    pub pos: u64,
    pub ty: String,
    pub anchor: String,
    pub yaml: Yaml,
}
Expand description

Parser node, includes line number, column number, type assertion and anchor.

This type will ignore additional members when comparison and hashing.

use std::collections::HashSet;
use yaml_peg::Node;
let mut s = HashSet::new();
s.insert(Node::new("a".into()).pos(0));
s.insert(Node::new("a".into()).pos(1));
s.insert(Node::new("a".into()).pos(2));
assert_eq!(s.len(), 1);

There is a convenient macro node! to create nodes literally.

Nodes can be indexing by usize or &str, but it will always return self if the index is not contained.

use yaml_peg::node;
let n = node!(null);
assert_eq!(n["a"][0]["bc"], n);

There are as_* methods provide Option returns, default options can be created by Option::unwrap_or, and the error Result can be return by Option::ok_or to indicate the position, which shown as following example:

use yaml_peg::node;

fn main() -> Result<(), (&'static str, u64)> {
    let n = node!({
        node!("title") => node!(12.)
    });
    let n = n.as_get(&["title"]).map_err(|p| ("missing \"title\"", p))?;
    assert_eq!(
        Err(("title", 0)),
        n.as_str().map_err(|p| ("title", p))
    );
    Ok(())
}

Fields

pos: u64

Document position

ty: String

Type assertion

anchor: String

Anchor reference

yaml: Yaml

YAML data

Implementations

Create node from YAML data.

Builder function for position.

Builder function for type assertion.

Builder function for anchor annotation.

Check the value is null.

Convert to boolean.

use yaml_peg::{node};
assert!(node!(true).as_bool().unwrap());

Convert to integer.

use yaml_peg::node;
assert_eq!(60, node!(60).as_int().unwrap());

Convert to float.

use yaml_peg::node;
assert_eq!(20.06, node!(20.06).as_float().unwrap());

Convert to number.

use yaml_peg::node;
assert_eq!(60, node!(60).as_number().unwrap());
assert_eq!(20.06, node!(20.06).as_number().unwrap());

Convert to string pointer.

This method allows null, it represented as empty string. You can check them by str::is_empty.

use yaml_peg::node;
assert_eq!("abc", node!("abc").as_str().unwrap());
assert!(node!(null).as_str().unwrap().is_empty());

Convert to the string pointer of an anchor.

use yaml_peg::node;
assert_eq!("abc", node!(*("abc")).as_anchor().unwrap());

Convert to array.

WARNING: The object ownership will be took.

use yaml_peg::node;
assert_eq!(
    &vec![node!(1), node!(2)],
    node!([node!(1), node!(2)]).as_array().unwrap()
);

Convert to map.

WARNING: The object ownership will be took.

use yaml_peg::node;
assert_eq!(
    &node!(2),
    node!({node!(1) => node!(2)}).as_map().unwrap().get(&node!(1)).unwrap()
);

Convert to map and try to get the value by keys recursivly.

If get failed, returns Option::None.

use yaml_peg::node;
assert_eq!(
    &node!(30.),
    node!({node!("a") => node!({node!("b") => node!(30.)})}).as_get(&["a", "b"]).unwrap()
);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Recursive dump function.

Generate indentation.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

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

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

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

Performs the conversion.

Performs the conversion.

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)

recently added

Uses borrowed data to replace owned data, usually by cloning. 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.