Rocket
The paginator-rocket crate provides request guards and responders for Rocket.
Installation
Section titled “Installation”[dependencies]paginator-rocket = "0.2.2"rocket = { version = "0.5", features = ["json"] }Pagination Request Guard
Section titled “Pagination Request Guard”The Pagination guard automatically extracts pagination parameters from the request URI:
use rocket::{get, routes};use paginator_rocket::{Pagination, PaginatedJson};use serde::Serialize;
#[derive(Serialize)]struct User { id: u32, name: String,}
#[get("/users")]async fn get_users(pagination: Pagination) -> PaginatedJson<User> { let users = vec![ User { id: 1, name: "Alice".to_string() }, User { id: 2, name: "Bob".to_string() }, ];
PaginatedJson::new(users, &pagination.params, 100)}
#[launch]fn rocket() -> _ { rocket::build().mount("/api", routes![get_users])}Query Parameters
Section titled “Query Parameters”The guard parses from the URI query string:
GET /api/users?page=2&per_page=20&sort_by=name&sort_direction=asc| Parameter | Type | Default |
|---|---|---|
page | u32 | 1 |
per_page | u32 | 20 |
sort_by | String | - |
sort_direction | String | - |
PaginatedJson Responder
Section titled “PaginatedJson Responder”PaginatedJson serializes the response and adds pagination headers:
PaginatedJson::new(data, ¶ms, total_count)Helper Function
Section titled “Helper Function”Use create_paginated_response for custom response building:
use paginator_rocket::create_paginated_response;
let response = create_paginated_response(users, ¶ms, 100);