Extract builder generation

This commit is contained in:
Joel Wachsler 2022-07-14 22:53:41 +00:00
parent 464c9b099d
commit 72074f60c1

View File

@ -6,7 +6,7 @@ use quote::quote;
use crate::{ use crate::{
generate::util, generate::util,
parser::{self, ApiParameters}, parser::{self, ApiMethod, ApiParameters},
types, types,
}; };
@ -27,8 +27,6 @@ pub fn create_method_with_params(
let parameters = Parameters::new(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 group_name = util::to_ident(&group.name.to_camel());
let send_builder = let send_builder =
SendMethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }) 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), None => send_impl_generator.generate(send_builder),
}; };
let mandatory_param_names = parameters.mandatory.names(); let builder = generate_builder(&parameters, method, method_name, &param_type);
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 group_impl = quote! { let group_impl = quote! {
pub struct #param_type<'a> { pub struct #param_type<'a> {
@ -72,6 +61,25 @@ pub fn create_method_with_params(
(builder, Some(group_impl)) (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)] #[derive(Debug)]
struct SendImplGenerator<'a> { struct SendImplGenerator<'a> {
group_name: &'a proc_macro2::Ident, group_name: &'a proc_macro2::Ident,