Add or else (default) to yaml spec.

This commit is contained in:
Jesse Brault 2025-09-04 10:21:30 -05:00
parent 9f3f3e0f0d
commit 0adb4bbe0e
4 changed files with 38 additions and 15 deletions

View File

@ -70,11 +70,12 @@ fn get_single_child_to_build(name: &str, rule: &str, optional: bool, build: &Yam
}
}
fn get_single_child(name: &str, rule: &str, optional: bool, build: &Yaml) -> ChildSpec {
fn get_single_child(name: &str, rule: &str, optional: bool, or_else: Option<String>, build: &Yaml) -> ChildSpec {
ChildSpec::SingleChild(SingleChild::new(
name,
rule,
get_single_child_to_build(name, rule, optional, build),
or_else
))
}
@ -109,8 +110,21 @@ fn get_child_specs(children: &Yaml) -> Vec<ChildSpec> {
let optional = props["optional"]
.as_bool()
.unwrap_or_else(|| false);
let or_else = props["or_else"]
.as_str()
.map(|s| s.to_string())
.or_else(|| {
let or_else_default = props["or_else_default"]
.as_bool()
.unwrap_or_else(|| false);
if or_else_default {
Some(String::from("default"))
} else {
None
}
});
get_single_child(name, &rule, optional, build)
get_single_child(name, &rule, optional, or_else, build)
}
} 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,
or_else: Option<String>
}
impl SingleChild {
@ -235,14 +236,16 @@ impl SingleChild {
&name.to_case(Case::Pascal),
false,
)),
or_else: None,
}
}
pub fn new(name: &str, rule: &str, build: SingleChildToBuild) -> Self {
pub fn new(name: &str, rule: &str, build: SingleChildToBuild, or_else: Option<String>) -> Self {
Self {
name: name.to_string(),
rule: rule.to_string(),
build,
or_else,
}
}

View File

@ -82,6 +82,12 @@ $defs:
optional:
type: boolean
description: If true, this child will be stored as an Option.
or_else:
type: string
description: The method name to call upon the built-type if the rule is not found. Takes precedence over "or_else_default".
or_else_default:
type: boolean
description: Whether to call the default method on the built-type if the rule is not found.
build:
oneOf:
- type: string

View File

@ -112,9 +112,9 @@ Interface:
skip: true
- identifier
- generic_parameters:
optional: true
or_else_default: true
- implements_list:
optional: true
or_else_default: true
- declarations:
rule: InterfaceLevelDeclaration
vec: true
@ -133,11 +133,11 @@ Class:
skip: true
- identifier
- generic_parameters:
optional: true
or_else_default: true
- class_constructor:
optional: true
or_else_default: true
- implements_list:
optional: true
or_else_default: true
- class_level_declarations:
rule: ClassLevelDeclaration
vec: true
@ -155,17 +155,17 @@ Function:
skip: true
- generics:
rule: GenericParameters
optional: true
or_else_default: true
- identifier
- parameters
- return_type:
optional: true
or_else: void
- function_body
OperatorFunction:
children:
- is_public:
rule: Pub
build:
build:
type: boolean
on: rule_present
- op_kw:
@ -173,17 +173,17 @@ OperatorFunction:
skip: true
- generics:
rule: GenericParameters
optional: true
or_else_default: true
- operator
- parameters
- return_type:
optional: true
or_else: void
- function_body
PlatformFunction:
children:
- is_public:
rule: Pub
build:
build:
type: boolean
on: rule_present
- platform_kw:
@ -194,7 +194,7 @@ PlatformFunction:
skip: true
- generics:
rule: GenericParameters
optional: true
or_else_default: true
- identifier
- parameters
- return_type