Convert send method to builder

This commit is contained in:
Joel Wachsler 2022-08-06 11:27:03 +00:00
parent 3a1af358b0
commit 2ad53985ba

View File

@ -75,16 +75,15 @@ impl<'a> GroupMethod<'a> {
let form_builder = self.mandatory_parameters_as_form_builder();
let method_impl = if self.method.types.optional_parameters().is_empty() {
self.generate_send_method(
&method_name,
parameters,
quote! { self.auth },
quote! { form },
quote! {
let form = reqwest::multipart::Form::new();
#form_builder
},
)
let builder =
SendMethodBuilder::new(&method_name, quote! { self.auth }, quote! { form })
.set_parameters(parameters)
.set_form_factory(quote! {
let form = reqwest::multipart::Form::new();
#form_builder
});
self.generate_send_method(builder)
} else {
quote! {
pub fn #method_name(&self, #(#parameters),*) -> Builder<'_> {
@ -139,13 +138,11 @@ impl<'a> GroupMethod<'a> {
.map(|param| param.generate_optional_builder_method_with_docs());
let group_name = self.group.struct_name();
let send_method = self.generate_send_method(
let send_method = self.generate_send_method(SendMethodBuilder::new(
&util::to_ident("send"),
vec![],
quote! { self.group.auth },
quote! { self.form },
quote! {},
);
));
quote! {
pub struct Builder<'a> {
@ -160,29 +157,55 @@ impl<'a> GroupMethod<'a> {
}
}
fn generate_send_method(
&self,
method_name: &Ident,
parameters: Vec<TokenStream>,
auth_access: TokenStream,
form_access: TokenStream,
form_factory: TokenStream,
) -> TokenStream {
let method_url = format!("/api/v2/{}/{}", self.group.url(), self.method.url);
fn generate_send_method(&self, builder: SendMethodBuilder) -> TokenStream {
builder.set_group(self).generate_send_method()
}
let (response_type, response_parse) = match self.method.types.response() {
Some(resp) => {
if resp.is_list {
(
quote! { std::vec::Vec<Response> },
quote! { .json::<std::vec::Vec<Response>>() },
)
} else {
(quote! { Response }, quote! { .json::<Response>() })
}
}
None => (quote! { String }, quote! { .text() }),
};
fn mandatory_parameters_as_form_builder(&self) -> TokenStream {
let builder = self
.method
.types
.mandatory_params()
.into_iter()
.map(|param| param.generate_form_builder(quote! { form }));
quote! {
#(let #builder)*
}
}
}
pub struct SendMethodBuilder<'a> {
group: Option<&'a GroupMethod<'a>>,
method_name: &'a Ident,
parameters: Vec<TokenStream>,
auth_access: TokenStream,
form_access: TokenStream,
form_factory: Option<TokenStream>,
override_return_type: Option<TokenStream>,
}
impl<'a> SendMethodBuilder<'a> {
pub fn new(method_name: &'a Ident, auth_access: TokenStream, form_access: TokenStream) -> Self {
Self {
group: Option::None,
method_name,
parameters: vec![],
auth_access,
form_access,
form_factory: Option::None,
override_return_type: Option::None,
}
}
fn generate_send_method(self) -> TokenStream {
let method_url = self.method_url();
let (response_type, response_parse) = self.response_type();
let form_factory = self.form_factory();
let parameters = self.parameters;
let form_access = self.form_access;
let method_name = self.method_name;
let auth_access = self.auth_access;
quote! {
pub async fn #method_name(self, #(#parameters),*) -> super::super::Result<#response_type> {
@ -200,16 +223,55 @@ impl<'a> GroupMethod<'a> {
}
}
fn mandatory_parameters_as_form_builder(&self) -> TokenStream {
let builder = self
.method
.types
.mandatory_params()
.into_iter()
.map(|param| param.generate_form_builder(quote! { form }));
quote! {
#(let #builder)*
fn response_type(&self) -> (TokenStream, TokenStream) {
match self.group().method.types.response() {
Some(resp) => {
if resp.is_list {
(
quote! { std::vec::Vec<Response> },
quote! { .json::<std::vec::Vec<Response>>() },
)
} else {
(quote! { Response }, quote! { .json::<Response>() })
}
}
None => (quote! { String }, quote! { .text() }),
}
}
fn method_url(&self) -> String {
format!(
"/api/v2/{}/{}",
self.group().group.url(),
self.group().method.url
)
}
fn set_override_return_type(mut self, override_return_type: TokenStream) -> Self {
self.override_return_type = Some(override_return_type);
self
}
fn set_form_factory(mut self, form_factory: TokenStream) -> Self {
self.form_factory = Some(form_factory);
self
}
fn form_factory(&self) -> TokenStream {
self.form_factory.clone().unwrap_or_else(|| quote! {})
}
fn set_parameters(mut self, parameters: Vec<TokenStream>) -> Self {
self.parameters = parameters;
self
}
fn set_group(mut self, group: &'a GroupMethod<'a>) -> Self {
self.group = Option::Some(group);
self
}
fn group(&self) -> &GroupMethod {
self.group.expect("Group is not defined")
}
}