From 72074f60c1cc650a8cd8c4df11a0ebd6d7078e02 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Thu, 14 Jul 2022 22:53:41 +0000 Subject: [PATCH] Extract builder generation --- .../group/method/method_with_params.rs | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) 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 d9003dd..a41e3c1 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 @@ -6,7 +6,7 @@ use quote::quote; use crate::{ generate::util, - parser::{self, ApiParameters}, + parser::{self, ApiMethod, ApiParameters}, types, }; @@ -27,8 +27,6 @@ pub fn create_method_with_params( let parameters = Parameters::new(params); - let mandatory_param_args = parameters.mandatory.generate_params(); - let group_name = util::to_ident(&group.name.to_camel()); let send_builder = SendMethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }) @@ -49,16 +47,7 @@ pub fn create_method_with_params( None => send_impl_generator.generate(send_builder), }; - let mandatory_param_names = parameters.mandatory.names(); - - 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),*) - } - }, - ); + let builder = generate_builder(¶meters, method, method_name, ¶m_type); let group_impl = quote! { pub struct #param_type<'a> { @@ -72,6 +61,25 @@ pub fn create_method_with_params( (builder, Some(group_impl)) } +fn generate_builder( + parameters: &Parameters, + method: &ApiMethod, + method_name: &proc_macro2::Ident, + param_type: &proc_macro2::Ident, +) -> proc_macro2::TokenStream { + let mandatory_param_names = parameters.mandatory.names(); + let mandatory_param_args = parameters.mandatory.generate_params(); + + util::add_docs( + &method.description, + quote! { + pub fn #method_name(&self, #(#mandatory_param_args),*) -> #param_type { + #param_type::new(self, #(#mandatory_param_names),*) + } + }, + ) +} + #[derive(Debug)] struct SendImplGenerator<'a> { group_name: &'a proc_macro2::Ident,