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( ChildSpec::SingleChild(SingleChild::new(
name, name,
rule, rule,
get_single_child_to_build(name, rule, optional, build), 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"] let optional = props["optional"]
.as_bool() .as_bool()
.unwrap_or_else(|| false); .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 { } 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,
or_else: Option<String>
} }
impl SingleChild { impl SingleChild {
@ -235,14 +236,16 @@ impl SingleChild {
&name.to_case(Case::Pascal), &name.to_case(Case::Pascal),
false, 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 { Self {
name: name.to_string(), name: name.to_string(),
rule: rule.to_string(), rule: rule.to_string(),
build, build,
or_else,
} }
} }

View File

@ -82,6 +82,12 @@ $defs:
optional: optional:
type: boolean type: boolean
description: If true, this child will be stored as an Option. 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: build:
oneOf: oneOf:
- type: string - type: string

View File

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