archway_bindings/pagination.rs
1use cosmwasm_schema::cw_serde;
2
3/// WASM binding version for the Cosmos SDK [`query.PageRequest`](https://github.com/cosmos/cosmos-sdk/blob/v0.45.16/proto/cosmos/base/query/v1beta1/pagination.proto),
4/// embedded in request messages for efficient pagination.
5#[cw_serde]
6#[derive(Default)]
7pub struct PageRequest {
8 key: Option<String>,
9 offset: Option<u64>,
10 limit: Option<u64>,
11 count_total: Option<bool>,
12 reverse: Option<bool>,
13}
14
15impl PageRequest {
16 pub fn new() -> Self {
17 Self::default()
18 }
19
20 /// The `key` is a value returned in `PageResponse.next_key` to begin querying the next page
21 /// most efficiently. Only one of offset or key should be set.
22 pub fn key(mut self, key: impl Into<String>) -> Self {
23 self.key = Some(key.into());
24 self
25 }
26
27 /// A numeric offset that can be used when key is unavailable. It is less efficient than using
28 /// key. Only one of offset or key should be set.
29 pub fn offset(mut self, offset: u64) -> Self {
30 self.offset = Some(offset);
31 self
32 }
33
34 /// Total number of results to be returned in the result page. If left empty, it will default
35 /// to a value to be set by each app.
36 pub fn limit(mut self, limit: u64) -> Self {
37 self.limit = Some(limit);
38 self
39 }
40
41 /// When set to `true`, indicates that the result set should include a count of the total number
42 /// of items available for pagination. `count_total` is only respected when `offset` is used.
43 /// It is ignored when `key` is set.
44 pub fn count_total(mut self) -> Self {
45 self.count_total = Some(true);
46 self
47 }
48
49 /// If set to `true`, the results will be returned in the descending order.
50 pub fn reverse(mut self) -> Self {
51 self.reverse = Some(true);
52 self
53 }
54}
55
56/// Embedded in response messages where the corresponding request message has used a [`PageRequest`].
57#[cw_serde]
58pub struct PageResponse {
59 /// Key to be passed to `PageRequest.key` to query the next page most efficiently.
60 pub next_key: Option<String>,
61 /// Total number of results available if `PageRequest.count_total` was set; its value is
62 /// `None` otherwise
63 pub total: Option<u64>,
64}