Extract method_builder to own file

This commit is contained in:
Joel Wachsler 2022-07-12 13:08:41 +00:00
parent 8ed9a98843
commit dfa61d203a
4 changed files with 47 additions and 48 deletions

View File

@ -0,0 +1,44 @@
use quote::quote;
pub struct MethodBuilder {
method_name: syn::Ident,
url: String,
return_type: Option<proc_macro2::TokenStream>,
}
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)
}
}
}
}

View File

@ -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<proc_macro2::TokenStream>,
}
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,

View File

@ -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;

View File

@ -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 {