mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 15:19:58 -07:00
31 lines
806 B
Rust
31 lines
806 B
Rust
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
|
|
use crate::{Index, deser::de_unquote_usize};
|
|
|
|
#[derive(Debug, Default, Deserialize, JsonSchema)]
|
|
pub struct PaginationParam {
|
|
#[schemars(description = "Pagination index")]
|
|
#[serde(default, alias = "p", deserialize_with = "de_unquote_usize")]
|
|
pub page: Option<usize>,
|
|
}
|
|
|
|
impl PaginationParam {
|
|
const PER_PAGE: usize = 1_000;
|
|
|
|
pub fn start(&self, len: usize) -> usize {
|
|
(self.page.unwrap_or_default() * Self::PER_PAGE).clamp(0, len)
|
|
}
|
|
|
|
pub fn end(&self, len: usize) -> usize {
|
|
((self.page.unwrap_or_default() + 1) * Self::PER_PAGE).clamp(0, len)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, JsonSchema)]
|
|
pub struct PaginatedIndexParam {
|
|
pub index: Index,
|
|
#[serde(flatten)]
|
|
pub pagination: PaginationParam,
|
|
}
|