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