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(
name,
rule,
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))
.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"]) {
return ChildSpec::SkipChild(SkipChild::new(name, rule));
return ChildSpec::SkipChild(SkipChild::new(name, &rule));
}
let build = &props["build"];
if get_vec(&props["vec"]) {
get_vec_child(name, rule, build)
get_vec_child(name, &rule, build)
} 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 {
ChildSpec::SingleChild(SingleChild::from_name_snake(child_spec.as_str().unwrap()))

View File

@ -224,6 +224,7 @@ pub struct SingleChild {
name: String,
rule: String,
build: SingleChildToBuild,
optional: bool
}
impl SingleChild {
@ -234,14 +235,16 @@ impl SingleChild {
build: SingleChildToBuild::Type(SingleTypeChildToBuild::from_build_or_rule(
&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 {
name: name.to_string(),
rule: rule.to_string(),
build,
optional
}
}