diff --git a/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs b/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs index 92bd4c3..57f7ba9 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs @@ -1,17 +1,27 @@ use quote::quote; +use crate::generate::util; + pub struct MethodBuilder { method_name: syn::Ident, url: String, + auth_module_path: proc_macro2::TokenStream, return_type: Option, + description: Option, } impl MethodBuilder { - pub fn new(method_name: &syn::Ident, url: &str) -> Self { + pub fn new( + method_name: &syn::Ident, + url: &str, + auth_module_path: proc_macro2::TokenStream, + ) -> Self { Self { method_name: method_name.clone(), url: url.to_string(), + auth_module_path, return_type: None, + description: None, } } @@ -20,6 +30,11 @@ impl MethodBuilder { self } + pub fn description(mut self, value: &Option) -> Self { + self.description = value.clone(); + self + } + pub fn build(&self) -> proc_macro2::TokenStream { let method_name = &self.method_name; let (return_type, parse_type) = match &self.return_type { @@ -27,18 +42,22 @@ impl MethodBuilder { None => (quote! { String }, quote! { .text() }), }; let url = &self.url; + let auth_module_path = &self.auth_module_path; - quote! { - pub async fn #method_name(&self) -> Result<#return_type> { - let res = self.auth - .authenticated_client(#url) - .send() - .await? - #parse_type - .await?; + util::add_docs( + &self.description, + quote! { + pub async fn #method_name(&self) -> Result<#return_type> { + let res = #auth_module_path + .authenticated_client(#url) + .send() + .await? + #parse_type + .await?; - Ok(res) - } - } + Ok(res) + } + }, + ) } } 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 e3d2bc3..0b74ac1 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,7 @@ -use crate::{generate::util, parser}; +use quote::quote; use super::{method_builder::MethodBuilder, return_type::create_return_type}; +use crate::parser; pub fn create_method_without_params( group: &parser::ApiGroup, @@ -8,19 +9,18 @@ pub fn create_method_without_params( method_name: proc_macro2::Ident, url: &str, ) -> (proc_macro2::TokenStream, Option) { - let res = match create_return_type(group, method) { + let builder = MethodBuilder::new(&method_name, url, quote! { self.auth }) + .description(&method.description); + + match create_return_type(group, method) { Some((return_type_name, return_type)) => ( - MethodBuilder::new(&method_name, url) - .return_type(&return_type_name) - .build(), + builder.return_type(&return_type_name).build(), Some(return_type), ), None => ( - MethodBuilder::new(&method_name, url).build(), + builder.build(), // assume that all methods without a return type returns a string None, ), - }; - - (util::add_docs(&method.description, res.0), res.1) + } }