Add docs to method_builder

This commit is contained in:
Joel Wachsler 2022-07-12 13:17:50 +00:00
parent dfa61d203a
commit 0684b4681a
2 changed files with 40 additions and 21 deletions

View File

@ -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<proc_macro2::TokenStream>,
description: Option<String>,
}
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<String>) -> 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)
}
},
)
}
}

View File

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