pub struct NodeBase<R: Repr>(_);
Expand description

Readonly node, including line number, column number, type assertion and anchor. You can access YamlBase type through NodeBase::yaml method.

This type will ignore additional information when comparison and hashing.

use std::collections::HashSet;
use yaml_peg::{Node, Yaml};

let mut s = HashSet::new();
s.insert(Node::new(Yaml::from("a"), 0, "", ""));
s.insert(Node::new("a", 1, "my-tag", ""));
s.insert(Node::new("a", 2, "", "my-anchor"));
assert_eq!(s.len(), 1);

There is also a convenient macro node! to create nodes literally. Please see the macro description for more information.

Nodes can be indexing by convertable values, or sequence indicator Ind, but it will be panic if the index is not contained.

use yaml_peg::{node, Ind};

let n = node!(["a", "b", "c"]);
assert_eq!(node!("b"), n[Ind(1)]);
use yaml_peg::{node, Ind};

let n = node!(());
let n = &n["a"][Ind(0)]["b"];

Same as containers, to prevent panic, the NodeBase::get method is the best choice. The NodeBase::get_default can provide missing key value when indexing.

There are as_* methods provide Result<T, u64> returns with node position, default options can be created by Result::unwrap_or, additional error message can be attach by Result::map_err, and the optional Option can be return by Result::ok, which shown as following example:

use yaml_peg::node;

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

Anchor

The anchors can be infer from AnchorBase, and attach with NodeBase::as_anchor method.

Clone

Since the YAML data is wrapped by reference counter alloc::rc::Rc and alloc::sync::Arc, cloning node just increase the reference counter, the entire data structure are still shared together.

use std::rc::Rc;
use yaml_peg::node;

let a = node!("a");
{
    let b = a.clone();
    assert_eq!(2, Rc::strong_count(&b));
}
assert_eq!(1, Rc::strong_count(&a));

If you want to copy data, please get the data first.

Implementations

Create node from YAML data.

Document position.

Tag. If the tag is not specified, returns a default tag from core schema.

Anchor has no tag.

Anchor reference.

YAML data.

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!(()).as_str().unwrap().is_empty());

Convert to string pointer for string, null, bool, int, and float type.

This method is useful when the option mixed with digit values.

use yaml_peg::node;

assert_eq!("abc", node!("abc").as_value().unwrap());
assert_eq!("123", node!(123).as_value().unwrap());
assert_eq!("12.04", node!(12.04).as_value().unwrap());
assert_eq!("true", node!(true).as_value().unwrap());
assert_eq!("false", node!(false).as_value().unwrap());
assert!(node!(()).as_value().unwrap().is_empty());

Infer an anchor with anchor visitor.

If the node is not a anchor, or the anchor does not exist, the original node is returned. Since the anchor type is invalid except for this method, missing anchor will still return an error.

use yaml_peg::{node, anchors};

let node_a = node!(*"a");
let v = anchors!["a" => 20.];
assert_eq!(20., node_a.as_anchor(&v).as_float().unwrap());

Convert to sequence.

use yaml_peg::node;

let n = node!(["55"]);
assert_eq!(node!("55"), n.as_seq().unwrap()[0]);
for n in n.as_seq().unwrap() {
    assert_eq!(node!("55"), n);
}

Convert to map.

use yaml_peg::node;

let n = node!({1 => 2});
assert_eq!(node!(2), n.as_map().unwrap()[&node!(1)]);
for (k, v) in n.as_map().unwrap() {
    assert_eq!(node!(1), k);
    assert_eq!(node!(2), v);
}

Convert to map and try to get the value by key.

If any key is missing, return Err with node position.

use yaml_peg::node;

let n = node!({node!("a") => node!({node!("b") => node!(30.)})});
assert_eq!(&node!(30.), n.get("a")?.get("b")?);

Same as NodeBase::get but provide default value if the key is missing. For this method, a transform method as_* is required.

  • If the value exist, return the value.
  • If value is a wrong type, return Err with node position.
  • If the value is not exist, return the default value.
use yaml_peg::{node, NodeBase};

let a = node!({node!("a") => node!({node!("b") => node!("c")})});
assert_eq!(
    "c",
    a.get("a")?.get_default("b", "d", NodeBase::as_str)?
);
let b = node!({node!("a") => node!({})});
assert_eq!(
    "d",
    b.get("a")?.get_default("b", "d", NodeBase::as_str)?
);
let c = node!({node!("a") => node!({node!("b") => node!(20.)})});
assert_eq!(
    Err(0),
    c.get("a")?.get_default("b", "d", NodeBase::as_str)
);
use yaml_peg::{node, NodeBase};

let n = node!({node!("a") => node!([node!(1), node!(2), node!(3)])});
let a = n.get_default("c", vec![], NodeBase::as_seq)?;
assert_eq!(a, vec![]);

Trait as map then get a key-value with anchor visitor, returns default value if key is not found.

If the value type is incorrect, returns Err. Same as NodeBase::get_default but support anchor visitor.

use yaml_peg::{anchors, node, Node};

let v = anchors!["c" => 10];
let a = node!({node!("a") => node!({node!("b") => node!(*"c")})});
let c = a.get("a")?.with(&v, "b", 0, Node::as_int)?;
assert_eq!(10, c);
let c = a.get("a")?.with(&v, "b", "default", Node::as_str);
assert_eq!(Err(0), c);

Get node through index indicator. Only suitable for sequence.

use yaml_peg::{node, Ind};

let n = node!([node!("a"), node!("b"), node!("c")]);
assert_eq!(&node!("b"), n.get_ind(Ind(1))?);

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

The resulting type after dereferencing.

Dereferences the value.

The resulting type after dereferencing.

Dereferences the value.

Deserialize this value from the given Serde deserializer. Read more

The error type that can be returned if some error occurs during deserialization. Read more

Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more

Hint that the Deserialize type is expecting a bool value.

Hint that the Deserialize type is expecting an i8 value.

Hint that the Deserialize type is expecting an i16 value.

Hint that the Deserialize type is expecting an i32 value.

Hint that the Deserialize type is expecting an i64 value.

Hint that the Deserialize type is expecting a u8 value.

Hint that the Deserialize type is expecting a u16 value.

Hint that the Deserialize type is expecting a u32 value.

Hint that the Deserialize type is expecting a u64 value.

Hint that the Deserialize type is expecting a f32 value.

Hint that the Deserialize type is expecting a f64 value.

Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more

Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more

Hint that the Deserialize type is expecting a char value.

Hint that the Deserialize type is expecting a sequence of values.

Hint that the Deserialize type is expecting a map of key-value pairs.

Hint that the Deserialize type is expecting the name of a struct field or the discriminant of an enum variant. Read more

Hint that the Deserialize type is expecting an i128 value. Read more

Hint that the Deserialize type is expecting an u128 value. Read more

Hint that the Deserialize type is expecting a byte array and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more

Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more

Hint that the Deserialize type is expecting an optional value. Read more

Hint that the Deserialize type is expecting a unit value.

Hint that the Deserialize type is expecting a unit struct with a particular name. Read more

Hint that the Deserialize type is expecting a newtype struct with a particular name. Read more

Hint that the Deserialize type is expecting a sequence of values and knows how many values there are without looking at the serialized data. Read more

Hint that the Deserialize type is expecting a tuple struct with a particular name and number of fields. Read more

Hint that the Deserialize type is expecting a struct with a particular name and fields. Read more

Hint that the Deserialize type is expecting an enum value with a particular name and possible variants. Read more

Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more

Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more

Performs the conversion.

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

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 !=.

Serialize this value into the given Serde serializer. Read more

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)

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.