Skip to main content

Module pagination

Module pagination 

Source
Expand description

Pagination result types (Page<T>, CursorPage<T>) and an RFC 8288 Link header builder.

Not tied to the model layer or any feature flag — build one by hand if your data source isn’t [crate::model::QueryBuilder] (an external API, an in-memory Vec, …). QueryBuilder::paginate() / ::paginate_after() (require a model-* feature) are the batteries-included way to get one directly from a database query.

§Offset pagination

use rust_web_server::pagination::Page;

let page = Page::new(vec!["a", "b", "c"], 1, 10, 25);
assert_eq!(3, page.total_pages);
assert!(page.has_next());
assert!(!page.has_prev());

let link = page.link_header("https://api.example.com/items").unwrap();
assert!(link.contains(r#"<https://api.example.com/items?page=2&per_page=10>; rel="next""#));
assert!(link.contains(r#"rel="last""#));

§Cursor (keyset) pagination

use rust_web_server::pagination::CursorPage;

let page = CursorPage { items: vec!["a", "b"], next_cursor: Some("42".to_string()) };
assert!(page.has_next());
let link = page.link_header("https://api.example.com/items", "cursor").unwrap();
assert_eq!(r#"<https://api.example.com/items?cursor=42>; rel="next""#, link);

Structs§

CursorPage
A single page of cursor (keyset) paginated results.
Page
A single page of offset-paginated results (LIMIT/OFFSET-style).