Refactor parameters

This commit is contained in:
Joel Wachsler 2022-07-14 21:17:27 +00:00
parent 250a1d098c
commit 9451c15cdf
2 changed files with 108 additions and 59 deletions

View File

@ -1,7 +1,13 @@
use std::rc::Rc;
use case::CaseExt; use case::CaseExt;
use quote::quote; use quote::quote;
use crate::{generate::util, parser, types}; use crate::{
generate::util,
parser::{self, ApiParameters},
types,
};
use super::{return_type::create_return_type, send_method_builder::SendMethodBuilder}; use super::{return_type::create_return_type, send_method_builder::SendMethodBuilder};
@ -18,12 +24,13 @@ pub fn create_method_with_params(
method.name.to_camel() method.name.to_camel()
)); ));
let mandatory_param_args = generate_mandatory_params(&params.mandatory); let rc_params = Rc::new(params);
let mandatory_param_names = params.mandatory.iter().map(|param| { let mandatory_params = MandatoryParams::new(&rc_params);
let (name, ..) = param_name(param); let optional_params = OptionalParams::new(&rc_params);
quote! { #name }
}); // let mandatory_param_args = generate_mandatory_params(&params.mandatory);
let mandatory_param_args = mandatory_params.generate_mandatory_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 =
@ -31,8 +38,8 @@ pub fn create_method_with_params(
.with_form(); .with_form();
let generate_send_impl = |send_method: proc_macro2::TokenStream| { let generate_send_impl = |send_method: proc_macro2::TokenStream| {
let optional_params = generate_optional_params(&params.optional); let optional_params = optional_params.generate_optional_params();
let mandatory_param_form_build = generate_mandatory_param_builder(&params.mandatory); let mandatory_param_form_build = mandatory_params.param_builder();
quote! { quote! {
impl<'a> #param_type<'a> { impl<'a> #param_type<'a> {
@ -60,6 +67,8 @@ pub fn create_method_with_params(
None => generate_send_impl(send_builder.build()), None => generate_send_impl(send_builder.build()),
}; };
let mandatory_param_names = mandatory_params.names();
let builder = util::add_docs( let builder = util::add_docs(
&method.description, &method.description,
quote! { quote! {
@ -81,24 +90,81 @@ pub fn create_method_with_params(
(builder, Some(group_impl)) (builder, Some(group_impl))
} }
fn generate_mandatory_params(mandatory_params: &[types::Type]) -> Vec<proc_macro2::TokenStream> { #[derive(Debug)]
mandatory_params.iter().map(param_with_name).collect() struct MandatoryParams<'a> {
params: &'a ApiParameters,
} }
fn generate_mandatory_param_builder( impl<'a> MandatoryParams<'a> {
mandatory_params: &[types::Type], fn new(params: &'a ApiParameters) -> Self {
) -> Vec<proc_macro2::TokenStream> { Self { params }
mandatory_params }
fn generate_mandatory_params(&self) -> Vec<proc_macro2::TokenStream> {
self.params
.mandatory
.iter()
.map(Self::param_with_name)
.collect()
}
fn param_with_name(param: &types::Type) -> proc_macro2::TokenStream {
let t = util::to_ident(&param.to_borrowed_type());
let (name, ..) = Self::name(param);
let t = if param.should_borrow() {
quote! { &#t }
} else {
quote! { #t }
};
quote! { #name: #t }
}
fn param_builder(&self) -> Vec<proc_macro2::TokenStream> {
self.params
.mandatory
.iter() .iter()
.map(|param| { .map(|param| {
let (name, name_as_str) = param_name(param); let (name, name_as_str) = Self::name(param);
quote! { let form = form.text(#name_as_str, #name.to_string()); } quote! { let form = form.text(#name_as_str, #name.to_string()); }
}) })
.collect() .collect()
} }
fn generate_optional_params(params: &[types::Type]) -> Vec<proc_macro2::TokenStream> { fn names(&self) -> Vec<proc_macro2::TokenStream> {
params.iter().map(generate_optional_param).collect() self.params
.mandatory
.iter()
.map(|param| {
let (name, ..) = Self::name(param);
quote! { #name }
})
.collect()
}
fn name(param: &types::Type) -> (proc_macro2::Ident, String) {
let name_as_str = param.get_type_info().name.to_snake();
(util::to_ident(&name_as_str), name_as_str)
}
}
#[derive(Debug)]
struct OptionalParams<'a> {
params: &'a ApiParameters,
}
impl<'a> OptionalParams<'a> {
fn new(params: &'a ApiParameters) -> Self {
Self { params }
}
fn generate_optional_params(&self) -> Vec<proc_macro2::TokenStream> {
self.params
.optional
.iter()
.map(Self::generate_optional_param)
.collect()
} }
fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream { fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream {
@ -121,21 +187,4 @@ fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream {
}, },
) )
} }
fn param_name(param: &types::Type) -> (proc_macro2::Ident, String) {
let name_as_str = param.get_type_info().name.to_snake();
(util::to_ident(&name_as_str), name_as_str)
}
fn param_with_name(param: &types::Type) -> proc_macro2::TokenStream {
let t = util::to_ident(&param.to_borrowed_type());
let (name, ..) = param_name(param);
let t = if param.should_borrow() {
quote! { &#t }
} else {
quote! { #t }
};
quote! { #name: #t }
} }

View File

@ -62,7 +62,7 @@ pub fn parse_api_method(child: &md_parser::TokenTree) -> Option<ApiMethod> {
fn to_api_method(child: &md_parser::TokenTree, name: &str) -> ApiMethod { fn to_api_method(child: &md_parser::TokenTree, name: &str) -> ApiMethod {
let method_description = parse_method_description(&child.content); let method_description = parse_method_description(&child.content);
let return_type = parse_return_type(&child.content); let return_type = parse_return_type(&child.content);
let parameters = parse_parameters(&child.content).map(|params| ApiParameters::new(params)); let parameters = parse_parameters(&child.content).map(ApiParameters::new);
let method_url = get_method_url(&child.content); let method_url = get_method_url(&child.content);
ApiMethod { ApiMethod {