diff --git a/src/api/ing/get_comment_list.rs b/src/api/ing/get_comment_list.rs index f30a0a9..c107442 100644 --- a/src/api/ing/get_comment_list.rs +++ b/src/api/ing/get_comment_list.rs @@ -28,7 +28,6 @@ pub struct IngCommentEntry { pub user_guid: String, } - impl Ing { pub async fn get_comment_list(&self, ing_id: usize) -> Result> { let client = reqwest::Client::new(); diff --git a/src/api/ing/get_list.rs b/src/api/ing/get_list.rs index 122e04c..b09f14d 100644 --- a/src/api/ing/get_list.rs +++ b/src/api/ing/get_list.rs @@ -8,7 +8,6 @@ use crate::openapi; use anyhow::Result; use serde::{Deserialize, Serialize}; use std::ops::ControlFlow; -use crate::api::ing::get_comment_list::IngCommentEntry; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct IngEntry { @@ -40,15 +39,13 @@ pub struct IngEntry { pub icons: String, } -type IngEntryWithComment = (IngEntry, Vec); - impl Ing { pub async fn get_list( &self, skip: usize, take: usize, ing_type: &IngType, - ) -> Result> { + ) -> Result> { let client = &reqwest::Client::new(); let range = (skip + 1)..=(skip + take); @@ -64,27 +61,15 @@ impl Ing { let body = body_or_err(resp).await?; - let entry_with_comment = { - match json::deserialize::>(&body)?.pop() { - Some(entry) => { - let id = entry.id; - let comment_vec = self.get_comment_list(id).await?; - - ControlFlow::Continue((entry, comment_vec)) - } - None => ControlFlow::Break(()), - } - }; - - entry_with_comment.into_ok::() + json::deserialize::>(&body)?.pop().into_ok() }) .join_all() .await .into_iter() .try_fold(vec![], |acc, it| match it { - Ok(cf) => match cf { - ControlFlow::Continue(it) => ControlFlow::Continue(acc.chain_push(it)), - _ => ControlFlow::Break(Ok(acc)), + Ok(maybe) => match maybe { + Some(entry) => ControlFlow::Continue(acc.chain_push(entry)), + None => ControlFlow::Break(Ok(acc)), }, Err(e) => ControlFlow::Break(Err(e)), }); diff --git a/src/api/post/get_comment_list.rs b/src/api/post/get_comment_list.rs index f29dbc6..7721abb 100644 --- a/src/api/post/get_comment_list.rs +++ b/src/api/post/get_comment_list.rs @@ -1,11 +1,11 @@ +use crate::api::post::Post; +use crate::api::user::User; use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::json; use crate::infra::result::IntoResult; use crate::openapi; use anyhow::Result; use serde::{Deserialize, Serialize}; -use crate::api::post::Post; -use crate::api::user::User; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct PostCommentEntry { @@ -31,7 +31,11 @@ impl Post { let client = reqwest::Client::new(); let req = { - let url = openapi!("https://api.cnblogs.com/api/blogs/{}/posts/{}/comments", blog_app, post_id); + let url = openapi!( + "https://api.cnblogs.com/api/blogs/{}/posts/{}/comments", + blog_app, + post_id + ); client.get(url).pat_auth(&self.pat) }; let resp = req.send().await?; @@ -43,4 +47,4 @@ impl Post { entry_vec.into_ok() } -} \ No newline at end of file +} diff --git a/src/api/post/mod.rs b/src/api/post/mod.rs index d4d590a..81a80b9 100644 --- a/src/api/post/mod.rs +++ b/src/api/post/mod.rs @@ -1,12 +1,12 @@ pub mod create; pub mod del_one; +pub mod get_comment_list; pub mod get_count; pub mod get_meta_list; pub mod get_one; pub mod get_one_raw; pub mod search; pub mod update; -pub mod get_comment_list; pub struct Post { pat: String, diff --git a/src/display/colorful.rs b/src/display/colorful.rs index 6847056..f76b87b 100644 --- a/src/display/colorful.rs +++ b/src/display/colorful.rs @@ -1,4 +1,5 @@ -use crate::api::ing::get_list::{ IngEntry}; +use crate::api::ing::get_comment_list::IngCommentEntry; +use crate::api::ing::get_list::IngEntry; use crate::api::ing::{ fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom, }; @@ -15,7 +16,6 @@ use std::fmt::Display; use std::ops::Not; use std::path::PathBuf; use unicode_width::UnicodeWidthStr; -use crate::api::ing::get_comment_list::IngCommentEntry; pub fn login(cfg_path: &Result) { match cfg_path { @@ -51,13 +51,17 @@ pub fn user_info(info: &Result) { } } -pub fn list_ing(ing_list: &Result)>>, rev: bool, align: bool) { - let ing_list = match ing_list { +pub fn list_ing( + ing_with_comment_list: &Result)>>, + rev: bool, + align: bool, +) { + let ing_with_comment_list = match ing_with_comment_list { Ok(o) => o, Err(e) => return println_err(e), }; - ing_list + ing_with_comment_list .iter() .dyn_rev(rev) .for_each(|(ing, comment_list)| { diff --git a/src/display/json.rs b/src/display/json.rs index 3ad4b1f..08e8802 100644 --- a/src/display/json.rs +++ b/src/display/json.rs @@ -1,4 +1,5 @@ -use crate::api::ing::get_list::{ IngEntry}; +use crate::api::ing::get_comment_list::IngCommentEntry; +use crate::api::ing::get_list::IngEntry; use crate::api::news::get_list::NewsEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; @@ -8,7 +9,6 @@ use anyhow::Result; use serde::Serialize; use serde_json::json; use std::path::PathBuf; -use crate::api::ing::get_comment_list::IngCommentEntry; pub fn login(cfg_path: &Result) { let json = cfg_path.as_ref().map(|pb| json!({"cfg_path":pb})); @@ -24,13 +24,13 @@ pub fn user_info(info: &Result) { println_result(info); } -pub fn list_ing(ing_list: &Result)>>, rev: bool) { - let ing_list = match ing_list { +pub fn list_ing(ing_with_comment_list: &Result)>>, rev: bool) { + let ing_with_comment_list = match ing_with_comment_list { Ok(o) => o, Err(e) => return println_err(e), }; - let vec = ing_list + let json_vec = ing_with_comment_list .iter() .dyn_rev(rev) .map(|(entry, comment_list)| { @@ -41,8 +41,7 @@ pub fn list_ing(ing_list: &Result)>>, rev: b }) .collect::>(); - let json = - json::serialize(vec.clone()).unwrap_or_else(|_| panic!("Can not serialize: {:?}", vec)); + let json = json::serialize(json_vec).expect("Can not serialize json_vec"); print!("{}", json); } diff --git a/src/display/mod.rs b/src/display/mod.rs index 359cea8..cd68785 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -1,11 +1,11 @@ -use crate::api::ing::get_list::{IngEntry}; +use crate::api::ing::get_comment_list::IngCommentEntry; +use crate::api::ing::get_list::IngEntry; use crate::api::news::get_list::NewsEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::args::Style; use anyhow::Result; use std::path::PathBuf; -use crate::api::ing::get_comment_list::IngCommentEntry; mod colorful; mod json; @@ -37,14 +37,14 @@ pub fn user_info(style: &Style, user_info: &Result) { pub fn list_ing( style: &Style, - ing_list: &Result)>>, + ing_with_comment_list: &Result)>>, rev: bool, align: bool, ) { match style { - Style::Colorful => colorful::list_ing(ing_list, rev, align), - Style::Normal => normal::list_ing(ing_list, rev, align), - Style::Json => json::list_ing(ing_list, rev), + Style::Colorful => colorful::list_ing(ing_with_comment_list, rev, align), + Style::Normal => normal::list_ing(ing_with_comment_list, rev, align), + Style::Json => json::list_ing(ing_with_comment_list, rev), } } diff --git a/src/display/normal.rs b/src/display/normal.rs index 0aa90cc..99bdc32 100644 --- a/src/display/normal.rs +++ b/src/display/normal.rs @@ -1,4 +1,5 @@ -use crate::api::ing::get_list::{ IngEntry}; +use crate::api::ing::get_comment_list::IngCommentEntry; +use crate::api::ing::get_list::IngEntry; use crate::api::ing::{ fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom, }; @@ -15,7 +16,6 @@ use std::fmt::Display; use std::ops::Not; use std::path::PathBuf; use unicode_width::UnicodeWidthStr; -use crate::api::ing::get_comment_list::IngCommentEntry; pub fn login(cfg_path: &Result) { match cfg_path { @@ -51,13 +51,17 @@ pub fn user_info(info: &Result) { } } -pub fn list_ing(ing_list: &Result)>>, rev: bool, align: bool) { - let ing_list = match ing_list { +pub fn list_ing( + ing_with_comment_list: &Result)>>, + rev: bool, + align: bool, +) { + let ing_with_comment_list = match ing_with_comment_list { Ok(o) => o, Err(e) => return println_err(e), }; - ing_list + ing_with_comment_list .iter() .dyn_rev(rev) .for_each(|(ing, comment_list)| { diff --git a/src/main.rs b/src/main.rs index 70b78e1..527ea30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use crate::api::user::User; use crate::args::parser::no_operation; use crate::args::{parser, Args}; use crate::infra::fp::currying::eq; +use crate::infra::iter::IntoIteratorExt; use crate::infra::option::OptionExt; use crate::infra::result::IntoResult; use anyhow::Result; @@ -69,11 +70,21 @@ async fn main() -> Result<()> { quiet.not().then(|| display::user_info(style, &user_info)); } _ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => { - let ing_vec = try { - Ing::new(pat?).get_list(skip, take, &r#type).await? + let ing_with_comment_list = try { + let ing_api = Ing::new(pat?); + let ing_vec = ing_api.get_list(skip, take, &r#type).await?; + ing_vec.into_iter() + .map(|ing| async { + let result = ing_api.get_comment_list(ing.id).await; + result.map(|comment_vec| (ing, comment_vec)) + }) + .join_all() + .await + .into_iter() + .collect::>>()? }; - foe.then(|| panic_if_err(&ing_vec)); - quiet.not().then(|| display::list_ing(style, &ing_vec, rev, align)); + foe.then(|| panic_if_err(&ing_with_comment_list)); + quiet.not().then(|| display::list_ing(style, &ing_with_comment_list, rev, align)); } _ if let Some(content) = parser::publish_ing(&args) => { let content = try {