From 201ba025f3a7535993630957cf6314ad3308bc49 Mon Sep 17 00:00:00 2001 From: Joel Wachsler Date: Tue, 12 Jul 2022 11:13:54 +0000 Subject: [PATCH] Extract api group and method --- .../src/parser/api_group.rs | 32 +++++++++++ .../src/parser/api_method.rs | 31 +++++++++++ .../src/parser/description.rs | 4 +- qbittorrent-web-api-gen/src/parser/mod.rs | 55 +------------------ .../src/parser/return_type.rs | 2 +- 5 files changed, 69 insertions(+), 55 deletions(-) create mode 100644 qbittorrent-web-api-gen/src/parser/api_group.rs create mode 100644 qbittorrent-web-api-gen/src/parser/api_method.rs diff --git a/qbittorrent-web-api-gen/src/parser/api_group.rs b/qbittorrent-web-api-gen/src/parser/api_group.rs new file mode 100644 index 0000000..cdb605f --- /dev/null +++ b/qbittorrent-web-api-gen/src/parser/api_group.rs @@ -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, + } +} diff --git a/qbittorrent-web-api-gen/src/parser/api_method.rs b/qbittorrent-web-api-gen/src/parser/api_method.rs new file mode 100644 index 0000000..e58dd9c --- /dev/null +++ b/qbittorrent-web-api-gen/src/parser/api_method.rs @@ -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 { + 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, + } +} diff --git a/qbittorrent-web-api-gen/src/parser/description.rs b/qbittorrent-web-api-gen/src/parser/description.rs index 0819ec2..52577ed 100644 --- a/qbittorrent-web-api-gen/src/parser/description.rs +++ b/qbittorrent-web-api-gen/src/parser/description.rs @@ -1,6 +1,6 @@ use crate::md_parser::MdContent; -pub fn get_group_description(content: &[MdContent]) -> Option { +pub fn parse_group_description(content: &[MdContent]) -> Option { let return_desc = content .iter() .map(|row| row.inner_value_as_string()) @@ -16,7 +16,7 @@ pub fn get_group_description(content: &[MdContent]) -> Option { } } -pub fn get_method_description(content: &[MdContent]) -> Option { +pub fn parse_method_description(content: &[MdContent]) -> Option { let return_desc = content .iter() // skip until we get to the "Returns:" text diff --git a/qbittorrent-web-api-gen/src/parser/mod.rs b/qbittorrent-web-api-gen/src/parser/mod.rs index d1f9b2d..c198ff6 100644 --- a/qbittorrent-web-api-gen/src/parser/mod.rs +++ b/qbittorrent-web-api-gen/src/parser/mod.rs @@ -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) -> 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 = 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::*; diff --git a/qbittorrent-web-api-gen/src/parser/return_type.rs b/qbittorrent-web-api-gen/src/parser/return_type.rs index 8c3fda3..ab7d491 100644 --- a/qbittorrent-web-api-gen/src/parser/return_type.rs +++ b/qbittorrent-web-api-gen/src/parser/return_type.rs @@ -3,7 +3,7 @@ use crate::{ parser::{object_types::parse_object_types, types, ReturnType, ReturnTypeParameter}, }; -pub fn get_return_type(content: &[MdContent]) -> Option { +pub fn parse_return_type(content: &[MdContent]) -> Option { let table = content .iter() // The response is a ... <-- Trying to find this line