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() {
|
let inner_builder = match child.kind() {
|
||||||
EnumRuleChildKind::Node(node_child) => {
|
EnumRuleChildKind::Node(node_child) => {
|
||||||
let inner_build_fn_ident =
|
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) }
|
quote! { #inner_build_fn_ident(inner_pair) }
|
||||||
}
|
}
|
||||||
_ => {
|
EnumRuleChildKind::Int(name_and_with) => {
|
||||||
let inner_build_fn_ident =
|
let inner_build_fn_ident = format_ident!("{}", name_and_with.with());
|
||||||
format_ident!("{}", make_build_fn_name(enum_rule.rule()));
|
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) }
|
quote! { #inner_build_fn_ident(inner_pair) }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,17 +1,20 @@
|
|||||||
use crate::deserialize::util::{get_as_bool_or, unwrap_single_member_hash};
|
use convert_case::{Case, Casing};
|
||||||
use crate::spec::tree_enum_spec::{
|
use crate::deserialize::util::{get_as_bool_or, make_build_fn_name, unwrap_single_member_hash};
|
||||||
EnumRuleChild, EnumRuleChildKind, TreeEnumBuildSpec, TreeEnumRule,
|
use crate::spec::tree_enum_spec::{EnumRuleChild, EnumRuleChildKind, EnumRuleChildNodeKind, NameAndWith, TreeEnumBuildSpec, TreeEnumRule};
|
||||||
};
|
|
||||||
use yaml_rust2::Yaml;
|
use yaml_rust2::Yaml;
|
||||||
|
|
||||||
fn deserialize_hash_enum_rule(rule: &str, props: &Yaml) -> TreeEnumRule {
|
fn deserialize_hash_enum_rule(rule: &str, props: &Yaml) -> TreeEnumRule {
|
||||||
if get_as_bool_or(&props["child"], true) {
|
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() {
|
let kind = match props["kind"].as_str().unwrap() {
|
||||||
"int" => EnumRuleChildKind::Int,
|
"int" => EnumRuleChildKind::Int(name_and_with),
|
||||||
"long" => EnumRuleChildKind::Long,
|
"long" => EnumRuleChildKind::Long(name_and_with),
|
||||||
"double" => EnumRuleChildKind::Double,
|
"double" => EnumRuleChildKind::Double(name_and_with),
|
||||||
"string" => EnumRuleChildKind::String,
|
"string" => EnumRuleChildKind::String(name_and_with),
|
||||||
"boolean" => EnumRuleChildKind::Boolean,
|
"boolean" => EnumRuleChildKind::Boolean(name_and_with),
|
||||||
_ => panic!("Invalid kind: {}", props["kind"].as_str().unwrap()),
|
_ => panic!("Invalid kind: {}", props["kind"].as_str().unwrap()),
|
||||||
};
|
};
|
||||||
TreeEnumRule::new(rule, Some(Box::new(EnumRuleChild::new(Box::new(kind)))))
|
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(
|
TreeEnumRule::new(
|
||||||
rule,
|
rule,
|
||||||
Some(Box::new(EnumRuleChild::new(Box::new(
|
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 {
|
pub enum EnumRuleChildKind {
|
||||||
Node,
|
Node(EnumRuleChildNodeKind),
|
||||||
Int,
|
Int(NameAndWith),
|
||||||
Long,
|
Long(NameAndWith),
|
||||||
Double,
|
Double(NameAndWith),
|
||||||
String,
|
String(NameAndWith),
|
||||||
Boolean,
|
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