14 releases

0.6.0 Apr 18, 2024
0.5.1 Aug 11, 2023
0.5.0 Jun 29, 2023
0.1.2 Jan 25, 2023
0.1.0 Dec 26, 2022

#804 in Encoding

33 downloads per month

MIT license

20KB
492 lines

Pegination

Implementation of cursor pagination. See the tests for details.

Requirements

1. Structure SourceRequest

Source request sent to API

#[derive(Debug, Clone)]
struct SourceRequest {}

2. Structure Filters

Data source filters, excludes limit and offset

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
struct Filters {}

3. Structure Item

Data source entity

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
struct Item {}

4. Entity Error

enum AppError {}

Required implementations

1. SourceRequest to Request<Filters> transormation

impl TryFrom<SourceRequest> for Request<Filters> {}

2. DataSource

trait DataSource<Item, Filters: Clone + PartialEq, OffsetValue: Clone>

Usage

Having all of the stuff described above, you can use pagination function this way:

const DEFAULT_LIMIT: u16 = 100;

let paginated_data = pagination::<Item, Filters, OffsetValue, AppError>(data_source, source_request, DEFAULT_LIMIT).await?;
// {
//   "last_cursor": null,
//   "has_next_page": false,
//   "data": [
//     ..items
//   ]
// }

Dependencies

~3–9MB
~85K SLoC