From 9cafd434b2442c4e4cb1422ad5b08cd0ec9539a3 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 14:32:49 +0000 Subject: [PATCH] Extract more methods --- .../group/method/method_with_params.rs | 89 +++++++++++-------- .../group/method/method_without_params.rs | 4 +- .../src/generate/group/method/mod.rs | 2 +- ...thod_builder.rs => send_method_builder.rs} | 4 +- 4 files changed, 59 insertions(+), 40 deletions(-) rename qbittorrent-web-api-gen/src/generate/group/method/{method_builder.rs => send_method_builder.rs} (97%) diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs index 1bb2aad..0700ba4 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs @@ -3,7 +3,7 @@ use quote::quote; use crate::{generate::util, parser, types}; -use super::{method_builder::MethodBuilder, return_type::create_return_type}; +use super::{send_method_builder::SendMethodBuilder, return_type::create_return_type}; pub fn create_method_with_params( group: &parser::ApiGroup, @@ -12,40 +12,30 @@ pub fn create_method_with_params( method_name: &proc_macro2::Ident, url: &str, ) -> (proc_macro2::TokenStream, Option) { - let parameter_type = util::to_ident(&format!( + let param_type = util::to_ident(&format!( "{}{}Parameters", group.name.to_camel(), method.name.to_camel() )); let mandatory_params = mandatory_params(params); - let mandatory_param_args = mandatory_params - .iter() - .map(|param| param_with_name(param)) - .collect::>(); + let mandatory_param_args = generate_mandatory_params(&mandatory_params); let mandatory_param_names = mandatory_params.iter().map(|param| { let (name, ..) = param_name(param); quote! { #name } }); - let mandatory_param_form_build = mandatory_params.iter().map(|param| { - let (name, name_as_str) = param_name(param); - quote! { let form = form.text(#name_as_str, #name.to_string()); } - }); - - let optional_params = params - .iter() - .filter(|param| param.get_type_info().is_optional) - .map(generate_optional_parameter); - let group_name = util::to_ident(&group.name.to_camel()); let send_builder = - MethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }).with_form(); + SendMethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }).with_form(); let generate_send_impl = |send_method: proc_macro2::TokenStream| { + let optional_params = generate_optional_params(params); + let mandatory_param_form_build = generate_mandatory_param_builder(&mandatory_params); + quote! { - impl<'a> #parameter_type<'a> { + impl<'a> #param_type<'a> { fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self { let form = reqwest::multipart::Form::new(); #(#mandatory_param_form_build)* @@ -70,24 +60,52 @@ pub fn create_method_with_params( None => generate_send_impl(send_builder.build()), }; - ( - util::add_docs( - &method.description, - quote! { - pub fn #method_name(&self, #(#mandatory_param_args),*) -> #parameter_type { - #parameter_type::new(self, #(#mandatory_param_names),*) - } - }, - ), - Some(quote! { - pub struct #parameter_type<'a> { - group: &'a #group_name<'a>, - form: reqwest::multipart::Form, + let builder = util::add_docs( + &method.description, + quote! { + pub fn #method_name(&self, #(#mandatory_param_args),*) -> #param_type { + #param_type::new(self, #(#mandatory_param_names),*) } + }, + ); - #send - }), - ) + let group_impl = quote! { + pub struct #param_type<'a> { + group: &'a #group_name<'a>, + form: reqwest::multipart::Form, + } + + #send + }; + + (builder, Some(group_impl)) +} + +fn generate_mandatory_params(mandatory_params: &[&types::Type]) -> Vec { + mandatory_params + .iter() + .map(|param| param_with_name(param)) + .collect() +} + +fn generate_mandatory_param_builder( + mandatory_params: &[&types::Type], +) -> Vec { + mandatory_params + .iter() + .map(|param| { + let (name, name_as_str) = param_name(param); + quote! { let form = form.text(#name_as_str, #name.to_string()); } + }) + .collect() +} + +fn generate_optional_params(params: &[types::Type]) -> Vec { + params + .iter() + .filter(|param| param.get_type_info().is_optional) + .map(generate_optional_param) + .collect() } fn mandatory_params(params: &[types::Type]) -> Vec<&types::Type> { @@ -97,7 +115,7 @@ fn mandatory_params(params: &[types::Type]) -> Vec<&types::Type> { .collect() } -fn generate_optional_parameter(param: &types::Type) -> proc_macro2::TokenStream { +fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream { let n = ¶m.get_type_info().name; let name = util::to_ident(&n.to_snake()); let t = util::to_ident(¶m.to_borrowed_type()); @@ -106,6 +124,7 @@ fn generate_optional_parameter(param: &types::Type) -> proc_macro2::TokenStream } else { quote! { #t } }; + util::add_docs( ¶m.get_type_info().description, quote! { diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs index 0b74ac1..0a53c41 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs @@ -1,6 +1,6 @@ use quote::quote; -use super::{method_builder::MethodBuilder, return_type::create_return_type}; +use super::{send_method_builder::SendMethodBuilder, return_type::create_return_type}; use crate::parser; pub fn create_method_without_params( @@ -9,7 +9,7 @@ pub fn create_method_without_params( method_name: proc_macro2::Ident, url: &str, ) -> (proc_macro2::TokenStream, Option) { - let builder = MethodBuilder::new(&method_name, url, quote! { self.auth }) + let builder = SendMethodBuilder::new(&method_name, url, quote! { self.auth }) .description(&method.description); match create_return_type(group, method) { diff --git a/qbittorrent-web-api-gen/src/generate/group/method/mod.rs b/qbittorrent-web-api-gen/src/generate/group/method/mod.rs index 051e5e5..f1948b9 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/mod.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/mod.rs @@ -1,4 +1,4 @@ -mod method_builder; +mod send_method_builder; mod method_with_params; mod method_without_params; mod return_type; diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs b/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs similarity index 97% rename from qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs rename to qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs index be895f1..da0c44a 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs @@ -2,7 +2,7 @@ use quote::quote; use crate::generate::util; -pub struct MethodBuilder { +pub struct SendMethodBuilder { method_name: syn::Ident, url: String, auth_module_path: proc_macro2::TokenStream, @@ -11,7 +11,7 @@ pub struct MethodBuilder { form: bool, } -impl MethodBuilder { +impl SendMethodBuilder { pub fn new( method_name: &syn::Ident, url: &str,