From f5a06eb0039c9b9c218341f5d1395a068b076865 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 14:48:41 +0000 Subject: [PATCH] Extract common parts --- .../src/generate/group/method/return_type.rs | 91 +++++++++---------- 1 file changed, 45 insertions(+), 46 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 da8c2c7..3c7b469 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 @@ -24,17 +24,19 @@ pub fn create_return_type( ) }; - let enum_types_with_names = - return_type - .parameters - .iter() - .flat_map(|parameter| match ¶meter.return_type { - types::Type::Number(types::TypeInfo { - ref name, - type_description: Some(type_description), - .. - }) => { - let enum_fields = type_description.values.iter().map(|value| { + let enum_types_with_names = return_type + .parameters + .iter() + .flat_map(|parameter| match ¶meter.return_type { + types::Type::Number(types::TypeInfo { + ref name, + type_description: Some(type_description), + .. + }) => { + let enum_fields: Vec = type_description + .values + .iter() + .map(|value| { let v = &value.value; let re = Regex::new(r#"\(.*\)"#).unwrap(); let desc = &value @@ -52,27 +54,20 @@ pub fn create_return_type( #ident }, ) - }); + }) + .collect(); - let enum_name = util::to_ident(&to_enum_name(name)); - - Some(( - name, - quote! { - #[allow(clippy::enum_variant_names)] - #[derive(Debug, Deserialize, PartialEq, Eq)] - pub enum #enum_name { - #(#enum_fields,)* - } - }, - )) - } - types::Type::String(types::TypeInfo { - ref name, - type_description: Some(type_description), - .. - }) => { - let enum_fields = type_description.values.iter().map(|type_description| { + Some((name, enum_fields)) + } + types::Type::String(types::TypeInfo { + ref name, + type_description: Some(type_description), + .. + }) => { + 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()); @@ -83,23 +78,27 @@ pub fn create_return_type( #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(( - name, - quote! { - #[allow(clippy::enum_variant_names)] - #[derive(Debug, Deserialize, PartialEq, Eq)] - pub enum #enum_name { - #(#enum_fields,)* - } - }, - )) - } - _ => None, - }); + Some(( + name, + quote! { + #[allow(clippy::enum_variant_names)] + #[derive(Debug, Deserialize, PartialEq, Eq)] + pub enum #enum_name { + #(#enum_fields,)* + } + }, + )) + }); let enum_names: HashMap<&String, String> = enum_types_with_names .clone()