Skip to main content

DocumentResolvedExt

Trait DocumentResolvedExt 

Source
pub trait DocumentResolvedExt {
    // Required methods
    fn get_resolved(
        &self,
        key: impl AsYaml,
    ) -> Result<Option<YamlValue>, ResolveError>;
    fn build_anchor_registry(&self) -> AnchorRegistry;
}
Expand description

Extension trait for Document to support resolved lookups

Required Methods§

Source

fn get_resolved( &self, key: impl AsYaml, ) -> Result<Option<YamlValue>, ResolveError>

Get a value with anchor/alias resolution

This method resolves aliases (*alias) to their anchored values (&anchor), and supports merge keys (<<) for combining mappings.

Returns Ok(None) if the document root is not a mapping or has no such key, and an error if the value contains an undefined or recursive alias, an invalid merge key, a mapping with the same key twice, or distinct keys that have the same string form, such as 1 and '1'.

Resolution is read-only. To write a merge or alias back, pass a mapping that already contains <<: *name (or an alias node) to Mapping::set. set does not infer a merge from a resolved object.

§Examples
use yaml_edit::Document;
use yaml_edit::anchor_resolution::DocumentResolvedExt;
use std::str::FromStr;

let yaml = r#"
config: &cfg
  port: 8080
server: *cfg
"#;
let doc = Document::from_str(yaml).unwrap();

// Get with resolution - expands the *cfg alias
if let Some(resolved_value) = doc.get_resolved("server").unwrap() {
    if let Some(server) = resolved_value.as_mapping() {
        if let Some(port) = server.get("port") {
            assert_eq!(port.to_i64(), Some(8080));
        }
    }
}
Source

fn build_anchor_registry(&self) -> AnchorRegistry

Build the anchor registry for this document

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§