Add optional to ast schema and ast-gen.

This commit is contained in:
Jesse Brault 2025-09-03 16:45:09 -05:00
parent 4dcb5ee783
commit a53388155a
2 changed files with 17 additions and 6 deletions

View File

@ -70,11 +70,12 @@ fn get_single_child_to_build(name: &str, rule: &str, build: &Yaml) -> SingleChil
} }
} }
fn get_single_child(name: &str, rule: &str, build: &Yaml) -> ChildSpec { fn get_single_child(name: &str, rule: &str, build: &Yaml, optional: bool) -> ChildSpec {
ChildSpec::SingleChild(SingleChild::new( ChildSpec::SingleChild(SingleChild::new(
name, name,
rule, rule,
get_single_child_to_build(name, rule, build), get_single_child_to_build(name, rule, build),
optional
)) ))
} }
@ -92,18 +93,25 @@ fn get_child_specs(children: &Yaml) -> Vec<ChildSpec> {
.map(|(name, props)| (name.as_str().unwrap(), props)) .map(|(name, props)| (name.as_str().unwrap(), props))
.unwrap(); .unwrap();
let rule = props["rule"].as_str().unwrap(); let rule = props["rule"]
.as_str()
.map(|s| s.to_string())
.unwrap_or(name.to_case(Case::Pascal));
if get_skip(&props["skip"]) { if get_skip(&props["skip"]) {
return ChildSpec::SkipChild(SkipChild::new(name, rule)); return ChildSpec::SkipChild(SkipChild::new(name, &rule));
} }
let build = &props["build"]; let build = &props["build"];
if get_vec(&props["vec"]) { if get_vec(&props["vec"]) {
get_vec_child(name, rule, build) get_vec_child(name, &rule, build)
} else { } else {
get_single_child(name, rule, build) let optional = props["optional"]
.as_bool()
.unwrap_or_else(|| false);
get_single_child(name, &rule, build, optional)
} }
} else { } else {
ChildSpec::SingleChild(SingleChild::from_name_snake(child_spec.as_str().unwrap())) ChildSpec::SingleChild(SingleChild::from_name_snake(child_spec.as_str().unwrap()))

View File

@ -224,6 +224,7 @@ pub struct SingleChild {
name: String, name: String,
rule: String, rule: String,
build: SingleChildToBuild, build: SingleChildToBuild,
optional: bool
} }
impl SingleChild { impl SingleChild {
@ -234,14 +235,16 @@ impl SingleChild {
build: SingleChildToBuild::Type(SingleTypeChildToBuild::from_build_or_rule( build: SingleChildToBuild::Type(SingleTypeChildToBuild::from_build_or_rule(
&name.to_case(Case::Pascal), &name.to_case(Case::Pascal),
)), )),
optional: false,
} }
} }
pub fn new(name: &str, rule: &str, build: SingleChildToBuild) -> Self { pub fn new(name: &str, rule: &str, build: SingleChildToBuild, optional: bool) -> Self {
Self { Self {
name: name.to_string(), name: name.to_string(),
rule: rule.to_string(), rule: rule.to_string(),
build, build,
optional
} }
} }