File tree 3 files changed +34
-2
lines changed
3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -277,6 +277,35 @@ impl<T> Paginated<T> {
277
277
Ok ( Some ( opts) )
278
278
}
279
279
280
+ pub ( crate ) fn prev_seek_params < S , F > ( & self , f : F ) -> AppResult < Option < IndexMap < String , String > > >
281
+ where
282
+ F : Fn ( & T ) -> S ,
283
+ S : Serialize ,
284
+ {
285
+ // When the data size is smaller than the page size, we would expect the prev page to be
286
+ // unavailable during backward pagination but available during forward pagination.
287
+ if self . options . is_explicit ( )
288
+ || self . records_and_total . is_empty ( )
289
+ || ( self . records_and_total . len ( ) < self . options . per_page as usize
290
+ && self . options . is_backward ( ) )
291
+ {
292
+ return Ok ( None ) ;
293
+ }
294
+
295
+ // We also like to return None for prev page when it's the first forward pagination.
296
+ let mut opts = IndexMap :: new ( ) ;
297
+ match self . options . page {
298
+ Page :: Unspecified => return Ok ( None ) ,
299
+ Page :: Seek ( ref raw) if raw. is_empty ( ) => return Ok ( None ) ,
300
+ Page :: Seek ( _) | Page :: SeekBackward ( _) => {
301
+ let seek = f ( & self . records_and_total . first ( ) . unwrap ( ) . record ) ;
302
+ opts. insert ( "seek" . into ( ) , format ! ( "-{}" , encode_seek( seek) ?) ) ;
303
+ }
304
+ Page :: Numeric ( _) => unreachable ! ( ) ,
305
+ } ;
306
+ Ok ( Some ( opts) )
307
+ }
308
+
280
309
pub ( crate ) fn iter ( & self ) -> impl Iterator < Item = & T > {
281
310
self . records_and_total . iter ( ) . map ( |row| & row. record )
282
311
}
Original file line number Diff line number Diff line change @@ -245,7 +245,8 @@ pub async fn list_crates(
245
245
data. total ( ) ,
246
246
data. next_seek_params ( |last| seek. to_payload ( last) ) ?
247
247
. map ( |p| req. query_with_params ( p) ) ,
248
- None ,
248
+ data. prev_seek_params ( |first| seek. to_payload ( first) ) ?
249
+ . map ( |p| req. query_with_params ( p) ) ,
249
250
data. into_iter ( ) . collect :: < Vec < _ > > ( ) ,
250
251
)
251
252
} else {
Original file line number Diff line number Diff line change @@ -1077,7 +1077,9 @@ async fn seek_based_pagination() -> anyhow::Result<()> {
1077
1077
assert_eq ! ( resp. meta. total, 0 ) ;
1078
1078
}
1079
1079
1080
- assert_eq ! ( resp. meta. prev_page, None ) ;
1080
+ if calls == 1 {
1081
+ assert_eq ! ( resp. meta. prev_page, None ) ;
1082
+ }
1081
1083
}
1082
1084
1083
1085
assert_eq ! ( calls, 4 ) ;
You can’t perform that action at this time.
0 commit comments