Refactor api parsing
This commit is contained in:
parent
bc54877578
commit
3360b2f3bd
|
@ -1,6 +1,6 @@
|
|||
use crate::{md_parser, types};
|
||||
|
||||
use self::{parameters::get_parameters, return_type::get_return_type, url_parser::get_method_url};
|
||||
use self::{parameters::parse_parameters, return_type::get_return_type, url_parser::get_method_url};
|
||||
|
||||
mod description;
|
||||
mod object_types;
|
||||
|
@ -39,6 +39,12 @@ pub struct ReturnTypeParameter {
|
|||
pub return_type: types::Type,
|
||||
}
|
||||
|
||||
pub fn parse_api_groups(content: &str) -> Vec<ApiGroup> {
|
||||
parse_groups(extract_relevant_parts(md_parser::TokenTreeFactory::create(
|
||||
content,
|
||||
)))
|
||||
}
|
||||
|
||||
fn extract_relevant_parts(tree: md_parser::TokenTree) -> Vec<md_parser::TokenTree> {
|
||||
let relevant: Vec<md_parser::TokenTree> = tree
|
||||
.children
|
||||
|
@ -56,12 +62,6 @@ fn extract_relevant_parts(tree: md_parser::TokenTree) -> Vec<md_parser::TokenTre
|
|||
relevant
|
||||
}
|
||||
|
||||
pub fn parse_api_groups(content: &str) -> Vec<ApiGroup> {
|
||||
parse_groups(extract_relevant_parts(md_parser::TokenTreeFactory::create(
|
||||
content,
|
||||
)))
|
||||
}
|
||||
|
||||
pub fn parse_groups(trees: Vec<md_parser::TokenTree>) -> Vec<ApiGroup> {
|
||||
trees.into_iter().map(parse_api_group).collect()
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ fn parse_api_method(child: md_parser::TokenTree) -> Option<ApiMethod> {
|
|||
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 = get_parameters(&child.content);
|
||||
let parameters = parse_parameters(&child.content);
|
||||
let method_url = get_method_url(&child.content);
|
||||
|
||||
ApiMethod {
|
||||
|
|
|
@ -1,34 +1,28 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::{md_parser::MdContent, parser::types};
|
||||
use crate::{md_parser, parser::types};
|
||||
|
||||
use super::types::TypeDescription;
|
||||
|
||||
pub fn get_object_types(content: &[MdContent]) -> HashMap<String, TypeDescription> {
|
||||
pub fn parse_object_types(
|
||||
content: &[md_parser::MdContent],
|
||||
) -> HashMap<String, types::TypeDescription> {
|
||||
let mut output = HashMap::new();
|
||||
let mut content_it = content.iter();
|
||||
|
||||
while let Some(entry) = content_it.next() {
|
||||
if let MdContent::Text(content) = entry {
|
||||
if let md_parser::MdContent::Text(content) = entry {
|
||||
const POSSIBLE_VALUES_OF: &str = "Possible values of ";
|
||||
if content.contains(POSSIBLE_VALUES_OF) {
|
||||
// is empty
|
||||
content_it.next();
|
||||
if let Some(MdContent::Table(table)) = content_it.next() {
|
||||
let enum_types = table
|
||||
.rows
|
||||
.iter()
|
||||
.map(|row| types::TypeDescriptions {
|
||||
value: row.columns[0].to_string(),
|
||||
description: row.columns[1].to_string(),
|
||||
})
|
||||
.collect();
|
||||
if let Some(md_parser::MdContent::Table(table)) = content_it.next() {
|
||||
let enum_types = to_type_descriptions(table);
|
||||
|
||||
let name = content
|
||||
.trim_start_matches(POSSIBLE_VALUES_OF)
|
||||
.replace('`', "")
|
||||
.replace(':', "");
|
||||
|
||||
output.insert(name, TypeDescription { values: enum_types });
|
||||
output.insert(name, types::TypeDescription { values: enum_types });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,3 +30,14 @@ pub fn get_object_types(content: &[MdContent]) -> HashMap<String, TypeDescriptio
|
|||
|
||||
output
|
||||
}
|
||||
|
||||
fn to_type_descriptions(table: &md_parser::Table) -> Vec<types::TypeDescriptions> {
|
||||
table
|
||||
.rows
|
||||
.iter()
|
||||
.map(|row| types::TypeDescriptions {
|
||||
value: row.columns[0].to_string(),
|
||||
description: row.columns[1].to_string(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::{md_parser::MdContent, parser::types};
|
||||
use crate::{md_parser, parser::types};
|
||||
|
||||
pub fn get_parameters(content: &[MdContent]) -> Option<Vec<types::Type>> {
|
||||
pub fn parse_parameters(content: &[md_parser::MdContent]) -> Option<Vec<types::Type>> {
|
||||
let mut it = content
|
||||
.iter()
|
||||
.skip_while(|row| match row {
|
||||
MdContent::Asterix(content) | MdContent::Text(content) => {
|
||||
md_parser::MdContent::Asterix(content) | md_parser::MdContent::Text(content) => {
|
||||
!content.starts_with("Parameters:")
|
||||
}
|
||||
_ => true,
|
||||
|
@ -17,7 +17,7 @@ pub fn get_parameters(content: &[MdContent]) -> Option<Vec<types::Type>> {
|
|||
.skip(2);
|
||||
|
||||
let parameter_table = match it.next() {
|
||||
Some(MdContent::Table(table)) => table,
|
||||
Some(md_parser::MdContent::Table(table)) => table,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
|
@ -27,7 +27,16 @@ pub fn get_parameters(content: &[MdContent]) -> Option<Vec<types::Type>> {
|
|||
let table = parameter_table
|
||||
.rows
|
||||
.iter()
|
||||
.flat_map(|row| {
|
||||
.flat_map(|row| parse_parameter(row, &type_map))
|
||||
.collect();
|
||||
|
||||
Some(table)
|
||||
}
|
||||
|
||||
fn parse_parameter(
|
||||
row: &md_parser::TableRow,
|
||||
type_map: &HashMap<String, types::TypeDescription>,
|
||||
) -> Option<types::Type> {
|
||||
let description = row.columns.get(2).cloned();
|
||||
|
||||
match &row.columns.get(2) {
|
||||
|
@ -35,12 +44,8 @@ pub fn get_parameters(content: &[MdContent]) -> Option<Vec<types::Type>> {
|
|||
Some(desc) if desc.contains("default: ") => {
|
||||
// type defines a variable as default if it contains: _optional_
|
||||
let name_with_optional = format!("{} {}", row.columns[0], types::OPTIONAL);
|
||||
types::Type::from(&row.columns[1], &name_with_optional, description, &type_map)
|
||||
types::Type::from(&row.columns[1], &name_with_optional, description, type_map)
|
||||
}
|
||||
_ => types::Type::from(&row.columns[1], &row.columns[0], description, &type_map),
|
||||
_ => types::Type::from(&row.columns[1], &row.columns[0], description, type_map),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Some(table)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
md_parser::MdContent,
|
||||
parser::{object_types::get_object_types, types, ReturnType, ReturnTypeParameter},
|
||||
parser::{object_types::parse_object_types, types, ReturnType, ReturnTypeParameter},
|
||||
};
|
||||
|
||||
pub fn get_return_type(content: &[MdContent]) -> Option<ReturnType> {
|
||||
|
@ -18,7 +18,7 @@ pub fn get_return_type(content: &[MdContent]) -> Option<ReturnType> {
|
|||
_ => None,
|
||||
})?;
|
||||
|
||||
let types = get_object_types(content);
|
||||
let types = parse_object_types(content);
|
||||
|
||||
let parameters = table
|
||||
.rows
|
||||
|
|
Loading…
Reference in New Issue
Block a user