From cc9aeaf6dba76160eb289e92a2277e0789b2564b Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Fri, 15 Jul 2022 00:03:52 +0000 Subject: [PATCH] Add parameters --- .../group/method/method_with_params.rs | 71 ++++++++++++++++--- .../group/method/send_method_builder.rs | 18 ++++- 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs index 1431011..d6d9dc6 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_with_params.rs @@ -28,19 +28,55 @@ pub fn create_method_with_params( )); let parameters = Parameters::new(params); + let has_optional_parameters = !parameters.optional.is_empty(); + let generator = MethodGenerator { + group, + url, + parameters, + param_type, + method, + method_name, + }; - if parameters.optional.is_empty() { - let fooz = quote! {}; - MethodsAndExtra::new(fooz) + if has_optional_parameters { + generator.generate_method_with_builder() } else { - let group_name = util::to_ident(&group.name.to_camel()); - let send_builder = - SendMethodBuilder::new(&util::to_ident("send"), url, quote! { self.group.auth }) - .with_form(); + generator.generate_method_without_builder() + } +} - let send_impl_generator = SendImplGenerator::new(&group_name, ¶meters, ¶m_type); +#[derive(Debug)] +struct MethodGenerator<'a> { + group: &'a parser::ApiGroup, + url: &'a str, + parameters: Parameters<'a>, + param_type: proc_macro2::Ident, + method: &'a ApiMethod, + method_name: &'a proc_macro2::Ident, +} - let send = match create_return_type(group, method) { +impl<'a> MethodGenerator<'a> { + fn generate_method_without_builder(&self) -> MethodsAndExtra { + let builder = SendMethodBuilder::new(self.method_name, self.url, quote! { self.auth }) + .description(&self.method.description) + .with_args(&self.parameters.mandatory.generate_params()); + + match create_return_type(self.group, self.method) { + Some((return_type_name, return_type)) => { + MethodsAndExtra::new(builder.return_type(&return_type_name).build()) + .with_structs(return_type) + } + None => MethodsAndExtra::new(builder.build()), + } + } + + fn generate_method_with_builder(&self) -> MethodsAndExtra { + let group_name = self.group_name(); + let send_builder = self.send_builder(&util::to_ident("send")); + let send_impl_generator = + SendImplGenerator::new(&group_name, &self.parameters, &self.param_type); + + let send = match create_return_type(self.group, self.method) { Some((return_type_name, return_type)) => { let send_impl = send_impl_generator.generate(send_builder.return_type(&return_type_name)); @@ -52,8 +88,13 @@ pub fn create_method_with_params( } None => send_impl_generator.generate(send_builder), }; - - let builder = generate_builder(¶meters, method, method_name, ¶m_type); + let builder = generate_builder( + &self.parameters, + self.method, + self.method_name, + &self.param_type, + ); + let param_type = &self.param_type; let group_impl = quote! { pub struct #param_type<'a> { @@ -66,6 +107,14 @@ pub fn create_method_with_params( MethodsAndExtra::new(builder).with_structs(group_impl) } + + fn group_name(&self) -> proc_macro2::Ident { + util::to_ident(&self.group.name.to_camel()) + } + + fn send_builder(&self, name: &proc_macro2::Ident) -> SendMethodBuilder { + SendMethodBuilder::new(name, self.url, quote! { self.group.auth }).with_form() + } } fn generate_builder( diff --git a/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs b/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs index da0c44a..0b1194a 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/send_method_builder.rs @@ -8,6 +8,7 @@ pub struct SendMethodBuilder { auth_module_path: proc_macro2::TokenStream, return_type: Option, description: Option, + args: Vec, form: bool, } @@ -24,6 +25,7 @@ impl SendMethodBuilder { return_type: None, description: None, form: false, + args: vec![], } } @@ -42,6 +44,14 @@ impl SendMethodBuilder { self } + pub fn with_args(mut self, value: &[proc_macro2::TokenStream]) -> Self { + for v in value { + self.args.push(v.clone()); + } + + self + } + pub fn build(&self) -> proc_macro2::TokenStream { let method_name = &self.method_name; let (return_type, parse_type) = match &self.return_type { @@ -55,11 +65,17 @@ impl SendMethodBuilder { } else { quote! {} }; + let arg_list = &self.args; + let args = if !arg_list.is_empty() { + quote! { , #(#arg_list),* } + } else { + quote! {} + }; util::add_docs( &self.description, quote! { - pub async fn #method_name(self) -> Result<#return_type> { + pub async fn #method_name(self #args) -> Result<#return_type> { let res = #auth_module_path .authenticated_client(#url) #form