use super::TpfQueryParams;
pub const DEFAULT_PAGE_SIZE: usize = 100;
pub const MAX_PAGE_SIZE: usize = 10_000;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PaginationParams {
pub page: usize,
pub page_size: usize,
}
impl PaginationParams {
pub fn from_params(params: &TpfQueryParams) -> Self {
Self {
page: params.page.unwrap_or(1).max(1),
page_size: params
.page_size
.unwrap_or(DEFAULT_PAGE_SIZE)
.clamp(1, MAX_PAGE_SIZE),
}
}
pub fn offset(&self) -> usize {
(self.page - 1) * self.page_size
}
}
impl Default for PaginationParams {
fn default() -> Self {
Self {
page: 1,
page_size: DEFAULT_PAGE_SIZE,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PaginationMetadata {
pub current_page: usize,
pub page_size: usize,
pub total_count: usize,
pub total_pages: usize,
pub has_next: bool,
pub has_previous: bool,
}
impl PaginationMetadata {
pub fn new(params: &PaginationParams, total_count: usize) -> Self {
let effective_size = params.page_size.max(1);
let total_pages = total_count.div_ceil(effective_size);
Self {
current_page: params.page,
page_size: params.page_size,
total_count,
total_pages,
has_next: params.page < total_pages,
has_previous: params.page > 1,
}
}
}