Skip to main content

YamlNode

Enum YamlNode 

Source
pub enum YamlNode {
    Scalar(Scalar),
    Mapping(Mapping),
    Sequence(Sequence),
    Alias(Alias),
    TaggedNode(TaggedNode),
}
Expand description

A type-erased handle to a CST node returned from navigation methods.

You get a YamlNode back from methods like Mapping::get, Sequence::get, Mapping::keys, etc.

Match on the variants to get the concrete type, or use the helper methods as_scalar, as_mapping, etc.

use yaml_edit::{Document, YamlNode};
use std::str::FromStr;

let doc = Document::from_str("name: Alice\nage: 30").unwrap();

if let Some(node) = doc.get("name") {
    match node {
        YamlNode::Scalar(s) => println!("scalar: {}", s.as_string()),
        YamlNode::Mapping(m) => println!("mapping with {} keys", m.len()),
        YamlNode::Sequence(s) => println!("sequence with {} items", s.len()),
        YamlNode::Alias(a) => println!("alias: *{}", a.name()),
        YamlNode::TaggedNode(t) => println!("tagged: {:?}", t.tag()),
    }
}

Variants§

§

Scalar(Scalar)

A scalar value (string, integer, float, boolean, null).

§

Mapping(Mapping)

A key-value mapping.

§

Sequence(Sequence)

An ordered sequence.

§

Alias(Alias)

An alias reference (e.g. *anchor_name).

§

TaggedNode(TaggedNode)

A tagged node (e.g. !!set, !!omap, !!pairs, or a custom tag).

Implementations§

Source§

impl YamlNode

Source

pub fn from_syntax(node: SyntaxNode<Lang>) -> Option<Self>

Cast a SyntaxNode to a YamlNode.

Returns None if the node’s kind is not one of SCALAR, MAPPING, SEQUENCE, ALIAS, or TAGGED_NODE.

Source

pub fn kind(&self) -> YamlKind

Returns the kind of YAML value this node represents.

Source

pub fn yaml_eq<O: AsYaml>(&self, other: &O) -> bool

Compare semantically with another value (ignores quoting/formatting).

Source

pub fn as_scalar(&self) -> Option<&Scalar>

If this node is a scalar, return a reference to it.

Source

pub fn as_mapping(&self) -> Option<&Mapping>

If this node is a mapping, return a reference to it.

Source

pub fn as_sequence(&self) -> Option<&Sequence>

If this node is a sequence, return a reference to it.

Source

pub fn as_tagged(&self) -> Option<&TaggedNode>

If this node is a tagged node, return a reference to it.

Source

pub fn as_alias(&self) -> Option<&Alias>

If this node is an alias, return a reference to it.

Source

pub fn is_scalar(&self) -> bool

Returns true if this node is a scalar.

Source

pub fn is_mapping(&self) -> bool

Returns true if this node is a mapping.

Source

pub fn is_sequence(&self) -> bool

Returns true if this node is a sequence.

Source

pub fn is_tagged(&self) -> bool

Returns true if this node is a tagged node.

Source

pub fn is_alias(&self) -> bool

Returns true if this node is an alias.

Source

pub fn to_i64(&self) -> Option<i64>

If this node is a scalar, try to parse it as an i64.

Returns None if this is not a scalar or cannot be parsed as an integer.

Source

pub fn to_f64(&self) -> Option<f64>

If this node is a scalar, try to parse it as an f64.

Returns None if this is not a scalar or cannot be parsed as a float.

Source

pub fn to_bool(&self) -> Option<bool>

If this node is a scalar, try to parse it as a bool.

Returns None if this is not a scalar or cannot be parsed as a boolean.

Source

pub fn get(&self, key: impl AsYaml) -> Option<YamlNode>

Get a value by key from a mapping node.

Returns None if this node is not a mapping or the key is not found.

Source

pub fn get_item(&self, index: usize) -> Option<YamlNode>

Get a value by index from a sequence node.

Returns None if this node is not a sequence or the index is out of bounds.

Trait Implementations§

Source§

impl AsYaml for YamlNode

Source§

fn as_node(&self) -> Option<&SyntaxNode<Lang>>

Returns a reference to the underlying SyntaxNode if one exists. Read more
Source§

fn kind(&self) -> YamlKind

Returns the kind of YAML value this represents.
Source§

fn build_content( &self, builder: &mut GreenNodeBuilder<'_>, _indent: usize, _flow_context: bool, ) -> bool

Serialize this value into a GreenNodeBuilder. Read more
Source§

fn is_inline(&self) -> bool

Returns whether this value should be rendered on the same line as its key. Read more
Source§

impl Clone for YamlNode

Source§

fn clone(&self) -> YamlNode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for YamlNode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for YamlNode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq<&str> for YamlNode

Source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for YamlNode

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for YamlNode

yaml_node == "some_string" — compares the node’s semantic value.

Source§

fn eq(&self, other: &str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for YamlNode

Source§

fn eq(&self, other: &YamlNode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for YamlNode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.