qrush_engine/utils/
pagination.rs

1// src/utils/pagination.rs
2use 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    /// Converts to a full Pagination struct using total count and default values.
40    pub fn into_pagination(self, total: usize) -> Pagination {
41        let page = self.page.unwrap_or(1).max(1);      // default to 1
42        let limit = self.limit.unwrap_or(10).max(1);   // default to 10
43        Pagination::new(page, limit, total)
44    }
45}