Skip to content

Commit 95b5f41

Browse files
committed
refactor: simplify impl
1 parent 9488b44 commit 95b5f41

File tree

8 files changed

+113
-94
lines changed

8 files changed

+113
-94
lines changed

src/api/ing/get_list.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::api::ing::{Ing, IngSendFrom, IngType};
22
use crate::infra::http::{body_or_err, RequestBuilderExt};
3+
use crate::infra::iter::IntoIteratorExt;
34
use crate::infra::json;
45
use crate::infra::result::IntoResult;
56
use crate::infra::vec::VecExt;
@@ -72,33 +73,33 @@ impl Ing {
7273
let client = &reqwest::Client::new();
7374

7475
let range = (skip + 1)..=(skip + take);
75-
let fut_iter = range.map(|i| async move {
76-
let req = {
77-
let url = openapi!("/statuses/@{}", ing_type.clone() as usize);
78-
let query = vec![("pageIndex", i), ("pageSize", 1)];
79-
client.get(url).query(&query).pat_auth(&self.pat)
80-
};
76+
let cf = range
77+
.map(|i| async move {
78+
let req = {
79+
let url = openapi!("/statuses/@{}", ing_type.clone() as usize);
80+
let query = vec![("pageIndex", i), ("pageSize", 1)];
81+
client.get(url).query(&query).pat_auth(&self.pat)
82+
};
8183

82-
let resp = req.send().await?;
84+
let resp = req.send().await?;
8385

84-
let body = body_or_err(resp).await?;
86+
let body = body_or_err(resp).await?;
8587

86-
let entry_with_comment = {
87-
match json::deserialize::<Vec<IngEntry>>(&body)?.pop() {
88-
Some(entry) => {
89-
let id = entry.id;
90-
let comment_vec = self.get_comment_list(id).await?;
88+
let entry_with_comment = {
89+
match json::deserialize::<Vec<IngEntry>>(&body)?.pop() {
90+
Some(entry) => {
91+
let id = entry.id;
92+
let comment_vec = self.get_comment_list(id).await?;
9193

92-
ControlFlow::Continue((entry, comment_vec))
94+
ControlFlow::Continue((entry, comment_vec))
95+
}
96+
None => ControlFlow::Break(()),
9397
}
94-
None => ControlFlow::Break(()),
95-
}
96-
};
98+
};
9799

98-
entry_with_comment.into_ok::<anyhow::Error>()
99-
});
100-
101-
let cf = futures::future::join_all(fut_iter)
100+
entry_with_comment.into_ok::<anyhow::Error>()
101+
})
102+
.join_all()
102103
.await
103104
.into_iter()
104105
.try_fold(vec![], |acc, it| match it {

src/api/ing/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod comment;
2-
pub mod create;
2+
pub mod publish;
33

44
use clap::{Parser, ValueEnum};
55
use lazy_static::lazy_static;

src/api/ing/create.rs renamed to src/api/ing/publish.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use anyhow::Result;
55
use serde_json::json;
66

77
impl Ing {
8-
// TODO: impl send from
9-
pub async fn create(&self, content: &str) -> Result<()> {
8+
pub async fn publish(&self, content: &str) -> Result<()> {
109
let client = reqwest::Client::new();
1110

1211
let req = {

src/api/news/get_list.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::api::news::News;
22
use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt};
3+
use crate::infra::iter::IntoIteratorExt;
34
use crate::infra::json;
45
use crate::infra::result::IntoResult;
56
use crate::openapi;
@@ -33,27 +34,27 @@ impl News {
3334
let client = &reqwest::Client::new();
3435

3536
let range = (skip + 1)..=(skip + take);
36-
let fut_iter = range.map(|i| async move {
37-
let req = {
38-
let url = {
39-
let query = vec![("pageIndex", i), ("pageSize", 1)].into_query_string();
40-
openapi!("/newsitems/?{}", query)
37+
range
38+
.map(|i| async move {
39+
let req = {
40+
let url = {
41+
let query = vec![("pageIndex", i), ("pageSize", 1)].into_query_string();
42+
openapi!("/newsitems/?{}", query)
43+
};
44+
client.get(url).pat_auth(&self.pat)
4145
};
42-
client.get(url).pat_auth(&self.pat)
43-
};
4446

45-
let resp = req.send().await?;
47+
let resp = req.send().await?;
4648

47-
let entry = {
48-
let body = body_or_err(resp).await?;
49-
let [entry, ..] = json::deserialize::<[NewsEntry; 1]>(&body)?;
50-
entry
51-
};
52-
53-
entry.into_ok::<anyhow::Error>()
54-
});
49+
let entry = {
50+
let body = body_or_err(resp).await?;
51+
let [entry, ..] = json::deserialize::<[NewsEntry; 1]>(&body)?;
52+
entry
53+
};
5554

56-
futures::future::join_all(fut_iter)
55+
entry.into_ok::<anyhow::Error>()
56+
})
57+
.join_all()
5758
.await
5859
.into_iter()
5960
.collect()

src/api/post/get_meta_list.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::api::post::get_one::PostEntry;
22
use crate::api::post::Post;
33
use crate::blog_backend;
44
use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt};
5+
use crate::infra::iter::IntoIteratorExt;
56
use crate::infra::json;
67
use crate::infra::result::IntoResult;
78
use anyhow::Result;
@@ -31,30 +32,30 @@ impl Post {
3132
let total_count = self.get_count().await?;
3233

3334
let range = (skip + 1)..=(skip + take).min(total_count);
34-
let fut_iter = range.map(|i| async move {
35-
let req = {
36-
let url = {
37-
let query = vec![('t', 1), ('p', i), ('s', 1)].into_query_string();
38-
blog_backend!("/posts/list?{}", query)
39-
};
40-
41-
client.get(url).pat_auth(&self.pat)
42-
};
35+
let vec = range
36+
.map(|i| async move {
37+
let req = {
38+
let url = {
39+
let query = vec![('t', 1), ('p', i), ('s', 1)].into_query_string();
40+
blog_backend!("/posts/list?{}", query)
41+
};
4342

44-
let resp = req.send().await?;
43+
client.get(url).pat_auth(&self.pat)
44+
};
4545

46-
let entry = {
47-
let body = body_or_err(resp).await?;
48-
let json = json::deserialize::<Value>(&body)?["postList"].take();
46+
let resp = req.send().await?;
4947

50-
let [entry, ..] = serde_json::from_value::<[PostEntry; 1]>(json)?;
51-
entry
52-
};
48+
let entry = {
49+
let body = body_or_err(resp).await?;
50+
let json = json::deserialize::<Value>(&body)?["postList"].take();
5351

54-
entry.into_ok()
55-
});
52+
let [entry, ..] = serde_json::from_value::<[PostEntry; 1]>(json)?;
53+
entry
54+
};
5655

57-
let vec = futures::future::join_all(fut_iter)
56+
entry.into_ok()
57+
})
58+
.join_all()
5859
.await
5960
.into_iter()
6061
.collect::<Result<Vec<_>>>();

src/api/post/search.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::api::post::Post;
22
use crate::blog_backend;
33
use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt};
4+
use crate::infra::iter::IntoIteratorExt;
45
use crate::infra::json;
56
use crate::infra::result::IntoResult;
67
use anyhow::Result;
@@ -44,43 +45,43 @@ impl Post {
4445
};
4546

4647
let range = (skip + 1)..=(skip + take).min(total_count);
47-
let fut_iter = range.map(|i| async move {
48-
let req = {
49-
let query = vec![
50-
("t", "1".to_string()),
51-
("p", i.to_string()),
52-
("s", 1.to_string()),
53-
("search", keyword.to_string()),
54-
]
55-
.into_query_string();
56-
let url = blog_backend!("/posts/list?{}", query);
57-
client.get(url).pat_auth(&self.pat)
58-
};
59-
let resp = req.send().await?;
60-
61-
let id_list = {
62-
let body = body_or_err(resp).await?;
63-
let mut json = json::deserialize::<Value>(&body)?;
64-
let post_id = {
65-
let json = json["postList"].take();
66-
let [post, ..] = serde_json::from_value::<[Value; 1]>(json)?;
67-
post["id"].as_u64().expect("as_u64 failed for `id`") as usize
48+
let id_list = range
49+
.map(|i| async move {
50+
let req = {
51+
let query = vec![
52+
("t", "1".to_string()),
53+
("p", i.to_string()),
54+
("s", 1.to_string()),
55+
("search", keyword.to_string()),
56+
]
57+
.into_query_string();
58+
let url = blog_backend!("/posts/list?{}", query);
59+
client.get(url).pat_auth(&self.pat)
6860
};
69-
let zzk_post_id_list = {
70-
let json = json["zzkSearchResult"]["postIds"].take();
71-
serde_json::from_value::<Vec<usize>>(json)
72-
}?;
61+
let resp = req.send().await?;
7362

74-
zzk_post_id_list
75-
.into_iter()
76-
.chain(iter::once(post_id))
77-
.collect::<Vec<usize>>()
78-
};
63+
let id_list = {
64+
let body = body_or_err(resp).await?;
65+
let mut json = json::deserialize::<Value>(&body)?;
66+
let post_id = {
67+
let json = json["postList"].take();
68+
let [post, ..] = serde_json::from_value::<[Value; 1]>(json)?;
69+
post["id"].as_u64().expect("as_u64 failed for `id`") as usize
70+
};
71+
let zzk_post_id_list = {
72+
let json = json["zzkSearchResult"]["postIds"].take();
73+
serde_json::from_value::<Vec<usize>>(json)
74+
}?;
7975

80-
id_list.into_ok::<anyhow::Error>()
81-
});
76+
zzk_post_id_list
77+
.into_iter()
78+
.chain(iter::once(post_id))
79+
.collect::<Vec<usize>>()
80+
};
8281

83-
let id_list = futures::future::join_all(fut_iter)
82+
id_list.into_ok::<anyhow::Error>()
83+
})
84+
.join_all()
8485
.await
8586
.into_iter()
8687
.collect::<Result<Vec<_>>>()?

src/infra/iter.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use futures::future::{join_all, JoinAll};
2+
use std::future::Future;
3+
14
pub trait IteratorExt<T>: Iterator<Item = T> {
25
#[inline]
36
fn dyn_rev<'t>(self, rev: bool) -> Box<dyn Iterator<Item = T> + 't>
@@ -13,3 +16,16 @@ pub trait IteratorExt<T>: Iterator<Item = T> {
1316
}
1417

1518
impl<T, I> IteratorExt<T> for I where I: Iterator<Item = T> {}
19+
20+
pub trait IntoIteratorExt: IntoIterator {
21+
#[inline]
22+
fn join_all(self) -> JoinAll<Self::Item>
23+
where
24+
Self::Item: Future,
25+
Self: Sized,
26+
{
27+
join_all(self)
28+
}
29+
}
30+
31+
impl<I> IntoIteratorExt for I where I: IntoIterator {}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async fn main() -> Result<()> {
7777
}
7878
_ if let Some(content) = parser::publish_ing(&args) => {
7979
let content = try {
80-
Ing::new(pat?).create(content).await?;
80+
Ing::new(pat?).publish(content).await?;
8181
content
8282
};
8383
foe.then(||panic_if_err(&content));

0 commit comments

Comments
 (0)