Move methods to TokenTree

This commit is contained in:
Joel Wachsler 2022-07-19 20:53:55 +00:00
parent 47b9701321
commit bc8d014bc3
8 changed files with 162 additions and 157 deletions

View File

@ -1,7 +1,9 @@
use crate::md_parser; use crate::md_parser;
pub fn parse_group_description(content: &[md_parser::MdContent]) -> Option<String> { impl md_parser::TokenTree {
let return_desc = content pub fn parse_group_description(&self) -> Option<String> {
let return_desc = self
.content
.iter() .iter()
.map(|row| row.inner_value_as_string()) .map(|row| row.inner_value_as_string())
.collect::<Vec<String>>() .collect::<Vec<String>>()
@ -14,4 +16,5 @@ pub fn parse_group_description(content: &[md_parser::MdContent]) -> Option<Strin
} else { } else {
Some(return_desc) Some(return_desc)
} }
}
} }

View File

@ -1,7 +1,9 @@
use crate::md_parser::MdContent; use crate::md_parser::{self, MdContent};
pub fn parse_method_description(content: &[MdContent]) -> Option<String> { impl md_parser::TokenTree {
let return_desc = content pub fn parse_method_description(&self) -> Option<String> {
let return_desc = self
.content
.iter() .iter()
// skip until we get to the "Returns:" text // skip until we get to the "Returns:" text
.skip_while(|row| match row { .skip_while(|row| match row {
@ -32,4 +34,5 @@ pub fn parse_method_description(content: &[MdContent]) -> Option<String> {
} else { } else {
Some(return_desc) Some(return_desc)
} }
}
} }

View File

@ -2,15 +2,9 @@ mod description;
mod return_type; mod return_type;
mod url; mod url;
use std::collections::HashMap;
use crate::{md_parser, parser::util, types}; use crate::{md_parser, parser::util, types};
pub use return_type::ReturnType; pub use return_type::ReturnType;
use std::collections::HashMap;
use self::{
description::parse_method_description, return_type::parse_return_type, url::get_method_url,
};
#[derive(Debug)] #[derive(Debug)]
pub struct ApiMethod { pub struct ApiMethod {
@ -61,11 +55,11 @@ impl ApiMethod {
fn new(child: &md_parser::TokenTree, name: &str) -> Self { fn new(child: &md_parser::TokenTree, name: &str) -> Self {
let tables = Tables::from(child); let tables = Tables::from(child);
let method_description = parse_method_description(&child.content); let method_description = child.parse_method_description();
let return_type = parse_return_type(&child.content); let return_type = child.parse_return_type();
// let return_type = tables.return_type().map(|r| ReturnType::new(r)); // let return_type = tables.return_type().map(|r| ReturnType::new(r));
let parameters = tables.parameters().map(ApiParameters::new); let parameters = tables.parameters().map(ApiParameters::new);
let method_url = get_method_url(&child.content); let method_url = child.get_method_url();
ApiMethod { ApiMethod {
name: name.to_string(), name: name.to_string(),

View File

@ -11,8 +11,10 @@ pub struct ReturnType {
pub parameters: Vec<ReturnTypeParameter>, pub parameters: Vec<ReturnTypeParameter>,
} }
pub fn parse_return_type(content: &[MdContent]) -> Option<ReturnType> { impl md_parser::TokenTree {
let table = content pub fn parse_return_type(&self) -> Option<ReturnType> {
let table = self
.content
.iter() .iter()
// The response is a ... <-- Trying to find this line // The response is a ... <-- Trying to find this line
// <-- The next line is empty // <-- The next line is empty
@ -26,7 +28,7 @@ pub fn parse_return_type(content: &[MdContent]) -> Option<ReturnType> {
_ => None, _ => None,
})?; })?;
let types = parse_object_types(content); let types = self.parse_object_types();
let parameters = table let parameters = table
.rows .rows
@ -44,26 +46,26 @@ pub fn parse_return_type(content: &[MdContent]) -> Option<ReturnType> {
}) })
.collect(); .collect();
let is_list = content Some(ReturnType {
parameters,
is_list: self.is_list(),
})
}
fn is_list(&self) -> bool {
self.content
.iter() .iter()
.find_map(|row| match row { .find_map(|row| match row {
MdContent::Text(text) if text.starts_with("The response is a") => Some(text), MdContent::Text(text) if text.starts_with("The response is a") => Some(text),
_ => None, _ => None,
}) })
.map(|found| found.contains("array")) .map(|found| found.contains("array"))
.unwrap_or_else(|| false); .unwrap_or_else(|| false)
}
Some(ReturnType { pub fn parse_object_types(&self) -> HashMap<String, types::TypeDescription> {
parameters,
is_list,
})
}
pub fn parse_object_types(
content: &[md_parser::MdContent],
) -> HashMap<String, types::TypeDescription> {
let mut output = HashMap::new(); let mut output = HashMap::new();
let mut content_it = content.iter(); let mut content_it = self.content.iter();
while let Some(entry) = content_it.next() { while let Some(entry) = content_it.next() {
if let md_parser::MdContent::Text(content) = entry { if let md_parser::MdContent::Text(content) = entry {
@ -86,6 +88,7 @@ pub fn parse_object_types(
} }
output output
}
} }
fn to_type_descriptions(table: &md_parser::Table) -> Vec<types::TypeDescriptions> { fn to_type_descriptions(table: &md_parser::Table) -> Vec<types::TypeDescriptions> {

View File

@ -1,9 +1,11 @@
use crate::{md_parser, parser::util}; use crate::{md_parser, parser::util};
pub fn get_method_url(content: &[md_parser::MdContent]) -> String { impl md_parser::TokenTree {
pub fn get_method_url(&self) -> String {
const START: &str = "Name: "; const START: &str = "Name: ";
util::find_content_starts_with(content, START) util::find_content_starts_with(&self.content, START)
.map(|text| text.trim_start_matches(START).trim_matches('`').to_string()) .map(|text| text.trim_start_matches(START).trim_matches('`').to_string())
.expect("Could find method url") .expect("Could find method url")
}
} }

View File

@ -4,7 +4,6 @@ mod url;
use crate::md_parser; use crate::md_parser;
use self::{description::parse_group_description, url::get_group_url};
pub use method::*; pub use method::*;
#[derive(Debug)] #[derive(Debug)]
@ -15,25 +14,29 @@ pub struct ApiGroup {
pub url: String, pub url: String,
} }
pub fn parse_api_group(tree: &md_parser::TokenTree) -> ApiGroup { impl ApiGroup {
let methods = tree.children.iter().flat_map(ApiMethod::try_new).collect(); pub fn new(tree: &md_parser::TokenTree) -> ApiGroup {
ApiGroup {
name: tree.name(),
methods: tree.methods(),
description: tree.parse_group_description(),
url: tree.get_group_url(),
}
}
}
let group_description = parse_group_description(&tree.content); impl md_parser::TokenTree {
let group_url = get_group_url(&tree.content); fn name(&self) -> String {
self.title
let name = tree
.title
.clone() .clone()
.unwrap() .unwrap()
.to_lowercase() .to_lowercase()
.trim_end_matches("(experimental)") .trim_end_matches("(experimental)")
.trim() .trim()
.replace(' ', "_"); .replace(' ', "_")
}
ApiGroup { fn methods(&self) -> Vec<ApiMethod> {
name, self.children.iter().flat_map(ApiMethod::try_new).collect()
methods,
description: group_description,
url: group_url,
} }
} }

View File

@ -2,8 +2,9 @@ use regex::Regex;
use crate::{md_parser, parser::util}; use crate::{md_parser, parser::util};
pub fn get_group_url(content: &[md_parser::MdContent]) -> String { impl md_parser::TokenTree {
let row = util::find_content_contains(content, "API methods are under") pub fn get_group_url(&self) -> String {
let row = util::find_content_contains(&self.content, "API methods are under")
.expect("Could not find api method"); .expect("Could not find api method");
let re = Regex::new(r#"All (?:\w+\s?)+ API methods are under "(\w+)", e.g."#) let re = Regex::new(r#"All (?:\w+\s?)+ API methods are under "(\w+)", e.g."#)
@ -11,4 +12,5 @@ pub fn get_group_url(content: &[md_parser::MdContent]) -> String {
let res = re.captures(&row).expect("Failed find capture"); let res = re.captures(&row).expect("Failed find capture");
res[1].to_string() res[1].to_string()
}
} }

View File

@ -1,7 +1,5 @@
use crate::{md_parser, types}; use crate::{md_parser, types};
use self::group::parse_api_group;
mod group; mod group;
mod util; mod util;
@ -19,10 +17,7 @@ pub fn parse_api_groups(token_tree: md_parser::TokenTree) -> Vec<ApiGroup> {
} }
pub fn parse_groups(trees: Vec<md_parser::TokenTree>) -> Vec<ApiGroup> { pub fn parse_groups(trees: Vec<md_parser::TokenTree>) -> Vec<ApiGroup> {
trees trees.iter().map(ApiGroup::new).collect()
.into_iter()
.map(|tree| parse_api_group(&tree))
.collect()
} }
fn extract_relevant_parts(tree: md_parser::TokenTree) -> Vec<md_parser::TokenTree> { fn extract_relevant_parts(tree: md_parser::TokenTree) -> Vec<md_parser::TokenTree> {