From bc5487757809a028f3a48418708c0469f4c195bf Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 10:57:14 +0000 Subject: [PATCH] Refactor --- .../parser/{group_parser => }/description.rs | 0 .../src/parser/group_parser/mod.rs | 65 ---------------- qbittorrent-web-api-gen/src/parser/mod.rs | 77 ++++++++++++++++--- .../parser/{group_parser => }/parameters.rs | 0 .../parser/{group_parser => }/return_type.rs | 0 .../parser/{group_parser => }/url_parser.rs | 0 6 files changed, 66 insertions(+), 76 deletions(-) rename qbittorrent-web-api-gen/src/parser/{group_parser => }/description.rs (100%) delete mode 100644 qbittorrent-web-api-gen/src/parser/group_parser/mod.rs rename qbittorrent-web-api-gen/src/parser/{group_parser => }/parameters.rs (100%) rename qbittorrent-web-api-gen/src/parser/{group_parser => }/return_type.rs (100%) rename qbittorrent-web-api-gen/src/parser/{group_parser => }/url_parser.rs (100%) diff --git a/qbittorrent-web-api-gen/src/parser/group_parser/description.rs b/qbittorrent-web-api-gen/src/parser/description.rs similarity index 100% rename from qbittorrent-web-api-gen/src/parser/group_parser/description.rs rename to qbittorrent-web-api-gen/src/parser/description.rs diff --git a/qbittorrent-web-api-gen/src/parser/group_parser/mod.rs b/qbittorrent-web-api-gen/src/parser/group_parser/mod.rs deleted file mode 100644 index 7b8d799..0000000 --- a/qbittorrent-web-api-gen/src/parser/group_parser/mod.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::md_parser::TokenTree; - -use self::{parameters::get_parameters, return_type::get_return_type}; - -use super::{util, ApiGroup, ApiMethod}; - -mod description; -mod parameters; -mod return_type; -mod url_parser; - -pub fn parse_groups(trees: Vec) -> Vec { - trees.into_iter().map(parse_api_group).collect() -} - -fn parse_api_group(tree: 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: TokenTree) -> Option { - 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: &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 method_url = url_parser::get_method_url(&child.content); - - ApiMethod { - name: name.to_string(), - description: method_description, - parameters, - return_type, - url: method_url, - } -} diff --git a/qbittorrent-web-api-gen/src/parser/mod.rs b/qbittorrent-web-api-gen/src/parser/mod.rs index 93104a5..ebca428 100644 --- a/qbittorrent-web-api-gen/src/parser/mod.rs +++ b/qbittorrent-web-api-gen/src/parser/mod.rs @@ -1,14 +1,14 @@ -mod group_parser; +use crate::{md_parser, types}; + +use self::{parameters::get_parameters, return_type::get_return_type, url_parser::get_method_url}; + +mod description; mod object_types; +mod parameters; +mod return_type; +mod url_parser; mod util; -use group_parser::parse_groups; - -use crate::{ - md_parser::{self, TokenTree}, - types, -}; - #[derive(Debug)] pub struct ApiGroup { pub name: String, @@ -39,8 +39,8 @@ pub struct ReturnTypeParameter { pub return_type: types::Type, } -fn extract_relevant_parts(tree: TokenTree) -> Vec { - let relevant: Vec = tree +fn extract_relevant_parts(tree: md_parser::TokenTree) -> Vec { + let relevant: Vec = tree .children .into_iter() .skip_while(|row| match &row.title { @@ -62,12 +62,67 @@ pub fn parse_api_groups(content: &str) -> Vec { ))) } +pub fn parse_groups(trees: Vec) -> Vec { + 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 { + 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 = get_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::*; use std::fs; - fn parse() -> TokenTree { + fn parse() -> md_parser::TokenTree { let content = include_str!("../../api-4_1.md"); let md_tree = md_parser::TokenTreeFactory::create(content); diff --git a/qbittorrent-web-api-gen/src/parser/group_parser/parameters.rs b/qbittorrent-web-api-gen/src/parser/parameters.rs similarity index 100% rename from qbittorrent-web-api-gen/src/parser/group_parser/parameters.rs rename to qbittorrent-web-api-gen/src/parser/parameters.rs diff --git a/qbittorrent-web-api-gen/src/parser/group_parser/return_type.rs b/qbittorrent-web-api-gen/src/parser/return_type.rs similarity index 100% rename from qbittorrent-web-api-gen/src/parser/group_parser/return_type.rs rename to qbittorrent-web-api-gen/src/parser/return_type.rs diff --git a/qbittorrent-web-api-gen/src/parser/group_parser/url_parser.rs b/qbittorrent-web-api-gen/src/parser/url_parser.rs similarity index 100% rename from qbittorrent-web-api-gen/src/parser/group_parser/url_parser.rs rename to qbittorrent-web-api-gen/src/parser/url_parser.rs