Skip to main content

Module mapping_view

Module mapping_view 

Source
Expand description

Read-only mapping interface shared by [Mapping] and MergedMapping.

MappingView abstracts over “anything that looks like a YAML mapping for the purposes of reading it.” It lets you write code that works generically against both the underlying CST [Mapping] and the alias/merge-key–resolving MergedMapping view.

Iterators are returned as boxed trait objects so the trait stays object-safe and works on the crate’s MSRV. Most uses pay one heap allocation per iteration — negligible for an editing library.

§Example

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

fn count_keys(view: &impl MappingView) -> usize {
    view.keys().count()
}

let yaml = "\
defaults: &d
  timeout: 30
prod:
  <<: *d
  host: prod.example.com
";
let doc = Document::from_str(yaml).unwrap();
let root = doc.as_mapping().unwrap();

// Works on a plain Mapping…
assert_eq!(count_keys(&root), 2);

// …and on a MergedMapping.
let registry = doc.merged().unwrap();
let prod = registry.as_mapping().get_merged("prod").unwrap();
assert_eq!(count_keys(&prod), 2); // timeout + host

The trait is intentionally read-only: mutations only make sense on the CST [Mapping], so set, remove, etc. remain on that type.

Traits§

MappingView
A read-only “view” of a YAML mapping, implemented by both the CST Mapping and the alias/merge-key–resolving MergedMapping.