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,61 +90,101 @@ 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 }
.iter()
.map(|param| { fn generate_mandatory_params(&self) -> Vec<proc_macro2::TokenStream> {
let (name, name_as_str) = param_name(param); self.params
quote! { let form = form.text(#name_as_str, #name.to_string()); } .mandatory
}) .iter()
.collect() .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()
.map(|param| {
let (name, name_as_str) = Self::name(param);
quote! { let form = form.text(#name_as_str, #name.to_string()); }
})
.collect()
}
fn names(&self) -> Vec<proc_macro2::TokenStream> {
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)
}
} }
fn generate_optional_params(params: &[types::Type]) -> Vec<proc_macro2::TokenStream> { #[derive(Debug)]
params.iter().map(generate_optional_param).collect() struct OptionalParams<'a> {
params: &'a ApiParameters,
} }
fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream { impl<'a> OptionalParams<'a> {
let n = &param.get_type_info().name; fn new(params: &'a ApiParameters) -> Self {
let name = util::to_ident(&n.to_snake()); Self { params }
let t = util::to_ident(&param.to_borrowed_type()); }
let builder_param = if param.should_borrow() {
quote! { &#t }
} else {
quote! { #t }
};
util::add_docs( fn generate_optional_params(&self) -> Vec<proc_macro2::TokenStream> {
&param.get_type_info().description, self.params
quote! { .optional
pub fn #name(mut self, value: #builder_param) -> Self { .iter()
self.form = self.form.text(#n, value.to_string()); .map(Self::generate_optional_param)
self .collect()
} }
},
) fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream {
} let n = &param.get_type_info().name;
let name = util::to_ident(&n.to_snake());
fn param_name(param: &types::Type) -> (proc_macro2::Ident, String) { let t = util::to_ident(&param.to_borrowed_type());
let name_as_str = param.get_type_info().name.to_snake(); let builder_param = if param.should_borrow() {
(util::to_ident(&name_as_str), name_as_str) quote! { &#t }
} } else {
quote! { #t }
fn param_with_name(param: &types::Type) -> proc_macro2::TokenStream { };
let t = util::to_ident(&param.to_borrowed_type());
util::add_docs(
let (name, ..) = param_name(param); &param.get_type_info().description,
let t = if param.should_borrow() { quote! {
quote! { &#t } pub fn #name(mut self, value: #builder_param) -> Self {
} else { self.form = self.form.text(#n, value.to_string());
quote! { #t } self
}; }
},
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 {