Move methods to associated methods
This commit is contained in:
parent
a72d3e5fa7
commit
47b9701321
|
@ -16,6 +16,4 @@ serde_json = "1.0.82"
|
||||||
thiserror = "1.0.31"
|
thiserror = "1.0.31"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = ["qbittorrent-web-api-gen"]
|
||||||
"qbittorrent-web-api-gen",
|
|
||||||
]
|
|
||||||
|
|
|
@ -49,39 +49,47 @@ impl ApiParameters {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_api_method(child: &md_parser::TokenTree) -> Option<ApiMethod> {
|
impl ApiMethod {
|
||||||
util::find_content_starts_with(&child.content, "Name: ")
|
pub fn try_new(child: &md_parser::TokenTree) -> Option<Self> {
|
||||||
.map(|name| {
|
const NAME: &str = "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 {
|
child
|
||||||
let tables = child.to_tables();
|
.find_content_starts_with(NAME)
|
||||||
let method_description = parse_method_description(&child.content);
|
.map(|name| name.trim_start_matches(NAME).trim_matches('`').to_string())
|
||||||
let return_type = parse_return_type(&child.content);
|
.map(|name| ApiMethod::new(child, &name))
|
||||||
let parameters = tables.parameters().map(ApiParameters::new);
|
}
|
||||||
let method_url = get_method_url(&child.content);
|
|
||||||
|
|
||||||
ApiMethod {
|
fn new(child: &md_parser::TokenTree, name: &str) -> Self {
|
||||||
name: name.to_string(),
|
let tables = Tables::from(child);
|
||||||
description: method_description,
|
let method_description = parse_method_description(&child.content);
|
||||||
parameters,
|
let return_type = parse_return_type(&child.content);
|
||||||
return_type,
|
// let return_type = tables.return_type().map(|r| ReturnType::new(r));
|
||||||
url: method_url,
|
let parameters = tables.parameters().map(ApiParameters::new);
|
||||||
|
let method_url = get_method_url(&child.content);
|
||||||
|
|
||||||
|
ApiMethod {
|
||||||
|
name: name.to_string(),
|
||||||
|
description: method_description,
|
||||||
|
parameters,
|
||||||
|
return_type,
|
||||||
|
url: method_url,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl md_parser::TokenTree {
|
impl md_parser::TokenTree {
|
||||||
fn to_tables(&self) -> Tables<'_> {
|
fn find_content_starts_with(&self, content: &str) -> Option<String> {
|
||||||
|
util::find_content_starts_with(&self.content, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a md_parser::TokenTree> for Tables<'a> {
|
||||||
|
fn from(token_tree: &'a md_parser::TokenTree) -> Self {
|
||||||
let mut tables = HashMap::new();
|
let mut tables = HashMap::new();
|
||||||
let mut prev_prev: Option<&md_parser::MdContent> = None;
|
let mut prev_prev: Option<&md_parser::MdContent> = None;
|
||||||
let mut prev: Option<&md_parser::MdContent> = None;
|
let mut prev: Option<&md_parser::MdContent> = None;
|
||||||
|
|
||||||
for content in &self.content {
|
for content in &token_tree.content {
|
||||||
if let md_parser::MdContent::Table(table) = content {
|
if let md_parser::MdContent::Table(table) = content {
|
||||||
let title = match prev_prev {
|
let title = match prev_prev {
|
||||||
Some(md_parser::MdContent::Text(text)) => text.clone(),
|
Some(md_parser::MdContent::Text(text)) => text.clone(),
|
||||||
|
@ -111,7 +119,7 @@ impl<'a> Tables<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn return_type(&self) -> Option<Vec<types::Type>> {
|
// fn return_type(&self) -> Option<Vec<types::Type>> {
|
||||||
// self.get_type_containing("Returns")
|
// self.get_type_containing("The response is a")
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn get_type_containing(&self, name: &str) -> Option<Vec<types::Type>> {
|
fn get_type_containing(&self, name: &str) -> Option<Vec<types::Type>> {
|
||||||
|
@ -170,7 +178,7 @@ mod tests {
|
||||||
|
|
||||||
// when
|
// when
|
||||||
let tree = TokenTreeFactory::create(input);
|
let tree = TokenTreeFactory::create(input);
|
||||||
let api_method = parse_api_method(&tree.children[0]).unwrap();
|
let api_method = ApiMethod::try_new(&tree.children[0]).unwrap();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
let api_method_as_str = format!("{api_method:#?}");
|
let api_method_as_str = format!("{api_method:#?}");
|
||||||
|
@ -187,7 +195,7 @@ mod tests {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
let input = include_str!(concat!(TEST_DIR!(), "/", $test_file, ".md"));
|
let input = include_str!(concat!(TEST_DIR!(), "/", $test_file, ".md"));
|
||||||
let tree = TokenTreeFactory::create(input);
|
let tree = ApiMethod::try_new(input);
|
||||||
let api_method = parse_api_method(&tree.children[0]).unwrap();
|
let api_method = parse_api_method(&tree.children[0]).unwrap();
|
||||||
|
|
||||||
let tree_as_str = format!("{tree:#?}");
|
let tree_as_str = format!("{tree:#?}");
|
||||||
|
|
|
@ -4,7 +4,7 @@ mod url;
|
||||||
|
|
||||||
use crate::md_parser;
|
use crate::md_parser;
|
||||||
|
|
||||||
use self::{description::parse_group_description, method::parse_api_method, url::get_group_url};
|
use self::{description::parse_group_description, url::get_group_url};
|
||||||
pub use method::*;
|
pub use method::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -16,7 +16,7 @@ pub struct ApiGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_api_group(tree: &md_parser::TokenTree) -> ApiGroup {
|
pub fn parse_api_group(tree: &md_parser::TokenTree) -> ApiGroup {
|
||||||
let methods = tree.children.iter().flat_map(parse_api_method).collect();
|
let methods = tree.children.iter().flat_map(ApiMethod::try_new).collect();
|
||||||
|
|
||||||
let group_description = parse_group_description(&tree.content);
|
let group_description = parse_group_description(&tree.content);
|
||||||
let group_url = get_group_url(&tree.content);
|
let group_url = get_group_url(&tree.content);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user