pub fn validate_tree(node: &SyntaxNode<Lang>) -> Result<(), String>Expand description
Validates that the CST follows expected structural invariants.
This is useful for testing that mutations don’t break tree structure.
Returns Ok(()) if the tree is valid, or an error message if issues are found.
Current checks:
- Every MAPPING_ENTRY has exactly one KEY
- Every MAPPING_ENTRY has exactly one COLON (zero allowed for explicit-key entries with implicit-null values)
- Every MAPPING_ENTRY has at most one VALUE
- Block-style MAPPING_ENTRY and SEQUENCE_ENTRY nodes are terminated by a NEWLINE – either as their own last token, or via a trailing comment/newline pair at the parent level, or (for MAPPING_ENTRYs) an implicit-null value
- A block MAPPING or SEQUENCE starts with its first entry: the comment and blank lines before it belong to the parent
- No two adjacent INDENT tokens (would render as doubled indentation)
- No two adjacent NEWLINE tokens at the tail of a MAPPING_ENTRY (would render as a stray blank line)
§Example
use yaml_edit::{YamlFile, debug};
use rowan::ast::AstNode;
use std::str::FromStr;
let yaml = YamlFile::from_str("name: Alice\n").unwrap();
debug::validate_tree(yaml.syntax()).expect("Tree should be valid");