Skip to main content

YamlValue

Enum YamlValue 

Source
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

Source

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() })
);
Source

pub fn scalar(value: impl Into<ScalarValue>) -> Self

Create a scalar value

Source

pub const fn sequence() -> Self

Create an empty sequence

Source

pub const fn from_sequence(items: Vec<YamlValue>) -> Self

Create a sequence from a vector

Source

pub const fn mapping() -> Self

Create an empty mapping

Source

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.

Source

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.

Source

pub const fn from_mapping(map: BTreeMap<String, YamlValue>) -> Self

Create a mapping from a BTreeMap

Source

pub const fn set() -> Self

Create an empty set

Source

pub const fn from_set(set: BTreeSet<String>) -> Self

Create a set from a BTreeSet

Source

pub const fn ordered_mapping() -> Self

Create an empty ordered mapping

Source

pub const fn from_ordered_mapping(pairs: Vec<(String, YamlValue)>) -> Self

Create an ordered mapping from a vector of pairs

Source

pub const fn pairs() -> Self

Create empty pairs

Source

pub const fn from_pairs(pairs: Vec<(String, YamlValue)>) -> Self

Create pairs from a vector of pairs

Source

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");
Source

pub fn tag(&self) -> Option<&str>

The explicit tag on this value, if any.

!!set, !!omap and !!pairs values report their tag too.

Source

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.

Source

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.

Source

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.

Source

pub fn is_set(&self) -> bool

Return true if this value is a !!set tagged collection.

Source

pub fn is_ordered_mapping(&self) -> bool

Return true if this value is a !!omap (ordered mapping) tagged collection.

Source

pub fn is_pairs(&self) -> bool

Return true if this value is a !!pairs tagged collection.

Source

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

Get as scalar if this is a scalar

Source

pub fn as_sequence(&self) -> Option<&[YamlValue]>

Get as sequence if this is a sequence

Source

pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<YamlValue>>

Get as mutable sequence if this is a sequence

Source

pub fn as_mapping(&self) -> Option<&BTreeMap<String, YamlValue>>

Get as mapping if this is a mapping

Source

pub fn as_mapping_mut(&mut self) -> Option<&mut BTreeMap<String, YamlValue>>

Get as mutable mapping if this is a mapping

Source

pub fn as_set(&self) -> Option<&BTreeSet<String>>

Get as set if this is a set

Source

pub fn as_set_mut(&mut self) -> Option<&mut BTreeSet<String>>

Get as mutable set if this is a set

Source

pub fn as_ordered_mapping(&self) -> Option<&[(String, YamlValue)]>

Get as ordered mapping if this is an ordered mapping

Source

pub fn as_ordered_mapping_mut( &mut self, ) -> Option<&mut Vec<(String, YamlValue)>>

Get as mutable ordered mapping if this is an ordered mapping

Source

pub fn as_pairs(&self) -> Option<&[(String, YamlValue)]>

Get as pairs if this is pairs

Source

pub fn as_pairs_mut(&mut self) -> Option<&mut Vec<(String, YamlValue)>>

Get as mutable pairs if this is pairs

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

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 YamlValue

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for YamlValue

Source§

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

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

impl Display for YamlValue

Source§

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

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

impl Eq for YamlValue

Source§

impl From<&YamlValue> for YamlValue

Source§

fn from(value: &YamlValue) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for YamlValue

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<BTreeMap<K, V>> for YamlValue
where K: Into<String>, V: Into<YamlValue>,

Source§

fn from(map: BTreeMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<BTreeSet<T>> for YamlValue
where T: Into<String>,

Source§

fn from(set: BTreeSet<T>) -> Self

Converts to this type from the input type.
Source§

impl From<ScalarValue> for YamlValue

Source§

fn from(value: ScalarValue) -> Self

Converts to this type from the input type.
Source§

impl From<String> for YamlValue

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for YamlValue
where T: Into<YamlValue>,

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for YamlValue

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for YamlValue

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for YamlValue

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for YamlValue

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for YamlValue

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for YamlValue

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for YamlValue

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.