Add abstraction over ApiParameters
This commit is contained in:
parent
79004924ec
commit
250a1d098c
File diff suppressed because it is too large
Load Diff
|
@ -8,7 +8,7 @@ use super::{return_type::create_return_type, send_method_builder::SendMethodBuil
|
|||
pub fn create_method_with_params(
|
||||
group: &parser::ApiGroup,
|
||||
method: &parser::ApiMethod,
|
||||
params: &[types::Type],
|
||||
params: &parser::ApiParameters,
|
||||
method_name: &proc_macro2::Ident,
|
||||
url: &str,
|
||||
) -> (proc_macro2::TokenStream, Option<proc_macro2::TokenStream>) {
|
||||
|
@ -18,10 +18,9 @@ pub fn create_method_with_params(
|
|||
method.name.to_camel()
|
||||
));
|
||||
|
||||
let mandatory_params = mandatory_params(params);
|
||||
let mandatory_param_args = generate_mandatory_params(&mandatory_params);
|
||||
let mandatory_param_args = generate_mandatory_params(¶ms.mandatory);
|
||||
|
||||
let mandatory_param_names = mandatory_params.iter().map(|param| {
|
||||
let mandatory_param_names = params.mandatory.iter().map(|param| {
|
||||
let (name, ..) = param_name(param);
|
||||
quote! { #name }
|
||||
});
|
||||
|
@ -32,8 +31,8 @@ pub fn create_method_with_params(
|
|||
.with_form();
|
||||
|
||||
let generate_send_impl = |send_method: proc_macro2::TokenStream| {
|
||||
let optional_params = generate_optional_params(params);
|
||||
let mandatory_param_form_build = generate_mandatory_param_builder(&mandatory_params);
|
||||
let optional_params = generate_optional_params(¶ms.optional);
|
||||
let mandatory_param_form_build = generate_mandatory_param_builder(¶ms.mandatory);
|
||||
|
||||
quote! {
|
||||
impl<'a> #param_type<'a> {
|
||||
|
@ -82,15 +81,12 @@ pub fn create_method_with_params(
|
|||
(builder, Some(group_impl))
|
||||
}
|
||||
|
||||
fn generate_mandatory_params(mandatory_params: &[&types::Type]) -> Vec<proc_macro2::TokenStream> {
|
||||
mandatory_params
|
||||
.iter()
|
||||
.map(|param| param_with_name(param))
|
||||
.collect()
|
||||
fn generate_mandatory_params(mandatory_params: &[types::Type]) -> Vec<proc_macro2::TokenStream> {
|
||||
mandatory_params.iter().map(param_with_name).collect()
|
||||
}
|
||||
|
||||
fn generate_mandatory_param_builder(
|
||||
mandatory_params: &[&types::Type],
|
||||
mandatory_params: &[types::Type],
|
||||
) -> Vec<proc_macro2::TokenStream> {
|
||||
mandatory_params
|
||||
.iter()
|
||||
|
@ -102,18 +98,7 @@ fn generate_mandatory_param_builder(
|
|||
}
|
||||
|
||||
fn generate_optional_params(params: &[types::Type]) -> Vec<proc_macro2::TokenStream> {
|
||||
params
|
||||
.iter()
|
||||
.filter(|param| param.get_type_info().is_optional)
|
||||
.map(generate_optional_param)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn mandatory_params(params: &[types::Type]) -> Vec<&types::Type> {
|
||||
params
|
||||
.iter()
|
||||
.filter(|param| !param.get_type_info().is_optional)
|
||||
.collect()
|
||||
params.iter().map(generate_optional_param).collect()
|
||||
}
|
||||
|
||||
fn generate_optional_param(param: &types::Type) -> proc_macro2::TokenStream {
|
||||
|
|
|
@ -16,11 +16,39 @@ use self::{
|
|||
pub struct ApiMethod {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub parameters: Option<Vec<types::Type>>,
|
||||
pub parameters: Option<ApiParameters>,
|
||||
pub return_type: Option<ReturnType>,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ApiParameters {
|
||||
pub mandatory: Vec<types::Type>,
|
||||
pub optional: Vec<types::Type>,
|
||||
}
|
||||
|
||||
impl ApiParameters {
|
||||
fn new(params: Vec<types::Type>) -> Self {
|
||||
let (mandatory, optional) = params.into_iter().fold(
|
||||
(vec![], vec![]),
|
||||
|(mut mandatory, mut optional), parameter| {
|
||||
if parameter.get_type_info().is_optional {
|
||||
optional.push(parameter);
|
||||
} else {
|
||||
mandatory.push(parameter);
|
||||
}
|
||||
|
||||
(mandatory, optional)
|
||||
},
|
||||
);
|
||||
|
||||
Self {
|
||||
mandatory,
|
||||
optional,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_api_method(child: &md_parser::TokenTree) -> Option<ApiMethod> {
|
||||
util::find_content_starts_with(&child.content, "Name: ")
|
||||
.map(|name| {
|
||||
|
@ -34,7 +62,7 @@ pub fn parse_api_method(child: &md_parser::TokenTree) -> Option<ApiMethod> {
|
|||
fn to_api_method(child: &md_parser::TokenTree, name: &str) -> ApiMethod {
|
||||
let method_description = parse_method_description(&child.content);
|
||||
let return_type = parse_return_type(&child.content);
|
||||
let parameters = parse_parameters(&child.content);
|
||||
let parameters = parse_parameters(&child.content).map(|params| ApiParameters::new(params));
|
||||
let method_url = get_method_url(&child.content);
|
||||
|
||||
ApiMethod {
|
||||
|
|
|
@ -5,7 +5,7 @@ mod url;
|
|||
use crate::md_parser;
|
||||
|
||||
use self::{description::parse_group_description, method::parse_api_method, url::get_group_url};
|
||||
pub use method::{ApiMethod, ReturnType};
|
||||
pub use method::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ApiGroup {
|
||||
|
|
|
@ -12,7 +12,7 @@ pub struct ReturnTypeParameter {
|
|||
pub return_type: types::Type,
|
||||
}
|
||||
|
||||
pub use group::{ApiGroup, ApiMethod, ReturnType};
|
||||
pub use group::*;
|
||||
|
||||
pub fn parse_api_groups(token_tree: md_parser::TokenTree) -> Vec<ApiGroup> {
|
||||
parse_groups(extract_relevant_parts(token_tree))
|
||||
|
|
Loading…
Reference in New Issue
Block a user