Skip to content

Commit ddbc557

Browse files
committed
yeast-schema: Track leaf node kinds
The `rules!` macro needs this information to decide whether a bare output node should inherit the value of the matched input node.
1 parent 73c6b40 commit ddbc557

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎shared/yeast-schema/src/node_types_yaml.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ pub fn schema_from_yaml(yaml_input: &str) -> Result<crate::schema::Schema, Strin
469469
Ok(schema)
470470
}
471471

472+
/// Return the named node kinds that declare no fields.
473+
pub fn leaf_kinds_from_yaml(yaml_input: &str) -> Result<BTreeSet<String>, String> {
474+
let yaml: YamlNodeTypes =
475+
serde_yaml::from_str(yaml_input).map_err(|e| format!("Failed to parse YAML: {e}"))?;
476+
Ok(yaml
477+
.named
478+
.into_iter()
479+
.filter_map(|(name, fields)| {
480+
fields
481+
.as_ref()
482+
.map_or(true, BTreeMap::is_empty)
483+
.then_some(name)
484+
})
485+
.collect())
486+
}
487+
472488
// ---------------------------------------------------------------------------
473489
// JSON → YAML conversion
474490
// ---------------------------------------------------------------------------
@@ -810,6 +826,24 @@ unnamed:
810826
assert_eq!(end["named"], false);
811827
}
812828

829+
#[test]
830+
fn test_leaf_kinds_from_yaml() {
831+
let leaf_kinds = leaf_kinds_from_yaml(
832+
r#"
833+
named:
834+
identifier:
835+
empty_fields: {}
836+
call:
837+
callee: identifier
838+
"#,
839+
)
840+
.unwrap();
841+
842+
assert!(leaf_kinds.contains("identifier"));
843+
assert!(leaf_kinds.contains("empty_fields"));
844+
assert!(!leaf_kinds.contains("call"));
845+
}
846+
813847
#[test]
814848
fn test_explicit_unnamed_disambiguation() {
815849
let yaml = r#"

0 commit comments

Comments
 (0)