actix_web_pagination/
pagination_config.rs1use crate::Pagination;
2
3#[derive(Debug, Clone, Copy)]
5pub struct PaginationConfig {
6 pub(crate) page_name: &'static str,
7 pub(crate) per_page_name: &'static str,
8 pub(crate) default_per_page: i32,
9 pub(crate) max_per_page: i32,
10}
11
12impl PaginationConfig {
13 pub(crate) const DEFAULT_PER_PAGE: u32 = 20;
14 pub(crate) const DEFAULT: Self = PaginationConfig {
15 page_name: "page",
16 per_page_name: "per_page",
17 default_per_page: Self::DEFAULT_PER_PAGE as _,
18 max_per_page: 100,
19 };
20
21 pub(crate) fn pagination(&self) -> Pagination {
22 Pagination {
23 per_page: self.default_per_page as _,
24 page: 0,
25 }
26 }
27
28 pub(crate) fn parse_page(&self, text: &str) -> i32 {
29 text.parse().unwrap_or(1).max(1) - 1
30 }
31
32 pub(crate) fn parse_per_page(&self, text: &str) -> i32 {
33 text.parse()
34 .unwrap_or(self.default_per_page)
35 .max(self.default_per_page)
36 .min(self.max_per_page)
37 }
38
39 pub fn page_name(mut self, page_name: &'static str) -> Self {
41 self.page_name = page_name;
42 self
43 }
44
45 pub fn per_page_name(mut self, per_page_name: &'static str) -> Self {
47 self.per_page_name = per_page_name;
48 self
49 }
50
51 pub fn default_per_page(mut self, default_per_page: u32) -> Self {
53 debug_assert!(default_per_page >= Self::DEFAULT_PER_PAGE);
54 self.default_per_page = default_per_page as _;
55 self
56 }
57
58 pub fn max_per_page(mut self, max_per_page: u32) -> Self {
60 debug_assert!(max_per_page >= Self::DEFAULT_PER_PAGE);
61 self.max_per_page = max_per_page as _;
62 self
63 }
64}
65
66impl Pagination {
67 pub fn config() -> PaginationConfig {
69 PaginationConfig::DEFAULT
70 }
71}