Extract api group and method

This commit is contained in:
Joel Wachsler 2022-07-12 11:13:54 +00:00
parent 3360b2f3bd
commit 201ba025f3
5 changed files with 69 additions and 55 deletions

View File

@ -0,0 +1,32 @@
use crate::md_parser;
use super::{
api_method::parse_api_method, description::parse_group_description, url_parser::get_group_url,
ApiGroup,
};
pub fn parse_api_group(tree: md_parser::TokenTree) -> ApiGroup {
let methods = tree
.children
.into_iter()
.flat_map(parse_api_method)
.collect();
let group_description = parse_group_description(&tree.content);
let group_url = get_group_url(&tree.content);
let name = tree
.title
.unwrap()
.to_lowercase()
.trim_end_matches("(experimental)")
.trim()
.replace(' ', "_");
ApiGroup {
name,
methods,
description: group_description,
url: group_url,
}
}

View File

@ -0,0 +1,31 @@
use crate::md_parser;
use super::{
description::parse_method_description, parameters::parse_parameters,
return_type::parse_return_type, url_parser::get_method_url, util, ApiMethod,
};
pub fn parse_api_method(child: md_parser::TokenTree) -> Option<ApiMethod> {
util::find_content_starts_with(&child.content, "Name: ")
.map(|name| {
name.trim_start_matches("Name: ")
.trim_matches('`')
.to_string()
})
.map(|name| to_api_method(&child, &name))
}
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 method_url = get_method_url(&child.content);
ApiMethod {
name: name.to_string(),
description: method_description,
parameters,
return_type,
url: method_url,
}
}

View File

@ -1,6 +1,6 @@
use crate::md_parser::MdContent;
pub fn get_group_description(content: &[MdContent]) -> Option<String> {
pub fn parse_group_description(content: &[MdContent]) -> Option<String> {
let return_desc = content
.iter()
.map(|row| row.inner_value_as_string())
@ -16,7 +16,7 @@ pub fn get_group_description(content: &[MdContent]) -> Option<String> {
}
}
pub fn get_method_description(content: &[MdContent]) -> Option<String> {
pub fn parse_method_description(content: &[MdContent]) -> Option<String> {
let return_desc = content
.iter()
// skip until we get to the "Returns:" text

View File

@ -1,7 +1,9 @@
use crate::{md_parser, types};
use self::{parameters::parse_parameters, return_type::get_return_type, url_parser::get_method_url};
use self::api_group::parse_api_group;
mod api_group;
mod api_method;
mod description;
mod object_types;
mod parameters;
@ -66,57 +68,6 @@ pub fn parse_groups(trees: Vec<md_parser::TokenTree>) -> Vec<ApiGroup> {
trees.into_iter().map(parse_api_group).collect()
}
fn parse_api_group(tree: md_parser::TokenTree) -> ApiGroup {
let methods = tree
.children
.into_iter()
.flat_map(parse_api_method)
.collect();
let group_description = description::get_group_description(&tree.content);
let group_url = url_parser::get_group_url(&tree.content);
let name = tree
.title
.unwrap()
.to_lowercase()
.trim_end_matches("(experimental)")
.trim()
.replace(' ', "_");
ApiGroup {
name,
methods,
description: group_description,
url: group_url,
}
}
fn parse_api_method(child: md_parser::TokenTree) -> Option<ApiMethod> {
util::find_content_starts_with(&child.content, "Name: ")
.map(|name| {
name.trim_start_matches("Name: ")
.trim_matches('`')
.to_string()
})
.map(|name| to_api_method(&child, &name))
}
fn to_api_method(child: &md_parser::TokenTree, name: &str) -> ApiMethod {
let method_description = description::get_method_description(&child.content);
let return_type = get_return_type(&child.content);
let parameters = parse_parameters(&child.content);
let method_url = get_method_url(&child.content);
ApiMethod {
name: name.to_string(),
description: method_description,
parameters,
return_type,
url: method_url,
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -3,7 +3,7 @@ use crate::{
parser::{object_types::parse_object_types, types, ReturnType, ReturnTypeParameter},
};
pub fn get_return_type(content: &[MdContent]) -> Option<ReturnType> {
pub fn parse_return_type(content: &[MdContent]) -> Option<ReturnType> {
let table = content
.iter()
// The response is a ... <-- Trying to find this line