This commit is contained in:
Joel Wachsler 2022-07-12 15:01:00 +00:00
parent f5a06eb003
commit 44eb0bcc57

View File

@ -36,25 +36,7 @@ pub fn create_return_type(
let enum_fields: Vec<proc_macro2::TokenStream> = type_description let enum_fields: Vec<proc_macro2::TokenStream> = type_description
.values .values
.iter() .iter()
.map(|value| { .map(create_number_enum_value)
let v = &value.value;
let re = Regex::new(r#"\(.*\)"#).unwrap();
let desc = &value
.description
.replace(' ', "_")
.replace('-', "_")
.replace(',', "_");
let desc_without_parentheses = re.replace_all(desc, "");
let ident = util::to_ident(&desc_without_parentheses.to_camel());
util::add_docs(
&Some(value.description.clone()),
quote! {
#[serde(rename = #v)]
#ident
},
)
})
.collect(); .collect();
Some((name, enum_fields)) Some((name, enum_fields))
@ -67,18 +49,7 @@ pub fn create_return_type(
let enum_fields: Vec<proc_macro2::TokenStream> = type_description let enum_fields: Vec<proc_macro2::TokenStream> = type_description
.values .values
.iter() .iter()
.map(|type_description| { .map(create_string_enum_value)
let value = &type_description.value;
let value_as_ident = util::to_ident(&value.to_camel());
util::add_docs(
&Some(type_description.description.clone()),
quote! {
#[serde(rename = #value)]
#value_as_ident
},
)
})
.collect(); .collect();
Some((name, enum_fields)) Some((name, enum_fields))
@ -170,3 +141,39 @@ pub fn create_return_type(
}, },
)) ))
} }
fn create_string_enum_value(
type_description: &types::TypeDescriptions,
) -> proc_macro2::TokenStream {
let value = &type_description.value;
let value_as_ident = util::to_ident(&value.to_camel());
create_enum_field(&value_as_ident, value, &type_description.description)
}
fn create_number_enum_value(value: &types::TypeDescriptions) -> proc_macro2::TokenStream {
let v = &value.value;
let re = Regex::new(r#"\(.*\)"#).unwrap();
let desc = &value
.description
.replace(' ', "_")
.replace('-', "_")
.replace(',', "_");
let desc_without_parentheses = re.replace_all(desc, "");
let ident = util::to_ident(&desc_without_parentheses.to_camel());
create_enum_field(&ident, v, &value.description)
}
fn create_enum_field(
ident: &syn::Ident,
rename: &str,
description: &str,
) -> proc_macro2::TokenStream {
util::add_docs(
&Some(description.to_string()),
quote! {
#[serde(rename = #rename)]
#ident
},
)
}