diff --git a/src/api/ing/get_list.rs b/src/api/ing/get_list.rs index 46c0e1a..d914a9c 100644 --- a/src/api/ing/get_list.rs +++ b/src/api/ing/get_list.rs @@ -2,9 +2,11 @@ use crate::api::ing::{Ing, IngSendFrom, IngType}; use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::json; use crate::infra::result::IntoResult; +use crate::infra::vec::VecExt; use crate::openapi; use anyhow::Result; use serde::{Deserialize, Serialize}; +use std::ops::ControlFlow; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct IngEntry { @@ -58,13 +60,15 @@ pub struct IngCommentEntry { pub user_guid: 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); @@ -80,18 +84,34 @@ impl Ing { let body = body_or_err(resp).await?; let entry_with_comment = { - let [entry, ..] = json::deserialize::<[IngEntry; 1]>(&body)?; + match json::deserialize::>(&body)?.pop() { + Some(entry) => { + let id = entry.id; + let comment_vec = self.get_comment_list(id).await?; - let id = entry.id; - (entry, self.get_comment_list(id).await?) + ControlFlow::Continue((entry, comment_vec)) + } + None => ControlFlow::Break(()), + } }; entry_with_comment.into_ok::() }); - futures::future::join_all(fut_iter) + let cf = futures::future::join_all(fut_iter) .await .into_iter() - .collect() + .try_fold(vec![], |acc, it| match it { + Ok(cf) => match cf { + ControlFlow::Continue(it) => ControlFlow::Continue(acc.chain_push(it)), + _ => ControlFlow::Break(Ok(acc)), + }, + Err(e) => ControlFlow::Break(Err(e)), + }); + + match cf { + ControlFlow::Continue(vec) => Ok(vec), + ControlFlow::Break(result) => result, + } } } diff --git a/src/infra/mod.rs b/src/infra/mod.rs index e48939d..2f553c9 100644 --- a/src/infra/mod.rs +++ b/src/infra/mod.rs @@ -5,3 +5,4 @@ pub mod json; pub mod option; pub mod result; pub mod time; +pub mod vec; diff --git a/src/infra/vec.rs b/src/infra/vec.rs new file mode 100644 index 0000000..3226781 --- /dev/null +++ b/src/infra/vec.rs @@ -0,0 +1,14 @@ +pub trait VecExt { + fn chain_push(self, item: T) -> Vec; +} + +impl VecExt for Vec +where + T: Clone, +{ + #[inline] + fn chain_push(mut self, item: T) -> Self { + self.push(item); + self + } +}