Skip to content

Commit 9488b44

Browse files
committed
fix: ing list
1 parent 4dfee79 commit 9488b44

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/api/ing/get_list.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use crate::api::ing::{Ing, IngSendFrom, IngType};
22
use crate::infra::http::{body_or_err, RequestBuilderExt};
33
use crate::infra::json;
44
use crate::infra::result::IntoResult;
5+
use crate::infra::vec::VecExt;
56
use crate::openapi;
67
use anyhow::Result;
78
use serde::{Deserialize, Serialize};
9+
use std::ops::ControlFlow;
810

911
#[derive(Clone, Debug, Serialize, Deserialize)]
1012
pub struct IngEntry {
@@ -58,13 +60,15 @@ pub struct IngCommentEntry {
5860
pub user_guid: String,
5961
}
6062

63+
type IngEntryWithComment = (IngEntry, Vec<IngCommentEntry>);
64+
6165
impl Ing {
6266
pub async fn get_list(
6367
&self,
6468
skip: usize,
6569
take: usize,
6670
ing_type: &IngType,
67-
) -> Result<Vec<(IngEntry, Vec<IngCommentEntry>)>> {
71+
) -> Result<Vec<IngEntryWithComment>> {
6872
let client = &reqwest::Client::new();
6973

7074
let range = (skip + 1)..=(skip + take);
@@ -80,18 +84,34 @@ impl Ing {
8084
let body = body_or_err(resp).await?;
8185

8286
let entry_with_comment = {
83-
let [entry, ..] = json::deserialize::<[IngEntry; 1]>(&body)?;
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?;
8491

85-
let id = entry.id;
86-
(entry, self.get_comment_list(id).await?)
92+
ControlFlow::Continue((entry, comment_vec))
93+
}
94+
None => ControlFlow::Break(()),
95+
}
8796
};
8897

8998
entry_with_comment.into_ok::<anyhow::Error>()
9099
});
91100

92-
futures::future::join_all(fut_iter)
101+
let cf = futures::future::join_all(fut_iter)
93102
.await
94103
.into_iter()
95-
.collect()
104+
.try_fold(vec![], |acc, it| match it {
105+
Ok(cf) => match cf {
106+
ControlFlow::Continue(it) => ControlFlow::Continue(acc.chain_push(it)),
107+
_ => ControlFlow::Break(Ok(acc)),
108+
},
109+
Err(e) => ControlFlow::Break(Err(e)),
110+
});
111+
112+
match cf {
113+
ControlFlow::Continue(vec) => Ok(vec),
114+
ControlFlow::Break(result) => result,
115+
}
96116
}
97117
}

src/infra/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub mod json;
55
pub mod option;
66
pub mod result;
77
pub mod time;
8+
pub mod vec;

src/infra/vec.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub trait VecExt<T> {
2+
fn chain_push(self, item: T) -> Vec<T>;
3+
}
4+
5+
impl<T> VecExt<T> for Vec<T>
6+
where
7+
T: Clone,
8+
{
9+
#[inline]
10+
fn chain_push(mut self, item: T) -> Self {
11+
self.push(item);
12+
self
13+
}
14+
}

0 commit comments

Comments
 (0)