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 2c72049..1431011 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 @@ -10,7 +10,9 @@ use crate::{ types, }; -use super::{return_type::create_return_type, send_method_builder::SendMethodBuilder}; +use super::{ + return_type::create_return_type, send_method_builder::SendMethodBuilder, MethodsAndExtra, +}; pub fn create_method_with_params( group: &parser::ApiGroup, @@ -18,7 +20,7 @@ pub fn create_method_with_params( params: &parser::ApiParameters, method_name: &proc_macro2::Ident, url: &str, -) -> (TokenStream, Option) { +) -> MethodsAndExtra { let param_type = util::to_ident(&format!( "{}{}Parameters", group.name.to_camel(), @@ -27,38 +29,43 @@ pub fn create_method_with_params( let parameters = Parameters::new(params); - 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(); + if parameters.optional.is_empty() { + let fooz = quote! {}; + MethodsAndExtra::new(fooz) + } 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(); - let send_impl_generator = SendImplGenerator::new(&group_name, ¶meters, ¶m_type); + let send_impl_generator = SendImplGenerator::new(&group_name, ¶meters, ¶m_type); - let send = match create_return_type(group, method) { - Some((return_type_name, return_type)) => { - let send_impl = - send_impl_generator.generate(send_builder.return_type(&return_type_name)); + let send = match create_return_type(group, method) { + Some((return_type_name, return_type)) => { + let send_impl = + send_impl_generator.generate(send_builder.return_type(&return_type_name)); - quote! { - #send_impl - #return_type + quote! { + #send_impl + #return_type + } } - } - None => send_impl_generator.generate(send_builder), - }; + None => send_impl_generator.generate(send_builder), + }; - let builder = generate_builder(¶meters, method, method_name, ¶m_type); + let builder = generate_builder(¶meters, method, method_name, ¶m_type); - let group_impl = quote! { - pub struct #param_type<'a> { - group: &'a #group_name<'a>, - form: reqwest::multipart::Form, - } + let group_impl = quote! { + pub struct #param_type<'a> { + group: &'a #group_name<'a>, + form: reqwest::multipart::Form, + } - #send - }; + #send + }; - (builder, Some(group_impl)) + MethodsAndExtra::new(builder).with_structs(group_impl) + } } fn generate_builder( @@ -196,6 +203,10 @@ impl<'a> OptionalParams<'a> { } } + fn is_empty(&self) -> bool { + self.params.is_empty() + } + fn generate_builder_methods(&self) -> Vec { self.params .iter() diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs index b705257..ec1d3a7 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_without_params.rs @@ -1,6 +1,8 @@ use quote::quote; -use super::{return_type::create_return_type, send_method_builder::SendMethodBuilder}; +use super::{ + return_type::create_return_type, send_method_builder::SendMethodBuilder, MethodsAndExtra, +}; use crate::parser; pub fn create_method_without_params( @@ -8,19 +10,18 @@ pub fn create_method_without_params( method: &parser::ApiMethod, method_name: proc_macro2::Ident, url: &str, -) -> (proc_macro2::TokenStream, Option) { +) -> MethodsAndExtra { let builder = SendMethodBuilder::new(&method_name, url, quote! { self.auth }) .description(&method.description); match create_return_type(group, method) { - Some((return_type_name, return_type)) => ( - builder.return_type(&return_type_name).build(), - Some(return_type), - ), - None => ( - builder.build(), + Some((return_type_name, return_type)) => { + MethodsAndExtra::new(builder.return_type(&return_type_name).build()) + .with_structs(return_type) + } + None => { // assume that all methods without a return type returns a string - None, - ), + MethodsAndExtra::new(builder.build()) + } } } diff --git a/qbittorrent-web-api-gen/src/generate/group/method/mod.rs b/qbittorrent-web-api-gen/src/generate/group/method/mod.rs index 3c2ea73..041cec4 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/mod.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/mod.rs @@ -17,13 +17,16 @@ pub fn generate_methods( auth: &syn::Ident, group_name_camel: &syn::Ident, ) -> proc_macro2::TokenStream { - let methods_and_param_structs = group + let methods_and_extra = group .methods .iter() .map(|method| generate_method(group, method)); - let methods = methods_and_param_structs.clone().map(|(method, ..)| method); - let structs = methods_and_param_structs.flat_map(|(_, s)| s); + let methods = methods_and_extra + .clone() + .map(|MethodsAndExtra { methods, .. }| methods); + + let extra = methods_and_extra.flat_map(|MethodsAndExtra { extra: structs, .. }| structs); quote! { impl <'a> #group_name_camel<'a> { @@ -34,14 +37,31 @@ pub fn generate_methods( #(#methods)* } - #(#structs)* + #(#extra)* } } -fn generate_method( - group: &parser::ApiGroup, - method: &parser::ApiMethod, -) -> (proc_macro2::TokenStream, Option) { +#[derive(Debug)] +pub struct MethodsAndExtra { + methods: proc_macro2::TokenStream, + extra: Option, +} + +impl MethodsAndExtra { + pub fn new(methods: proc_macro2::TokenStream) -> Self { + Self { + methods, + extra: None, + } + } + + pub fn with_structs(mut self, structs: proc_macro2::TokenStream) -> Self { + self.extra = Some(structs); + self + } +} + +fn generate_method(group: &parser::ApiGroup, method: &parser::ApiMethod) -> MethodsAndExtra { let method_name = util::to_ident(&method.name.to_snake()); let url = format!("/api/v2/{}/{}", group.url, method.url);