Skip to content

Commit

Permalink
fix: ing list
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 14, 2023
1 parent 4dfee79 commit 9488b44
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/api/ing/get_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,13 +60,15 @@ pub struct IngCommentEntry {
pub user_guid: String,
}

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

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

let range = (skip + 1)..=(skip + take);
Expand All @@ -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::<Vec<IngEntry>>(&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::<anyhow::Error>()
});

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,
}
}
}
1 change: 1 addition & 0 deletions src/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod json;
pub mod option;
pub mod result;
pub mod time;
pub mod vec;
14 changes: 14 additions & 0 deletions src/infra/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub trait VecExt<T> {
fn chain_push(self, item: T) -> Vec<T>;
}

impl<T> VecExt<T> for Vec<T>
where
T: Clone,
{
#[inline]
fn chain_push(mut self, item: T) -> Self {
self.push(item);
self
}
}

0 comments on commit 9488b44

Please sign in to comment.