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 form_builder = self.mandatory_parameters_as_form_builder();
let method_impl = if self.method.types.optional_parameters().is_empty() { let method_impl = if self.method.types.optional_parameters().is_empty() {
self.generate_send_method( let builder =
&method_name, SendMethodBuilder::new(&method_name, quote! { self.auth }, quote! { form })
parameters, .set_parameters(parameters)
quote! { self.auth }, .set_form_factory(quote! {
quote! { form }, let form = reqwest::multipart::Form::new();
quote! { #form_builder
let form = reqwest::multipart::Form::new(); });
#form_builder
}, self.generate_send_method(builder)
)
} else { } else {
quote! { quote! {
pub fn #method_name(&self, #(#parameters),*) -> Builder<'_> { pub fn #method_name(&self, #(#parameters),*) -> Builder<'_> {
@ -139,13 +138,11 @@ impl<'a> GroupMethod<'a> {
.map(|param| param.generate_optional_builder_method_with_docs()); .map(|param| param.generate_optional_builder_method_with_docs());
let group_name = self.group.struct_name(); 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"), &util::to_ident("send"),
vec![],
quote! { self.group.auth }, quote! { self.group.auth },
quote! { self.form }, quote! { self.form },
quote! {}, ));
);
quote! { quote! {
pub struct Builder<'a> { pub struct Builder<'a> {
@ -160,29 +157,55 @@ impl<'a> GroupMethod<'a> {
} }
} }
fn generate_send_method( fn generate_send_method(&self, builder: SendMethodBuilder) -> TokenStream {
&self, builder.set_group(self).generate_send_method()
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);
let (response_type, response_parse) = match self.method.types.response() { fn mandatory_parameters_as_form_builder(&self) -> TokenStream {
Some(resp) => { let builder = self
if resp.is_list { .method
( .types
quote! { std::vec::Vec<Response> }, .mandatory_params()
quote! { .json::<std::vec::Vec<Response>>() }, .into_iter()
) .map(|param| param.generate_form_builder(quote! { form }));
} else {
(quote! { Response }, quote! { .json::<Response>() }) quote! {
} #(let #builder)*
} }
None => (quote! { String }, quote! { .text() }), }
}; }
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! { quote! {
pub async fn #method_name(self, #(#parameters),*) -> super::super::Result<#response_type> { 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 { fn response_type(&self) -> (TokenStream, TokenStream) {
let builder = self match self.group().method.types.response() {
.method Some(resp) => {
.types if resp.is_list {
.mandatory_params() (
.into_iter() quote! { std::vec::Vec<Response> },
.map(|param| param.generate_form_builder(quote! { form })); quote! { .json::<std::vec::Vec<Response>>() },
)
quote! { } else {
#(let #builder)* (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")
}
} }