pub const DEFAULT_PER_PAGE: usize = 50;
pub const MAX_PER_PAGE: usize = 100;
pub const PER_PAGE_CHOICES: [usize; 3] = [25, 50, 100];
#[must_use]
pub fn clamp_per_page(requested: Option<usize>) -> usize {
match requested {
None | Some(0) => DEFAULT_PER_PAGE,
Some(n) => PER_PAGE_CHOICES
.into_iter()
.find(|&choice| n <= choice)
.unwrap_or(MAX_PER_PAGE),
}
}
#[must_use]
pub fn context(page: i64, per_page: usize, total: usize) -> serde_json::Value {
let paginator = rustango::pagination::Paginator::new(total, per_page);
let total_pages = paginator.num_pages().max(1) as i64;
let current = page.clamp(1, total_pages);
let start = if total == 0 {
0
} else {
(current - 1) * per_page as i64 + 1
};
let end = (current * per_page as i64).min(total as i64);
serde_json::json!({
"page": current,
"per_page": per_page,
"total": total,
"total_pages": total_pages,
"start_index": start,
"end_index": end,
"has_prev": current > 1,
"has_next": current < total_pages,
"prev_page": (current - 1).max(1),
"next_page": (current + 1).min(total_pages),
"pages": (1..=total_pages).collect::<Vec<i64>>(),
"per_page_choices": PER_PAGE_CHOICES,
"show": total_pages > 1
|| total > PER_PAGE_CHOICES[0]
|| per_page != DEFAULT_PER_PAGE,
})
}
#[must_use]
pub fn offset(page: i64, per_page: usize) -> i64 {
(page.max(1) - 1) * per_page as i64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn per_page_is_clamped_to_the_ceiling() {
assert_eq!(clamp_per_page(None), DEFAULT_PER_PAGE);
assert_eq!(clamp_per_page(Some(0)), DEFAULT_PER_PAGE);
assert_eq!(clamp_per_page(Some(25)), 25);
assert_eq!(clamp_per_page(Some(100_000)), MAX_PER_PAGE);
}
#[test]
fn odd_sizes_snap_to_an_offered_choice() {
assert_eq!(clamp_per_page(Some(2)), 25);
assert_eq!(clamp_per_page(Some(26)), 50);
assert_eq!(clamp_per_page(Some(51)), 100);
for n in PER_PAGE_CHOICES {
assert_eq!(clamp_per_page(Some(n)), n, "offered sizes pass through");
}
}
#[test]
fn the_size_control_never_hides_itself() {
let c = context(1, 100, 6);
assert_eq!(c["total_pages"], 1);
assert!(
c["show"].as_bool().unwrap(),
"picking a large size must not remove the size selector"
);
assert!(!context(1, DEFAULT_PER_PAGE, 6)["show"].as_bool().unwrap());
}
#[test]
fn out_of_range_page_clamps_to_the_last_one() {
let c = context(99, 50, 120);
assert_eq!(c["page"], 3, "120 items / 50 = 3 pages");
assert!(!c["has_next"].as_bool().unwrap());
assert!(c["has_prev"].as_bool().unwrap());
}
#[test]
fn indices_describe_the_visible_slice() {
let c = context(2, 50, 120);
assert_eq!(c["start_index"], 51);
assert_eq!(c["end_index"], 100);
let last = context(3, 50, 120);
assert_eq!(last["start_index"], 101);
assert_eq!(last["end_index"], 120);
}
#[test]
fn an_empty_list_is_still_page_one() {
let c = context(1, 50, 0);
assert_eq!(c["page"], 1);
assert_eq!(c["total_pages"], 1);
assert_eq!(c["start_index"], 0);
assert_eq!(c["end_index"], 0);
assert!(
!c["show"].as_bool().unwrap(),
"no controls for an empty list"
);
}
#[test]
fn offsets_follow_the_page() {
assert_eq!(offset(1, 50), 0);
assert_eq!(offset(3, 50), 100);
assert_eq!(offset(0, 50), 0, "page 0 is treated as page 1");
}
}