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(®istry);
// 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>
impl<'a> MergedMapping<'a>
Sourcepub fn new(base: Mapping, registry: &'a AnchorRegistry) -> Self
pub fn new(base: Mapping, registry: &'a AnchorRegistry) -> Self
Create a new merged view over base, resolving aliases against registry.
Sourcepub fn base(&self) -> &Mapping
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.
Sourcepub fn registry(&self) -> &AnchorRegistry
pub fn registry(&self) -> &AnchorRegistry
Return the AnchorRegistry this view resolves against.
Sourcepub fn get(&self, key: impl AsYaml) -> Option<YamlNode>
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.
Sourcepub fn contains_key(&self, key: impl AsYaml) -> bool
pub fn contains_key(&self, key: impl AsYaml) -> bool
Returns true if a value would be returned by get for
key.
Sourcepub fn iter(&self) -> impl Iterator<Item = (YamlNode, YamlNode)> + '_
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.
Sourcepub fn keys(&self) -> impl Iterator<Item = YamlNode> + '_
pub fn keys(&self) -> impl Iterator<Item = YamlNode> + '_
Iterate over the keys of the merged view.
Sourcepub fn values(&self) -> impl Iterator<Item = YamlNode> + '_
pub fn values(&self) -> impl Iterator<Item = YamlNode> + '_
Iterate over the values of the merged view.
Sourcepub fn get_merged(&self, key: impl AsYaml) -> Option<MergedMapping<'a>>
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>
impl<'a> Clone for MergedMapping<'a>
Source§fn clone(&self) -> MergedMapping<'a>
fn clone(&self) -> MergedMapping<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more