From dfa61d203a06799ddd2c8bc155363621cc33530b Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 13:08:41 +0000 Subject: [PATCH] Extract method_builder to own file --- .../generate/group/method/method_builder.rs | 44 ++++++++++++++++++ .../group/method/method_without_params.rs | 46 +------------------ .../src/generate/group/method/mod.rs | 1 + .../src/generate/skeleton.rs | 4 +- 4 files changed, 47 insertions(+), 48 deletions(-) create mode 100644 qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs 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 new file mode 100644 index 0000000..92bd4c3 --- /dev/null +++ b/qbittorrent-web-api-gen/src/generate/group/method/method_builder.rs @@ -0,0 +1,44 @@ +use quote::quote; + +pub struct MethodBuilder { + method_name: syn::Ident, + url: String, + return_type: Option, +} + +impl MethodBuilder { + pub fn new(method_name: &syn::Ident, url: &str) -> Self { + Self { + method_name: method_name.clone(), + url: url.to_string(), + return_type: None, + } + } + + pub fn return_type(mut self, value: &proc_macro2::TokenStream) -> Self { + self.return_type = Some(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 { + Some(t) => (t.clone(), quote! { .json::<#t>() }), + None => (quote! { String }, quote! { .text() }), + }; + let url = &self.url; + + quote! { + pub async fn #method_name(&self) -> Result<#return_type> { + let res = self.auth + .authenticated_client(#url) + .send() + .await? + #parse_type + .await?; + + 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 91c3a51..e3d2bc3 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,50 +1,6 @@ use crate::{generate::util, parser}; -use quote::quote; -use super::return_type::create_return_type; - -struct MethodBuilder { - method_name: syn::Ident, - url: String, - return_type: Option, -} - -impl MethodBuilder { - fn new(method_name: &syn::Ident, url: &str) -> Self { - Self { - method_name: method_name.clone(), - url: url.to_string(), - return_type: None, - } - } - - fn return_type(mut self, value: &proc_macro2::TokenStream) -> Self { - self.return_type = Some(value.clone()); - self - } - - fn build(&self) -> proc_macro2::TokenStream { - let method_name = &self.method_name; - let (return_type, parse_type) = match &self.return_type { - Some(t) => (t.clone(), quote! { .json::<#t>() }), - None => (quote! { String }, quote! { .text() }), - }; - let url = &self.url; - - quote! { - pub async fn #method_name(&self) -> Result<#return_type> { - let res = self.auth - .authenticated_client(#url) - .send() - .await? - #parse_type - .await?; - - Ok(res) - } - } - } -} +use super::{method_builder::MethodBuilder, return_type::create_return_type}; pub fn create_method_without_params( group: &parser::ApiGroup, 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 764f8e5..5237f52 100644 --- a/qbittorrent-web-api-gen/src/generate/group/method/mod.rs +++ b/qbittorrent-web-api-gen/src/generate/group/method/mod.rs @@ -1,6 +1,7 @@ mod method_with_params; mod method_without_params; mod return_type; +mod method_builder; use crate::{generate::util, parser}; use case::CaseExt; diff --git a/qbittorrent-web-api-gen/src/generate/skeleton.rs b/qbittorrent-web-api-gen/src/generate/skeleton.rs index e2118a7..fb74c23 100644 --- a/qbittorrent-web-api-gen/src/generate/skeleton.rs +++ b/qbittorrent-web-api-gen/src/generate/skeleton.rs @@ -2,10 +2,8 @@ use quote::quote; use super::util; -pub const AUTH_IDENT: &str = "Authenticated"; - pub fn auth_ident() -> proc_macro2::Ident { - util::to_ident(AUTH_IDENT) + util::to_ident("Authenticated") } pub fn generate_skeleton(ident: &syn::Ident) -> proc_macro2::TokenStream {