Extract common parts

This commit is contained in:
Joel Wachsler 2022-07-12 14:48:41 +00:00
parent 9cafd434b2
commit f5a06eb003

View File

@ -24,17 +24,19 @@ pub fn create_return_type(
) )
}; };
let enum_types_with_names = let enum_types_with_names = return_type
return_type .parameters
.parameters .iter()
.iter() .flat_map(|parameter| match &parameter.return_type {
.flat_map(|parameter| match &parameter.return_type { types::Type::Number(types::TypeInfo {
types::Type::Number(types::TypeInfo { ref name,
ref name, type_description: Some(type_description),
type_description: Some(type_description), ..
.. }) => {
}) => { let enum_fields: Vec<proc_macro2::TokenStream> = type_description
let enum_fields = type_description.values.iter().map(|value| { .values
.iter()
.map(|value| {
let v = &value.value; let v = &value.value;
let re = Regex::new(r#"\(.*\)"#).unwrap(); let re = Regex::new(r#"\(.*\)"#).unwrap();
let desc = &value let desc = &value
@ -52,27 +54,20 @@ pub fn create_return_type(
#ident #ident
}, },
) )
}); })
.collect();
let enum_name = util::to_ident(&to_enum_name(name)); Some((name, enum_fields))
}
Some(( types::Type::String(types::TypeInfo {
name, ref name,
quote! { type_description: Some(type_description),
#[allow(clippy::enum_variant_names)] ..
#[derive(Debug, Deserialize, PartialEq, Eq)] }) => {
pub enum #enum_name { let enum_fields: Vec<proc_macro2::TokenStream> = type_description
#(#enum_fields,)* .values
} .iter()
}, .map(|type_description| {
))
}
types::Type::String(types::TypeInfo {
ref name,
type_description: Some(type_description),
..
}) => {
let enum_fields = type_description.values.iter().map(|type_description| {
let value = &type_description.value; let value = &type_description.value;
let value_as_ident = util::to_ident(&value.to_camel()); let value_as_ident = util::to_ident(&value.to_camel());
@ -83,23 +78,27 @@ pub fn create_return_type(
#value_as_ident #value_as_ident
}, },
) )
}); })
.collect();
let enum_name = util::to_ident(&to_enum_name(name)); Some((name, enum_fields))
}
_ => None,
})
.flat_map(|(name, enum_fields)| {
let enum_name = util::to_ident(&to_enum_name(name));
Some(( Some((
name, name,
quote! { quote! {
#[allow(clippy::enum_variant_names)] #[allow(clippy::enum_variant_names)]
#[derive(Debug, Deserialize, PartialEq, Eq)] #[derive(Debug, Deserialize, PartialEq, Eq)]
pub enum #enum_name { pub enum #enum_name {
#(#enum_fields,)* #(#enum_fields,)*
} }
}, },
)) ))
} });
_ => None,
});
let enum_names: HashMap<&String, String> = enum_types_with_names let enum_names: HashMap<&String, String> = enum_types_with_names
.clone() .clone()