@@ -2,9 +2,11 @@ use crate::api::ing::{Ing, IngSendFrom, IngType};
2
2
use crate :: infra:: http:: { body_or_err, RequestBuilderExt } ;
3
3
use crate :: infra:: json;
4
4
use crate :: infra:: result:: IntoResult ;
5
+ use crate :: infra:: vec:: VecExt ;
5
6
use crate :: openapi;
6
7
use anyhow:: Result ;
7
8
use serde:: { Deserialize , Serialize } ;
9
+ use std:: ops:: ControlFlow ;
8
10
9
11
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
10
12
pub struct IngEntry {
@@ -58,13 +60,15 @@ pub struct IngCommentEntry {
58
60
pub user_guid : String ,
59
61
}
60
62
63
+ type IngEntryWithComment = ( IngEntry , Vec < IngCommentEntry > ) ;
64
+
61
65
impl Ing {
62
66
pub async fn get_list (
63
67
& self ,
64
68
skip : usize ,
65
69
take : usize ,
66
70
ing_type : & IngType ,
67
- ) -> Result < Vec < ( IngEntry , Vec < IngCommentEntry > ) > > {
71
+ ) -> Result < Vec < IngEntryWithComment > > {
68
72
let client = & reqwest:: Client :: new ( ) ;
69
73
70
74
let range = ( skip + 1 ) ..=( skip + take) ;
@@ -80,18 +84,34 @@ impl Ing {
80
84
let body = body_or_err ( resp) . await ?;
81
85
82
86
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 ?;
84
91
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
+ }
87
96
} ;
88
97
89
98
entry_with_comment. into_ok :: < anyhow:: Error > ( )
90
99
} ) ;
91
100
92
- futures:: future:: join_all ( fut_iter)
101
+ let cf = futures:: future:: join_all ( fut_iter)
93
102
. await
94
103
. 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
+ }
96
116
}
97
117
}
0 commit comments