Refactor tree_enum deserialize and build fn.
This commit is contained in:
parent
2d8843b80d
commit
5842304f0b
@ -16,12 +16,27 @@ pub fn make_enum_build_fn(enum_build_spec: &TreeEnumBuildSpec) -> TokenStream {
|
||||
let inner_builder = match child.kind() {
|
||||
EnumRuleChildKind::Node(node_child) => {
|
||||
let inner_build_fn_ident =
|
||||
format_ident!("{}", make_build_fn_name(node_child.build()));
|
||||
format_ident!("{}", node_child.with());
|
||||
quote! { #inner_build_fn_ident(inner_pair) }
|
||||
}
|
||||
_ => {
|
||||
let inner_build_fn_ident =
|
||||
format_ident!("{}", make_build_fn_name(enum_rule.rule()));
|
||||
EnumRuleChildKind::Int(name_and_with) => {
|
||||
let inner_build_fn_ident = format_ident!("{}", name_and_with.with());
|
||||
quote! { #inner_build_fn_ident(inner_pair) }
|
||||
}
|
||||
EnumRuleChildKind::Long(name_and_with) => {
|
||||
let inner_build_fn_ident = format_ident!("{}", name_and_with.with());
|
||||
quote! { #inner_build_fn_ident(inner_pair) }
|
||||
}
|
||||
EnumRuleChildKind::Double(name_and_with) => {
|
||||
let inner_build_fn_ident = format_ident!("{}", name_and_with.with());
|
||||
quote! { #inner_build_fn_ident(inner_pair) }
|
||||
}
|
||||
EnumRuleChildKind::String(name_and_with) => {
|
||||
let inner_build_fn_ident = format_ident!("{}", name_and_with.with());
|
||||
quote! { #inner_build_fn_ident(inner_pair) }
|
||||
}
|
||||
EnumRuleChildKind::Boolean(name_and_with) => {
|
||||
let inner_build_fn_ident = format_ident!("{}", name_and_with.with());
|
||||
quote! { #inner_build_fn_ident(inner_pair) }
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,17 +1,20 @@
|
||||
use crate::deserialize::util::{get_as_bool_or, unwrap_single_member_hash};
|
||||
use crate::spec::tree_enum_spec::{
|
||||
EnumRuleChild, EnumRuleChildKind, TreeEnumBuildSpec, TreeEnumRule,
|
||||
};
|
||||
use convert_case::{Case, Casing};
|
||||
use crate::deserialize::util::{get_as_bool_or, make_build_fn_name, unwrap_single_member_hash};
|
||||
use crate::spec::tree_enum_spec::{EnumRuleChild, EnumRuleChildKind, EnumRuleChildNodeKind, NameAndWith, TreeEnumBuildSpec, TreeEnumRule};
|
||||
use yaml_rust2::Yaml;
|
||||
|
||||
fn deserialize_hash_enum_rule(rule: &str, props: &Yaml) -> TreeEnumRule {
|
||||
if get_as_bool_or(&props["child"], true) {
|
||||
let name_and_with = NameAndWith::new(
|
||||
&rule.to_case(Case::Snake),
|
||||
&make_build_fn_name(rule)
|
||||
);
|
||||
let kind = match props["kind"].as_str().unwrap() {
|
||||
"int" => EnumRuleChildKind::Int,
|
||||
"long" => EnumRuleChildKind::Long,
|
||||
"double" => EnumRuleChildKind::Double,
|
||||
"string" => EnumRuleChildKind::String,
|
||||
"boolean" => EnumRuleChildKind::Boolean,
|
||||
"int" => EnumRuleChildKind::Int(name_and_with),
|
||||
"long" => EnumRuleChildKind::Long(name_and_with),
|
||||
"double" => EnumRuleChildKind::Double(name_and_with),
|
||||
"string" => EnumRuleChildKind::String(name_and_with),
|
||||
"boolean" => EnumRuleChildKind::Boolean(name_and_with),
|
||||
_ => panic!("Invalid kind: {}", props["kind"].as_str().unwrap()),
|
||||
};
|
||||
TreeEnumRule::new(rule, Some(Box::new(EnumRuleChild::new(Box::new(kind)))))
|
||||
@ -25,7 +28,10 @@ fn deserialize_string_enum_rule(rule: &str) -> TreeEnumRule {
|
||||
TreeEnumRule::new(
|
||||
rule,
|
||||
Some(Box::new(EnumRuleChild::new(Box::new(
|
||||
EnumRuleChildKind::Node,
|
||||
EnumRuleChildKind::Node(EnumRuleChildNodeKind::new(
|
||||
rule,
|
||||
&make_build_fn_name(rule)
|
||||
)),
|
||||
)))),
|
||||
)
|
||||
}
|
||||
|
||||
@ -64,10 +64,54 @@ impl EnumRuleChild {
|
||||
}
|
||||
|
||||
pub enum EnumRuleChildKind {
|
||||
Node,
|
||||
Int,
|
||||
Long,
|
||||
Double,
|
||||
String,
|
||||
Boolean,
|
||||
Node(EnumRuleChildNodeKind),
|
||||
Int(NameAndWith),
|
||||
Long(NameAndWith),
|
||||
Double(NameAndWith),
|
||||
String(NameAndWith),
|
||||
Boolean(NameAndWith),
|
||||
}
|
||||
|
||||
pub struct EnumRuleChildNodeKind {
|
||||
node_kind: String,
|
||||
with: String,
|
||||
}
|
||||
|
||||
impl EnumRuleChildNodeKind {
|
||||
pub fn new(node_kind: &str, with: &str) -> Self {
|
||||
Self {
|
||||
node_kind: node_kind.to_string(),
|
||||
with: with.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn node_kind(&self) -> &str {
|
||||
&self.node_kind
|
||||
}
|
||||
|
||||
pub fn with(&self) -> &str {
|
||||
&self.with
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NameAndWith {
|
||||
name: String,
|
||||
with: String,
|
||||
}
|
||||
|
||||
impl NameAndWith {
|
||||
pub fn new(name: &str, with: &str) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
with: with.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn with(&self) -> &str {
|
||||
&self.with
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user