qrush_engine/utils/
pagination.rs1use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Clone)]
5pub struct Pagination {
6 pub page: usize,
7 pub limit: usize,
8 pub total: usize,
9 pub has_prev: bool,
10 pub has_next: bool,
11}
12
13impl Pagination {
14 pub fn new(page: usize, limit: usize, total: usize) -> Self {
15 let has_prev = page > 1;
16 let has_next = page * limit < total;
17
18 Pagination {
19 page,
20 limit,
21 total,
22 has_prev,
23 has_next,
24 }
25 }
26
27 pub fn offset(&self) -> usize {
28 (self.page.saturating_sub(1)) * self.limit
29 }
30}
31
32#[derive(Debug, Deserialize)]
33pub struct PaginationQuery {
34 pub page: Option<usize>,
35 pub limit: Option<usize>,
36}
37
38impl PaginationQuery {
39 pub fn into_pagination(self, total: usize) -> Pagination {
41 let page = self.page.unwrap_or(1).max(1); let limit = self.limit.unwrap_or(10).max(1); Pagination::new(page, limit, total)
44 }
45}