Extract more methods
This commit is contained in:
parent
2776870e0d
commit
9cafd434b2
|
@ -3,7 +3,7 @@ use quote::quote;
|
||||||
|
|
||||||
use crate::{generate::util, parser, types};
|
use crate::{generate::util, parser, types};
|
||||||
|
|
||||||
use super::{method_builder::MethodBuilder, return_type::create_return_type};
|
use super::{send_method_builder::SendMethodBuilder, return_type::create_return_type};
|
||||||
|
|
||||||
pub fn create_method_with_params(
|
pub fn create_method_with_params(
|
||||||
group: &parser::ApiGroup,
|
group: &parser::ApiGroup,
|
||||||
|
@ -12,40 +12,30 @@ pub fn create_method_with_params(
|
||||||
method_name: &proc_macro2::Ident,
|
method_name: &proc_macro2::Ident,
|
||||||
url: &str,
|
url: &str,
|
||||||
) -> (proc_macro2::TokenStream, Option<proc_macro2::TokenStream>) {
|
) -> (proc_macro2::TokenStream, Option<proc_macro2::TokenStream>) {
|
||||||
let parameter_type = util::to_ident(&format!(
|
let param_type = util::to_ident(&format!(
|
||||||
"{}{}Parameters",
|
"{}{}Parameters",
|
||||||
group.name.to_camel(),
|
group.name.to_camel(),
|
||||||
method.name.to_camel()
|
method.name.to_camel()
|
||||||
));
|
));
|
||||||
|
|
||||||
let mandatory_params = mandatory_params(params);
|
let mandatory_params = mandatory_params(params);
|
||||||
let mandatory_param_args = mandatory_params
|
let mandatory_param_args = generate_mandatory_params(&mandatory_params);
|
||||||
.iter()
|
|
||||||
.map(|param| param_with_name(param))
|
|
||||||
.collect::<Vec<proc_macro2::TokenStream>>();
|
|
||||||
|
|
||||||
let mandatory_param_names = mandatory_params.iter().map(|param| {
|
let mandatory_param_names = mandatory_params.iter().map(|param| {
|
||||||
let (name, ..) = param_name(param);
|
let (name, ..) = param_name(param);
|
||||||
quote! { #name }
|
quote! { #name }
|
||||||
});
|
});
|
||||||
|
|
||||||
let mandatory_param_form_build = mandatory_params.iter().map(|param| {
|
|
||||||
let (name, name_as_str) = param_name(param);
|
|
||||||
quote! { let form = form.text(#name_as_str, #name.to_string()); }
|
|
||||||
});
|
|
||||||
|
|
||||||
let optional_params = params
|
|
||||||
.iter()
|
|
||||||
.filter(|param| param.get_type_info().is_optional)
|
|
||||||
.map(generate_optional_parameter);
|
|
||||||
|
|
||||||
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 =
|
||||||
MethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }).with_form();
|
SendMethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }).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);
|
||||||
|
let mandatory_param_form_build = generate_mandatory_param_builder(&mandatory_params);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl<'a> #parameter_type<'a> {
|
impl<'a> #param_type<'a> {
|
||||||
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
|
fn new(group: &'a #group_name, #(#mandatory_param_args),*) -> Self {
|
||||||
let form = reqwest::multipart::Form::new();
|
let form = reqwest::multipart::Form::new();
|
||||||
#(#mandatory_param_form_build)*
|
#(#mandatory_param_form_build)*
|
||||||
|
@ -70,24 +60,52 @@ pub fn create_method_with_params(
|
||||||
None => generate_send_impl(send_builder.build()),
|
None => generate_send_impl(send_builder.build()),
|
||||||
};
|
};
|
||||||
|
|
||||||
(
|
let builder = util::add_docs(
|
||||||
util::add_docs(
|
&method.description,
|
||||||
&method.description,
|
quote! {
|
||||||
quote! {
|
pub fn #method_name(&self, #(#mandatory_param_args),*) -> #param_type {
|
||||||
pub fn #method_name(&self, #(#mandatory_param_args),*) -> #parameter_type {
|
#param_type::new(self, #(#mandatory_param_names),*)
|
||||||
#parameter_type::new(self, #(#mandatory_param_names),*)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Some(quote! {
|
|
||||||
pub struct #parameter_type<'a> {
|
|
||||||
group: &'a #group_name<'a>,
|
|
||||||
form: reqwest::multipart::Form,
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
#send
|
let group_impl = quote! {
|
||||||
}),
|
pub struct #param_type<'a> {
|
||||||
)
|
group: &'a #group_name<'a>,
|
||||||
|
form: reqwest::multipart::Form,
|
||||||
|
}
|
||||||
|
|
||||||
|
#send
|
||||||
|
};
|
||||||
|
|
||||||
|
(builder, Some(group_impl))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_mandatory_params(mandatory_params: &[&types::Type]) -> Vec<proc_macro2::TokenStream> {
|
||||||
|
mandatory_params
|
||||||
|
.iter()
|
||||||
|
.map(|param| param_with_name(param))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_mandatory_param_builder(
|
||||||
|
mandatory_params: &[&types::Type],
|
||||||
|
) -> Vec<proc_macro2::TokenStream> {
|
||||||
|
mandatory_params
|
||||||
|
.iter()
|
||||||
|
.map(|param| {
|
||||||
|
let (name, name_as_str) = param_name(param);
|
||||||
|
quote! { let form = form.text(#name_as_str, #name.to_string()); }
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_optional_params(params: &[types::Type]) -> Vec<proc_macro2::TokenStream> {
|
||||||
|
params
|
||||||
|
.iter()
|
||||||
|
.filter(|param| param.get_type_info().is_optional)
|
||||||
|
.map(generate_optional_param)
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mandatory_params(params: &[types::Type]) -> Vec<&types::Type> {
|
fn mandatory_params(params: &[types::Type]) -> Vec<&types::Type> {
|
||||||
|
@ -97,7 +115,7 @@ fn mandatory_params(params: &[types::Type]) -> Vec<&types::Type> {
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_optional_parameter(param: &types::Type) -> proc_macro2::TokenStream {
|
fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream {
|
||||||
let n = ¶m.get_type_info().name;
|
let n = ¶m.get_type_info().name;
|
||||||
let name = util::to_ident(&n.to_snake());
|
let name = util::to_ident(&n.to_snake());
|
||||||
let t = util::to_ident(¶m.to_borrowed_type());
|
let t = util::to_ident(¶m.to_borrowed_type());
|
||||||
|
@ -106,6 +124,7 @@ fn generate_optional_parameter(param: &types::Type) -> proc_macro2::TokenStream
|
||||||
} else {
|
} else {
|
||||||
quote! { #t }
|
quote! { #t }
|
||||||
};
|
};
|
||||||
|
|
||||||
util::add_docs(
|
util::add_docs(
|
||||||
¶m.get_type_info().description,
|
¶m.get_type_info().description,
|
||||||
quote! {
|
quote! {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
use super::{method_builder::MethodBuilder, return_type::create_return_type};
|
use super::{send_method_builder::SendMethodBuilder, return_type::create_return_type};
|
||||||
use crate::parser;
|
use crate::parser;
|
||||||
|
|
||||||
pub fn create_method_without_params(
|
pub fn create_method_without_params(
|
||||||
|
@ -9,7 +9,7 @@ pub fn create_method_without_params(
|
||||||
method_name: proc_macro2::Ident,
|
method_name: proc_macro2::Ident,
|
||||||
url: &str,
|
url: &str,
|
||||||
) -> (proc_macro2::TokenStream, Option<proc_macro2::TokenStream>) {
|
) -> (proc_macro2::TokenStream, Option<proc_macro2::TokenStream>) {
|
||||||
let builder = MethodBuilder::new(&method_name, url, quote! { self.auth })
|
let builder = SendMethodBuilder::new(&method_name, url, quote! { self.auth })
|
||||||
.description(&method.description);
|
.description(&method.description);
|
||||||
|
|
||||||
match create_return_type(group, method) {
|
match create_return_type(group, method) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
mod method_builder;
|
mod send_method_builder;
|
||||||
mod method_with_params;
|
mod method_with_params;
|
||||||
mod method_without_params;
|
mod method_without_params;
|
||||||
mod return_type;
|
mod return_type;
|
||||||
|
|
|
@ -2,7 +2,7 @@ use quote::quote;
|
||||||
|
|
||||||
use crate::generate::util;
|
use crate::generate::util;
|
||||||
|
|
||||||
pub struct MethodBuilder {
|
pub struct SendMethodBuilder {
|
||||||
method_name: syn::Ident,
|
method_name: syn::Ident,
|
||||||
url: String,
|
url: String,
|
||||||
auth_module_path: proc_macro2::TokenStream,
|
auth_module_path: proc_macro2::TokenStream,
|
||||||
|
@ -11,7 +11,7 @@ pub struct MethodBuilder {
|
||||||
form: bool,
|
form: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MethodBuilder {
|
impl SendMethodBuilder {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
method_name: &syn::Ident,
|
method_name: &syn::Ident,
|
||||||
url: &str,
|
url: &str,
|
Loading…
Reference in New Issue
Block a user