Skip to content

Commit e4458d5

Browse files
committed
refactor: list ing
1 parent d680e68 commit e4458d5

File tree

9 files changed

+59
-53
lines changed

9 files changed

+59
-53
lines changed

src/api/ing/get_comment_list.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub struct IngCommentEntry {
2828
pub user_guid: String,
2929
}
3030

31-
3231
impl Ing {
3332
pub async fn get_comment_list(&self, ing_id: usize) -> Result<Vec<IngCommentEntry>> {
3433
let client = reqwest::Client::new();

src/api/ing/get_list.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::openapi;
88
use anyhow::Result;
99
use serde::{Deserialize, Serialize};
1010
use std::ops::ControlFlow;
11-
use crate::api::ing::get_comment_list::IngCommentEntry;
1211

1312
#[derive(Clone, Debug, Serialize, Deserialize)]
1413
pub struct IngEntry {
@@ -40,15 +39,13 @@ pub struct IngEntry {
4039
pub icons: String,
4140
}
4241

43-
type IngEntryWithComment = (IngEntry, Vec<IngCommentEntry>);
44-
4542
impl Ing {
4643
pub async fn get_list(
4744
&self,
4845
skip: usize,
4946
take: usize,
5047
ing_type: &IngType,
51-
) -> Result<Vec<IngEntryWithComment>> {
48+
) -> Result<Vec<IngEntry>> {
5249
let client = &reqwest::Client::new();
5350

5451
let range = (skip + 1)..=(skip + take);
@@ -64,27 +61,15 @@ impl Ing {
6461

6562
let body = body_or_err(resp).await?;
6663

67-
let entry_with_comment = {
68-
match json::deserialize::<Vec<IngEntry>>(&body)?.pop() {
69-
Some(entry) => {
70-
let id = entry.id;
71-
let comment_vec = self.get_comment_list(id).await?;
72-
73-
ControlFlow::Continue((entry, comment_vec))
74-
}
75-
None => ControlFlow::Break(()),
76-
}
77-
};
78-
79-
entry_with_comment.into_ok::<anyhow::Error>()
64+
json::deserialize::<Vec<IngEntry>>(&body)?.pop().into_ok()
8065
})
8166
.join_all()
8267
.await
8368
.into_iter()
8469
.try_fold(vec![], |acc, it| match it {
85-
Ok(cf) => match cf {
86-
ControlFlow::Continue(it) => ControlFlow::Continue(acc.chain_push(it)),
87-
_ => ControlFlow::Break(Ok(acc)),
70+
Ok(maybe) => match maybe {
71+
Some(entry) => ControlFlow::Continue(acc.chain_push(entry)),
72+
None => ControlFlow::Break(Ok(acc)),
8873
},
8974
Err(e) => ControlFlow::Break(Err(e)),
9075
});

src/api/post/get_comment_list.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use crate::api::post::Post;
2+
use crate::api::user::User;
13
use crate::infra::http::{body_or_err, RequestBuilderExt};
24
use crate::infra::json;
35
use crate::infra::result::IntoResult;
46
use crate::openapi;
57
use anyhow::Result;
68
use serde::{Deserialize, Serialize};
7-
use crate::api::post::Post;
8-
use crate::api::user::User;
99

1010
#[derive(Clone, Debug, Serialize, Deserialize)]
1111
pub struct PostCommentEntry {
@@ -31,7 +31,11 @@ impl Post {
3131
let client = reqwest::Client::new();
3232

3333
let req = {
34-
let url = openapi!("https://api.cnblogs.com/api/blogs/{}/posts/{}/comments", blog_app, post_id);
34+
let url = openapi!(
35+
"https://api.cnblogs.com/api/blogs/{}/posts/{}/comments",
36+
blog_app,
37+
post_id
38+
);
3539
client.get(url).pat_auth(&self.pat)
3640
};
3741
let resp = req.send().await?;
@@ -43,4 +47,4 @@ impl Post {
4347

4448
entry_vec.into_ok()
4549
}
46-
}
50+
}

src/api/post/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
pub mod create;
22
pub mod del_one;
3+
pub mod get_comment_list;
34
pub mod get_count;
45
pub mod get_meta_list;
56
pub mod get_one;
67
pub mod get_one_raw;
78
pub mod search;
89
pub mod update;
9-
pub mod get_comment_list;
1010

1111
pub struct Post {
1212
pat: String,

src/display/colorful.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::api::ing::get_list::{ IngEntry};
1+
use crate::api::ing::get_comment_list::IngCommentEntry;
2+
use crate::api::ing::get_list::IngEntry;
23
use crate::api::ing::{
34
fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom,
45
};
@@ -15,7 +16,6 @@ use std::fmt::Display;
1516
use std::ops::Not;
1617
use std::path::PathBuf;
1718
use unicode_width::UnicodeWidthStr;
18-
use crate::api::ing::get_comment_list::IngCommentEntry;
1919

2020
pub fn login(cfg_path: &Result<PathBuf>) {
2121
match cfg_path {
@@ -51,13 +51,17 @@ pub fn user_info(info: &Result<UserInfo>) {
5151
}
5252
}
5353

54-
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
55-
let ing_list = match ing_list {
54+
pub fn list_ing(
55+
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
56+
rev: bool,
57+
align: bool,
58+
) {
59+
let ing_with_comment_list = match ing_with_comment_list {
5660
Ok(o) => o,
5761
Err(e) => return println_err(e),
5862
};
5963

60-
ing_list
64+
ing_with_comment_list
6165
.iter()
6266
.dyn_rev(rev)
6367
.for_each(|(ing, comment_list)| {

src/display/json.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::api::ing::get_list::{ IngEntry};
1+
use crate::api::ing::get_comment_list::IngCommentEntry;
2+
use crate::api::ing::get_list::IngEntry;
23
use crate::api::news::get_list::NewsEntry;
34
use crate::api::post::get_one::PostEntry;
45
use crate::api::user::info::UserInfo;
@@ -8,7 +9,6 @@ use anyhow::Result;
89
use serde::Serialize;
910
use serde_json::json;
1011
use std::path::PathBuf;
11-
use crate::api::ing::get_comment_list::IngCommentEntry;
1212

1313
pub fn login(cfg_path: &Result<PathBuf>) {
1414
let json = cfg_path.as_ref().map(|pb| json!({"cfg_path":pb}));
@@ -24,13 +24,13 @@ pub fn user_info(info: &Result<UserInfo>) {
2424
println_result(info);
2525
}
2626

27-
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
28-
let ing_list = match ing_list {
27+
pub fn list_ing(ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
28+
let ing_with_comment_list = match ing_with_comment_list {
2929
Ok(o) => o,
3030
Err(e) => return println_err(e),
3131
};
3232

33-
let vec = ing_list
33+
let json_vec = ing_with_comment_list
3434
.iter()
3535
.dyn_rev(rev)
3636
.map(|(entry, comment_list)| {
@@ -41,8 +41,7 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
4141
})
4242
.collect::<Vec<_>>();
4343

44-
let json =
45-
json::serialize(vec.clone()).unwrap_or_else(|_| panic!("Can not serialize: {:?}", vec));
44+
let json = json::serialize(json_vec).expect("Can not serialize json_vec");
4645
print!("{}", json);
4746
}
4847

src/display/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::api::ing::get_list::{IngEntry};
1+
use crate::api::ing::get_comment_list::IngCommentEntry;
2+
use crate::api::ing::get_list::IngEntry;
23
use crate::api::news::get_list::NewsEntry;
34
use crate::api::post::get_one::PostEntry;
45
use crate::api::user::info::UserInfo;
56
use crate::args::Style;
67
use anyhow::Result;
78
use std::path::PathBuf;
8-
use crate::api::ing::get_comment_list::IngCommentEntry;
99

1010
mod colorful;
1111
mod json;
@@ -37,14 +37,14 @@ pub fn user_info(style: &Style, user_info: &Result<UserInfo>) {
3737

3838
pub fn list_ing(
3939
style: &Style,
40-
ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
40+
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
4141
rev: bool,
4242
align: bool,
4343
) {
4444
match style {
45-
Style::Colorful => colorful::list_ing(ing_list, rev, align),
46-
Style::Normal => normal::list_ing(ing_list, rev, align),
47-
Style::Json => json::list_ing(ing_list, rev),
45+
Style::Colorful => colorful::list_ing(ing_with_comment_list, rev, align),
46+
Style::Normal => normal::list_ing(ing_with_comment_list, rev, align),
47+
Style::Json => json::list_ing(ing_with_comment_list, rev),
4848
}
4949
}
5050

src/display/normal.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::api::ing::get_list::{ IngEntry};
1+
use crate::api::ing::get_comment_list::IngCommentEntry;
2+
use crate::api::ing::get_list::IngEntry;
23
use crate::api::ing::{
34
fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom,
45
};
@@ -15,7 +16,6 @@ use std::fmt::Display;
1516
use std::ops::Not;
1617
use std::path::PathBuf;
1718
use unicode_width::UnicodeWidthStr;
18-
use crate::api::ing::get_comment_list::IngCommentEntry;
1919

2020
pub fn login(cfg_path: &Result<PathBuf>) {
2121
match cfg_path {
@@ -51,13 +51,17 @@ pub fn user_info(info: &Result<UserInfo>) {
5151
}
5252
}
5353

54-
pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
55-
let ing_list = match ing_list {
54+
pub fn list_ing(
55+
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
56+
rev: bool,
57+
align: bool,
58+
) {
59+
let ing_with_comment_list = match ing_with_comment_list {
5660
Ok(o) => o,
5761
Err(e) => return println_err(e),
5862
};
5963

60-
ing_list
64+
ing_with_comment_list
6165
.iter()
6266
.dyn_rev(rev)
6367
.for_each(|(ing, comment_list)| {

src/main.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::api::user::User;
1313
use crate::args::parser::no_operation;
1414
use crate::args::{parser, Args};
1515
use crate::infra::fp::currying::eq;
16+
use crate::infra::iter::IntoIteratorExt;
1617
use crate::infra::option::OptionExt;
1718
use crate::infra::result::IntoResult;
1819
use anyhow::Result;
@@ -69,11 +70,21 @@ async fn main() -> Result<()> {
6970
quiet.not().then(|| display::user_info(style, &user_info));
7071
}
7172
_ if let Some((skip, take, r#type, align)) = parser::list_ing(&args) => {
72-
let ing_vec = try {
73-
Ing::new(pat?).get_list(skip, take, &r#type).await?
73+
let ing_with_comment_list = try {
74+
let ing_api = Ing::new(pat?);
75+
let ing_vec = ing_api.get_list(skip, take, &r#type).await?;
76+
ing_vec.into_iter()
77+
.map(|ing| async {
78+
let result = ing_api.get_comment_list(ing.id).await;
79+
result.map(|comment_vec| (ing, comment_vec))
80+
})
81+
.join_all()
82+
.await
83+
.into_iter()
84+
.collect::<Result<Vec<_>>>()?
7485
};
75-
foe.then(|| panic_if_err(&ing_vec));
76-
quiet.not().then(|| display::list_ing(style, &ing_vec, rev, align));
86+
foe.then(|| panic_if_err(&ing_with_comment_list));
87+
quiet.not().then(|| display::list_ing(style, &ing_with_comment_list, rev, align));
7788
}
7889
_ if let Some(content) = parser::publish_ing(&args) => {
7990
let content = try {

0 commit comments

Comments
 (0)