pub enum YamlValue {
Scalar(ScalarValue),
Sequence(Vec<YamlValue>),
Mapping(BTreeMap<String, YamlValue>),
Set(BTreeSet<String>),
OrderedMapping(Vec<(String, YamlValue)>),
Pairs(Vec<(String, YamlValue)>),
Tagged(String, Box<YamlValue>),
}Expand description
Represents any YAML value - scalar, sequence, mapping, or special collections.
Deprecated: Prefer using syntax tree types (Mapping, Sequence,
Scalar) and the AsYaml trait instead - they preserve formatting and
work with the CST directly. YamlValue keeps tags and scalar styles, but
loses comments, collection styles and other formatting.
Variants§
Scalar(ScalarValue)
A scalar value (string, number, boolean, null)
Sequence(Vec<YamlValue>)
A sequence of values (list/array)
Mapping(BTreeMap<String, YamlValue>)
A mapping of key-value pairs
Converted from a document, each key is held by its string form:
the canonical value of a scalar (0x10 is "16", ~ is
"null") and a flow rendering of a collection, with mapping
entries sorted ({y: 1, x: 2} is "{x: 2, y: 1}"). Equal keys
have the same string form, however they are written.
Set(BTreeSet<String>)
A set of unique values (!!set)
OrderedMapping(Vec<(String, YamlValue)>)
An ordered mapping preserving key order (!!omap)
Pairs(Vec<(String, YamlValue)>)
A sequence of key-value pairs allowing duplicates (!!pairs)
Tagged(String, Box<YamlValue>)
A sequence or mapping with an explicit tag, such as !!map or
!local, other than the ones with their own variant above.
Tags on scalars are kept on the ScalarValue instead; use
YamlValue::tagged to attach a tag to either.
Implementations§
Source§impl YamlValue
impl YamlValue
Sourcepub fn cast(node: SyntaxNode<Lang>) -> Result<Option<Self>, ResolveError>
pub fn cast(node: SyntaxNode<Lang>) -> Result<Option<Self>, ResolveError>
Convert a syntax node to a YamlValue.
Returns Ok(None) if node is not a YAML value, such as a
Document node. A KEY or VALUE wrapper is
converted as its content.
Content that a YamlValue cannot hold is an error rather than being
dropped: an alias, which this cannot resolve without the document’s
anchors (YamlValue::from_document and
DocumentResolvedExt::get_resolved resolve them), a mapping with
the same key twice, distinct mapping keys with the same string form, such as 1 and
'1', and !!set, !!omap or !!pairs content of the wrong shape.
A merge key (<<) is kept as an ordinary key.
use std::str::FromStr;
use rowan::ast::AstNode;
use yaml_edit::{Document, YamlValue};
use yaml_edit::anchor_resolution::ResolveError;
let doc = Document::from_str("a: &x 1\nb: [*x, 2]\n").unwrap();
let b = doc.as_mapping().unwrap().get("b").unwrap();
assert_eq!(
YamlValue::cast(b.as_sequence().unwrap().syntax().clone()),
Err(ResolveError::UnresolvedAlias { alias: "x".to_string() })
);Sourcepub fn scalar(value: impl Into<ScalarValue>) -> Self
pub fn scalar(value: impl Into<ScalarValue>) -> Self
Create a scalar value
Sourcepub const fn from_sequence(items: Vec<YamlValue>) -> Self
pub const fn from_sequence(items: Vec<YamlValue>) -> Self
Create a sequence from a vector
Sourcepub fn parse_raw(yaml_str: &str) -> YamlResult<Self>
pub fn parse_raw(yaml_str: &str) -> YamlResult<Self>
Parse a raw YAML string into a YamlValue This handles both simple scalars and complex structures
Input without a document, such as an empty string, is null.
Fails with YamlError::Parse if
yaml_str does not parse, with
YamlError::InvalidOperation
if it holds more than one document, and with
YamlError::Resolve where
YamlValue::from_document fails. Convert each document of a
multi-document stream with YamlValue::from_document instead.
Sourcepub fn from_document(doc: &Document) -> Result<Self, ResolveError>
pub fn from_document(doc: &Document) -> Result<Self, ResolveError>
Convert a Document to a YamlValue
Unlike YamlValue::cast, this resolves aliases against the
document’s anchors and applies merge keys (<<), as
DocumentResolvedExt::get_resolved does. Merge keys are a YAML 1.1
type that YAML 1.2 does not define, but are still widely used.
An empty document is null.
Fails if an alias is undefined or recursive, a merge key operand is
not a mapping, or on other content YamlValue::cast reports.
Sourcepub const fn from_mapping(map: BTreeMap<String, YamlValue>) -> Self
pub const fn from_mapping(map: BTreeMap<String, YamlValue>) -> Self
Create a mapping from a BTreeMap
Sourcepub const fn ordered_mapping() -> Self
pub const fn ordered_mapping() -> Self
Create an empty ordered mapping
Sourcepub const fn from_ordered_mapping(pairs: Vec<(String, YamlValue)>) -> Self
pub const fn from_ordered_mapping(pairs: Vec<(String, YamlValue)>) -> Self
Create an ordered mapping from a vector of pairs
Sourcepub const fn from_pairs(pairs: Vec<(String, YamlValue)>) -> Self
pub const fn from_pairs(pairs: Vec<(String, YamlValue)>) -> Self
Create pairs from a vector of pairs
Sourcepub fn tagged(tag: impl Into<String>, value: YamlValue) -> Self
pub fn tagged(tag: impl Into<String>, value: YamlValue) -> Self
Attach an explicit tag to value.
A scalar keeps the tag itself (see ScalarValue::with_tag), any
other value is wrapped in YamlValue::Tagged.
use yaml_edit::YamlValue;
let v = YamlValue::tagged("!point", YamlValue::from(vec![1, 2]));
assert_eq!(v.tag(), Some("!point"));
assert_eq!(v.to_yaml_string(0), "!point\n - 1\n - 2");Sourcepub fn tag(&self) -> Option<&str>
pub fn tag(&self) -> Option<&str>
The explicit tag on this value, if any.
!!set, !!omap and !!pairs values report their tag too.
Sourcepub fn is_scalar(&self) -> bool
pub fn is_scalar(&self) -> bool
Return true if this value is a scalar (string, integer, float, bool, null, etc.).
Use as_scalar to access the inner ScalarValue.
Sourcepub fn is_sequence(&self) -> bool
pub fn is_sequence(&self) -> bool
Return true if this value is a sequence (YAML list / array).
Use as_sequence to access the inner sequence.
Sourcepub fn is_mapping(&self) -> bool
pub fn is_mapping(&self) -> bool
Return true if this value is a plain mapping (key: value pairs).
Use as_mapping to access the inner mapping.
Sourcepub fn is_ordered_mapping(&self) -> bool
pub fn is_ordered_mapping(&self) -> bool
Return true if this value is a !!omap (ordered mapping) tagged collection.
Sourcepub fn as_scalar(&self) -> Option<&ScalarValue>
pub fn as_scalar(&self) -> Option<&ScalarValue>
Get as scalar if this is a scalar
Sourcepub fn as_sequence(&self) -> Option<&[YamlValue]>
pub fn as_sequence(&self) -> Option<&[YamlValue]>
Get as sequence if this is a sequence
Sourcepub fn as_sequence_mut(&mut self) -> Option<&mut Vec<YamlValue>>
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<YamlValue>>
Get as mutable sequence if this is a sequence
Sourcepub fn as_mapping(&self) -> Option<&BTreeMap<String, YamlValue>>
pub fn as_mapping(&self) -> Option<&BTreeMap<String, YamlValue>>
Get as mapping if this is a mapping
Sourcepub fn as_mapping_mut(&mut self) -> Option<&mut BTreeMap<String, YamlValue>>
pub fn as_mapping_mut(&mut self) -> Option<&mut BTreeMap<String, YamlValue>>
Get as mutable mapping if this is a mapping
Sourcepub fn as_set_mut(&mut self) -> Option<&mut BTreeSet<String>>
pub fn as_set_mut(&mut self) -> Option<&mut BTreeSet<String>>
Get as mutable set if this is a set
Sourcepub fn as_ordered_mapping(&self) -> Option<&[(String, YamlValue)]>
pub fn as_ordered_mapping(&self) -> Option<&[(String, YamlValue)]>
Get as ordered mapping if this is an ordered mapping
Sourcepub fn as_ordered_mapping_mut(
&mut self,
) -> Option<&mut Vec<(String, YamlValue)>>
pub fn as_ordered_mapping_mut( &mut self, ) -> Option<&mut Vec<(String, YamlValue)>>
Get as mutable ordered mapping if this is an ordered mapping
Sourcepub fn as_pairs_mut(&mut self) -> Option<&mut Vec<(String, YamlValue)>>
pub fn as_pairs_mut(&mut self) -> Option<&mut Vec<(String, YamlValue)>>
Get as mutable pairs if this is pairs
Sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
Get a reference to the string value if this is a scalar
Returns the string representation of the scalar value. Returns None for non-scalar types (mappings, sequences, etc). Note: All scalars can be represented as strings, regardless of their type.
Sourcepub fn to_i64(&self) -> Option<i64>
pub fn to_i64(&self) -> Option<i64>
Try to convert this value to an i64
Returns the integer value if this is a scalar with Integer type.
Reads the YAML 1.2 forms: decimal, hexadecimal 0x and octal 0o.
Returns None if this is not a scalar or not an integer type.
Note: This respects YAML’s type system - 42 (unquoted) is an integer,
but "42" (quoted) is a string and will return None.
Sourcepub fn to_f64(&self) -> Option<f64>
pub fn to_f64(&self) -> Option<f64>
Try to convert this value to an f64
Returns the float value if this is a scalar with Float type. Returns None if this is not a scalar or not a float type.
Note: This respects YAML’s type system - 3.14 (unquoted) is a float,
but "3.14" (quoted) is a string and will return None.
Sourcepub fn to_bool(&self) -> Option<bool>
pub fn to_bool(&self) -> Option<bool>
Try to convert this value to a bool
Returns the boolean value if this is a scalar with Boolean type. Recognizes: true, True, TRUE, false, False, FALSE. Returns None if this is not a scalar or not a boolean type.
Note: This respects YAML’s type system - false (unquoted) is boolean,
but "false" (quoted) is a string and will return None.
Sourcepub fn to_yaml_string(&self, indent: usize) -> String
pub fn to_yaml_string(&self, indent: usize) -> String
Convert to YAML string representation, written at column indent:
a block mapping or sequence has every line indented by indent, and
anything else has the lines after its first, such as the body of a
block scalar, indented past it.
The text has no final line break, except where a block scalar ends it with an empty line: that line needs its line break, or the value would read back without it.
Trait Implementations§
Source§impl AsYaml for YamlValue
impl AsYaml for YamlValue
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 more