use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Pagination {
pub page: u32,
pub per_page: u32,
pub total: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl Pagination {
pub fn new(page: u32, per_page: u32, total: u32) -> Self {
let total_pages = total.div_ceil(per_page); Self {
page,
per_page,
total,
total_pages,
has_next: page < total_pages,
has_prev: page > 1,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PaginatedResponse<T> {
pub items: Vec<T>,
pub pagination: Pagination,
}
impl<T> PaginatedResponse<T> {
pub fn new(items: Vec<T>, pagination: Pagination) -> Self {
Self { items, pagination }
}
pub fn has_more(&self) -> bool {
self.pagination.has_next
}
pub fn next_page(&self) -> Option<u32> {
if self.pagination.has_next {
Some(self.pagination.page + 1)
} else {
None
}
}
pub fn prev_page(&self) -> Option<u32> {
if self.pagination.has_prev {
Some(self.pagination.page - 1)
} else {
None
}
}
}