Skip to main content

MergedMapping

Struct MergedMapping 

Source
pub struct MergedMapping<'a> { /* private fields */ }
Expand description

A view over a Mapping that transparently expands alias references (*name) and merge keys (<<:).

MergedMapping is a lightweight, zero-copy view: it borrows the underlying mapping and an AnchorRegistry. The underlying syntax tree is not modified — every lookup walks the CST on demand and resolves aliases via the registry.

Direct keys in the base mapping always shadow keys contributed by merge sources, matching the YAML 1.1 merge-key semantics. When a merge value is a sequence of aliases (<<: [*a, *b]), earlier aliases take precedence over later ones, again per the YAML 1.1 spec.

Returned values are YamlNodes backed by real CST nodes, so they preserve the original formatting and quoting style.

§Examples

use yaml_edit::{Document, anchor_resolution::{DocumentResolvedExt, MappingMergedExt}};
use std::str::FromStr;

let yaml = r#"
defaults: &defaults
  timeout: 30
  retries: 3

production:
  <<: *defaults
  host: prod.example.com
  timeout: 60
"#;

let doc = Document::from_str(yaml).unwrap();
let root = doc.as_mapping().unwrap();
let registry = doc.build_anchor_registry();

let prod = root.get_mapping("production").unwrap();
let merged = prod.merged(&registry);

// Direct key wins over merged value.
assert_eq!(merged.get("timeout").unwrap().to_i64(), Some(60));
// Merged key from defaults is visible.
assert_eq!(merged.get("retries").unwrap().to_i64(), Some(3));
// Direct-only key is visible.
assert!(merged.get("host").is_some());
// `<<` itself is hidden.
assert!(merged.get("<<").is_none());

Implementations§

Source§

impl<'a> MergedMapping<'a>

Source

pub fn new(base: Mapping, registry: &'a AnchorRegistry) -> Self

Create a new merged view over base, resolving aliases against registry.

Source

pub fn base(&self) -> &Mapping

Return the underlying (un-merged) Mapping.

Useful when you want to mutate the original mapping or read its raw (non-resolved) contents.

Source

pub fn registry(&self) -> &AnchorRegistry

Return the AnchorRegistry this view resolves against.

Source

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

Get the value associated with key, resolving aliases and merge keys.

Direct entries in the base mapping take precedence over keys contributed by <<: merge sources. The synthetic << key itself is hidden — looking it up returns None.

If the matched value is an alias (*name), the resolved target node is returned. If no such anchor is defined, the alias is left unresolved and returned as-is.

Source

pub fn contains_key(&self, key: impl AsYaml) -> bool

Returns true if a value would be returned by get for key.

Source

pub fn iter(&self) -> impl Iterator<Item = (YamlNode, YamlNode)> + '_

Iterate over (key, value) pairs in the merged view.

Direct entries appear first, in their original document order. Merged-in entries follow, with duplicates (and any direct-key matches already yielded) filtered out. The << merge key itself is never yielded.

Source

pub fn keys(&self) -> impl Iterator<Item = YamlNode> + '_

Iterate over the keys of the merged view.

Source

pub fn values(&self) -> impl Iterator<Item = YamlNode> + '_

Iterate over the values of the merged view.

Source

pub fn len(&self) -> usize

Number of distinct keys visible through the merged view.

Source

pub fn is_empty(&self) -> bool

Returns true if the merged view has no entries.

Source

pub fn get_merged(&self, key: impl AsYaml) -> Option<MergedMapping<'a>>

Get a nested mapping by key and return it as another MergedMapping that shares the same registry.

Returns None if the key does not exist or the value is not a mapping.

Trait Implementations§

Source§

impl<'a> Clone for MergedMapping<'a>

Source§

fn clone(&self) -> MergedMapping<'a>

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 MergedMapping<'_>

Source§

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

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

impl MappingView for MergedMapping<'_>

Source§

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

Look up the value associated with key. Read more
Source§

fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (YamlNode, YamlNode)> + 'a>

Iterate over (key, value) pairs.
Source§

fn contains_key(&self, key: &dyn AsYaml) -> bool

Returns true if get would return Some for key.
Source§

fn len(&self) -> usize

Number of entries visible through this view.
Source§

fn is_empty(&self) -> bool

Returns true if the view has no entries.
Source§

fn keys<'a>(&'a self) -> Box<dyn Iterator<Item = YamlNode> + 'a>

Iterate over the keys.
Source§

fn values<'a>(&'a self) -> Box<dyn Iterator<Item = YamlNode> + 'a>

Iterate over the values.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for MergedMapping<'a>

§

impl<'a> !Send for MergedMapping<'a>

§

impl<'a> !Sync for MergedMapping<'a>

§

impl<'a> !UnwindSafe for MergedMapping<'a>

§

impl<'a> Freeze for MergedMapping<'a>

§

impl<'a> Unpin for MergedMapping<'a>

§

impl<'a> UnsafeUnpin for MergedMapping<'a>

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, 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.