1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use thiserror::Error;
use crate::api::paged::LinkHeaderParseError;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PaginationError {
#[error("failed to parse a Link HTTP header: {}", source)]
LinkHeader {
#[from]
source: LinkHeaderParseError,
},
#[error("failed to parse a Link HTTP header URL: {}", source)]
InvalidUrl {
#[from]
source: url::ParseError,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Pagination {
All,
Limit(usize),
}
impl Default for Pagination {
fn default() -> Self {
Pagination::All
}
}
const MAX_PAGE_SIZE: usize = 100;
impl Pagination {
pub(crate) fn page_limit(self) -> usize {
match self {
Pagination::All => MAX_PAGE_SIZE,
Pagination::Limit(size) => size.min(MAX_PAGE_SIZE),
}
}
pub(crate) fn is_last_page(self, last_page_size: usize, num_results: usize) -> bool {
if last_page_size < self.page_limit() {
return true;
}
if let Pagination::Limit(limit) = self {
return limit <= num_results;
}
false
}
}
#[cfg(test)]
mod tests {
use crate::api::Pagination;
#[test]
fn pagination_default() {
assert_eq!(Pagination::default(), Pagination::All);
}
}