Skip to main content

parse_document_with_config

Function parse_document_with_config 

Source
pub fn parse_document_with_config(
    input: &str,
    config: &ParserConfig,
) -> Result<Document>
Expand description

Parse one YAML document into an editable Document under config, mirroring crate::from_str_with_config.

The document keeps the configuration: every later re-parse of its source — the typed view behind Document::as_value, Document::validate, the safety-net re-parse an edit falls back to, and the value guard on comment edits — runs under the same limits. A document that only opens with a relaxed budget therefore stays readable and editable after its first edit instead of tripping the default limits on the next read.

Every knob of ParserConfig applies: the resource budgets, the alias-to-anchor ratio heuristic, the duplicate-key and merge-key policies, the schema toggles, and any policies.

§Errors

Returns the same parse errors as crate::from_str_with_config under the same configuration, including rejection of a stream with more than one document. Use parse_stream_with_config for streams.

§Examples

A values file that reuses each anchored default block more than ten times trips the default alias-to-anchor ratio heuristic (Some(10.0)) even though it is ordinary, valid YAML. Disable the heuristic and keep every absolute budget:

use noyalib::cst::{parse_document, parse_document_with_config};
use noyalib::{BudgetBreach, Error, ParserConfig};

let mut src = String::from("base: &b\n  k: v\ntenants:\n");
for i in 0..11 {
    src.push_str(&format!("  t{i}:\n    <<: *b\n"));
}

assert!(matches!(
    parse_document(&src),
    Err(Error::Budget(BudgetBreach::AliasAnchorRatio { .. }))
));

let cfg = ParserConfig::new().alias_anchor_ratio(None);
let doc = parse_document_with_config(&src, &cfg).unwrap();
assert_eq!(doc.to_string(), src);
assert_eq!(doc.as_value()["tenants"]["t10"]["k"].as_str(), Some("v"));