From 5f120f39fc1b8d597fef5c04860f8f261cadbb77 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 15:34:27 +0000 Subject: [PATCH] Extract common parts --- .../src/generate/group/method/return_type.rs | 85 +++++++++---------- 1 file changed, 41 insertions(+), 44 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 3a4ef53..c6264f6 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 @@ -32,32 +32,16 @@ pub fn create_return_type( ref name, type_description: Some(type_description), .. - }) => { - let enum_fields: Vec = type_description - .values - .iter() - .map(create_number_enum_value) - .collect(); - - Some((name, enum_fields)) - } + }) => create_enum_field_value(type_description, name, create_number_enum_value), types::Type::String(types::TypeInfo { ref name, type_description: Some(type_description), .. - }) => { - let enum_fields: Vec = type_description - .values - .iter() - .map(create_string_enum_value) - .collect(); - - Some((name, enum_fields)) - } + }) => create_enum_field_value(type_description, name, create_string_enum_value), _ => None, }) .flat_map(|(name, enum_fields)| { - let enum_name = util::to_ident(&to_enum_name(name)); + let enum_name = util::to_ident(&to_enum_name(&name)); Some(( name, @@ -71,45 +55,39 @@ pub fn create_return_type( )) }); - let enum_names: HashMap<&String, String> = enum_types_with_names + let enum_names: HashMap = enum_types_with_names .clone() - .map(|(enum_name, _)| (enum_name, to_enum_name(enum_name))) + .map(|(enum_name, _)| (enum_name.clone(), to_enum_name(&enum_name))) .collect(); let enum_types = enum_types_with_names.map(|(_, enum_type)| enum_type); - let parameters = return_type.parameters.iter().map(|parameter| { + let builder_fields = return_type.parameters.iter().map(|parameter| { let namestr = ¶meter.name; let name = util::to_ident(&namestr.to_snake().replace("__", "_")); - let rtype = if let Some(enum_type) = enum_names.get(namestr) { - util::to_ident(enum_type) - } else { - util::to_ident(¶meter.return_type.to_owned_type()) + let enum_name = match enum_names.get(namestr) { + Some(enum_type) => enum_type.to_owned(), + None => parameter.return_type.to_owned_type(), }; - let type_info = parameter.return_type.get_type_info(); - - let rtype_as_quote = if type_info.is_list { - quote! { - std::vec::Vec<#rtype> - } + let rtype = util::to_ident(&enum_name); + let rtype_as_quote = if parameter.return_type.get_type_info().is_list { + quote! { std::vec::Vec<#rtype> } } else { + quote! { #rtype } + }; + + let generate_field = |field_name| { quote! { - #rtype + #[serde(rename = #namestr)] + pub #field_name: #rtype_as_quote } }; - // "type" is a reserved keyword in Rust, so we use a different name. + // "type" is a reserved keyword in Rust, so we just add "t_" to it. if namestr == "type" { - let non_reserved_name = format_ident!("t_{}", name); - quote! { - #[serde(rename = #namestr)] - pub #non_reserved_name: #rtype_as_quote - } + generate_field(format_ident!("t_{}", name)) } else { - quote! { - #[serde(rename = #namestr)] - pub #name: #rtype_as_quote - } + generate_field(name) } }); @@ -134,7 +112,7 @@ pub fn create_return_type( quote! { #[derive(Debug, Deserialize)] pub struct #return_type_name { - #(#parameters,)* + #(#builder_fields,)* } #(#enum_types)* @@ -142,6 +120,25 @@ pub fn create_return_type( )) } +fn create_enum_field_value( + type_description: &types::TypeDescription, + name: &str, + f: F, +) -> Option<(String, Vec)> +where + F: Fn(&types::TypeDescriptions) -> proc_macro2::TokenStream, +{ + let enum_fields: Vec = type_description + .values + .iter() + .map(f) + .collect::>(); + + let nn = name.to_string(); + + Some((nn, enum_fields)) +} + fn create_string_enum_value( type_description: &types::TypeDescriptions, ) -> proc_macro2::TokenStream {