Skip to content

Commit

Permalink
refactor: list ing
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 19, 2023
1 parent d680e68 commit e4458d5
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 53 deletions.
1 change: 0 additions & 1 deletion src/api/ing/get_comment_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub struct IngCommentEntry {
pub user_guid: String,
}


impl Ing {
pub async fn get_comment_list(&self, ing_id: usize) -> Result<Vec<IngCommentEntry>> {
let client = reqwest::Client::new();
Expand Down
25 changes: 5 additions & 20 deletions src/api/ing/get_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -40,15 +39,13 @@ pub struct IngEntry {
pub icons: String,
}

type IngEntryWithComment = (IngEntry, Vec<IngCommentEntry>);

impl Ing {
pub async fn get_list(
&self,
skip: usize,
take: usize,
ing_type: &IngType,
) -> Result<Vec<IngEntryWithComment>> {
) -> Result<Vec<IngEntry>> {
let client = &reqwest::Client::new();

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

let body = body_or_err(resp).await?;

let entry_with_comment = {
match json::deserialize::<Vec<IngEntry>>(&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::<anyhow::Error>()
json::deserialize::<Vec<IngEntry>>(&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)),
});
Expand Down
12 changes: 8 additions & 4 deletions src/api/post/get_comment_list.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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?;
Expand All @@ -43,4 +47,4 @@ impl Post {

entry_vec.into_ok()
}
}
}
2 changes: 1 addition & 1 deletion src/api/post/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
14 changes: 9 additions & 5 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -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<PathBuf>) {
match cfg_path {
Expand Down Expand Up @@ -51,13 +51,17 @@ pub fn user_info(info: &Result<UserInfo>) {
}
}

pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
let ing_list = match ing_list {
pub fn list_ing(
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
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)| {
Expand Down
13 changes: 6 additions & 7 deletions src/display/json.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<PathBuf>) {
let json = cfg_path.as_ref().map(|pb| json!({"cfg_path":pb}));
Expand All @@ -24,13 +24,13 @@ pub fn user_info(info: &Result<UserInfo>) {
println_result(info);
}

pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool) {
let ing_list = match ing_list {
pub fn list_ing(ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, 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)| {
Expand All @@ -41,8 +41,7 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
})
.collect::<Vec<_>>();

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);
}

Expand Down
12 changes: 6 additions & 6 deletions src/display/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,14 +37,14 @@ pub fn user_info(style: &Style, user_info: &Result<UserInfo>) {

pub fn list_ing(
style: &Style,
ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
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),
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand All @@ -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<PathBuf>) {
match cfg_path {
Expand Down Expand Up @@ -51,13 +51,17 @@ pub fn user_info(info: &Result<UserInfo>) {
}
}

pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: bool, align: bool) {
let ing_list = match ing_list {
pub fn list_ing(
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
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)| {
Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Result<Vec<_>>>()?
};
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 {
Expand Down

0 comments on commit e4458d5

Please sign in to comment.