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
impl YamlNode
Sourcepub fn from_syntax(node: SyntaxNode<Lang>) -> Option<Self>
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.
Sourcepub fn yaml_eq<O: AsYaml>(&self, other: &O) -> bool
pub fn yaml_eq<O: AsYaml>(&self, other: &O) -> bool
Compare semantically with another value (ignores quoting/formatting).
Sourcepub fn as_mapping(&self) -> Option<&Mapping>
pub fn as_mapping(&self) -> Option<&Mapping>
If this node is a mapping, return a reference to it.
Sourcepub fn as_sequence(&self) -> Option<&Sequence>
pub fn as_sequence(&self) -> Option<&Sequence>
If this node is a sequence, return a reference to it.
Sourcepub fn as_tagged(&self) -> Option<&TaggedNode>
pub fn as_tagged(&self) -> Option<&TaggedNode>
If this node is a tagged node, return a reference to it.
Sourcepub fn is_mapping(&self) -> bool
pub fn is_mapping(&self) -> bool
Returns true if this node is a mapping.
Sourcepub fn is_sequence(&self) -> bool
pub fn is_sequence(&self) -> bool
Returns true if this node is a sequence.
Sourcepub fn to_i64(&self) -> Option<i64>
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.
Sourcepub fn to_f64(&self) -> Option<f64>
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.
Sourcepub fn to_bool(&self) -> Option<bool>
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.
Trait Implementations§
Source§impl AsYaml for YamlNode
impl AsYaml for YamlNode
Source§fn as_node(&self) -> Option<&SyntaxNode<Lang>>
fn as_node(&self) -> Option<&SyntaxNode<Lang>>
SyntaxNode if one exists. Read moreSource§fn build_content(
&self,
builder: &mut GreenNodeBuilder<'_>,
_indent: usize,
_flow_context: bool,
) -> bool
fn build_content( &self, builder: &mut GreenNodeBuilder<'_>, _indent: usize, _flow_context: bool, ) -> bool
GreenNodeBuilder. Read moreSource§impl PartialEq<str> for YamlNode
yaml_node == "some_string" — compares the node’s semantic value.
impl PartialEq<str> for YamlNode
yaml_node == "some_string" — compares the node’s semantic value.