From 44eb0bcc578fe6a94d2019320605820239251fcd Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 15:01:00 +0000 Subject: [PATCH] Extract --- .../src/generate/group/method/return_type.rs | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/qbittorrent-web-api-gen/src/generate/group/method/return_type.rs b/qbittorrent-web-api-gen/src/generate/group/method/return_type.rs index 3c7b469..3a4ef53 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/return_type.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/return_type.rs @@ -36,25 +36,7 @@ pub fn create_return_type( let enum_fields: Vec = type_description .values .iter() - .map(|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 - }, - ) - }) + .map(create_number_enum_value) .collect(); Some((name, enum_fields)) @@ -67,18 +49,7 @@ pub fn create_return_type( let enum_fields: Vec = type_description .values .iter() - .map(|type_description| { - 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 - }, - ) - }) + .map(create_string_enum_value) .collect(); 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 + }, + ) +}