Page Hunter
Page Hunter library is a Rust-based pagination tool that provides a way to manage and navigate through pages of data. It offers a set of resources that encapsulates all the necessary pagination information such as the current page, total pages, previous page, next page and the items on the current page.
The library also includes validation methods to ensure the integrity of the pagination data. It's designed to be flexible and easy to integrate into any Rust project that requires pagination functionality and standard data validation.
CRATE FEATURES
serde: Add Serialize and Deserialize support forPageandBookbased on serde. This feature is useful for implementing pagination models as a request or response body in REST APIs, among other implementations.utoipa: Add ToSchema support forPageandBookbased on utoipa. This feature is useful for generating OpenAPI schemas for pagination models. This feature depends on theserdefeature and therefore you only need to implementutoipato get both.sqlx: Add support for pagination with SQLx for Postgres, MySQL and SQLite databases.
BASIC OPERATION
The page-hunter library provides two main models to manage pagination:
Page: Represents a page of records with the current page, total pages, previous page, next page, and the items on the current page.Book: Represents a book of pages with a collection ofPageinstances.
The library also provides a set of functions to paginate records into a Page model and bind records into a Book model. The following examples show how to use the page-hunter library:
Paginate records:
If you need to paginate records and get a specific Page:
use ;
let records: = vec!;
let page: usize = 0;
let size: usize = 2;
// Using the paginate_records function:
let page_model: = match paginate_records ;
// Using RecordsPagination trait:
let page_model: = match records.paginate ;
To create a new instance of a Page from known parameters:
use ;
let items: = vec!;
let page: usize = 0;
let size: usize = 2;
let total_elements: usize = 5;
let page_model_result: = new;
On feature serde enabled, you can serialize and deserialize a Page as follows:
use Page;
let items: = vec!;
let page: usize = 0;
let size: usize = 2;
let total_elements: usize = 5;
let page_model: = new.unwrap_or_else;
let serialized_page: String = to_string
.unwrap_or_else;
let deserialized_page: = from_str
.unwrap_or_else;
When you create a new Page instance from the constructor or deserialization, the following rules are validated for the fields on the page:
- pages must be equal to total divided by size rounded up. When size is 0, pages must be 1.
- page must be less than or equal to pages - 1.
- if page is less than pages - 1, items length must be equal to size.
- if page is equal to pages - 1, total must be equal to (pages - 1) * size + items length.
- previous_page must be equal to page - 1 if page is greater than 0, otherwise it must be
None. - next_page must be equal to page + 1 if page is less than pages - 1, otherwise it must be
None.
If any of these rules are violated, a PaginationError will be returned.
Bind records:
If you need to bind records into a Book model:
use ;
let records: = vec!;
let size: usize = 2;
// Using the bind_records function:
let book: = match bind_records ;
// Using RecordsPagination trait:
let book: = match records.bind ;
To create a new Book instance from known parameters:
use ;
let sheets: = vec!;
let book: = new;
On feature serde enabled, you can serialize and deserialize a Book as follows:
use ;
let sheets: = vec!;
let book: = new;
let serialized_book: String = to_string
.unwrap_or_else;
let deserialized_book: = from_str
.unwrap_or_else;
Generate OpenAPI schemas:
On feature utoipa enabled, you can generate OpenAPI schemas for Page and Book models as follows:
use ;
use ;
use ;
pub type PeoplePage = ;
pub type PeopleBook = ;
;
Take a look at the examples folder where you can find practical uses in REST API implementations with some web frameworks.
Paginate records from a relational database with SQLx:
To paginate records from a Postgres database:
use ;
use ;
use ;
async
Similar to using pagination for Postgres, SQLxPagination can be used for MySQL and SQLite. If you are working with a connection pool, you can Acquire a single connection before running paginate.
DEVELOPMENT
To test page-hunter, follow these recommendations:
Set env variables:
Create local.env file at workspace folder to store the required environment variables. For example,
DB_HOST=localhost
DB_USER=test
DB_PASSWORD=docker
DB_NAME=test
PG_DB_PORT=5432
PG_MIGRATIONS_PATH=page-hunter/src/pagination/sqlx/tests/pg/migrations
Install required tools:
Install the following tools required for the development process.
SQLx client for Postgres:
Cargo LLVM cov:
Cargo Nextest:
Cargo Deny:
Setup databases:
Run Postgres database as a Docker container:
Run database migrations:
- Run migrations:
- Revert migrations:
To format the code:
To verify the code format:
To verify lints:
- No features:
- Feature
serde:
- Feature
utoipa:
- Feature
sqlx:
- All features:
To check the project:
- No features:
- Feature
serde:
- Feature
utoipa:
- Feature
sqlx:
- All features:
To generate the documentation:
To run doc tests:
To test using llvm-cov:
Security analysis:
CONTRIBUTIONS
The Page Hunter project is open source and therefore any interested software developer can contribute to its improvement. To contribute, take a look at the following recommendations:
- Bug Reports: If you find a bug, please create an issue detailing the problem, the steps to reproduce it, and the expected behavior.
- Feature Requests: If you have an idea for a new feature or an enhancement to an existing one, please create an issue describing your idea.
- Pull Requests: If you've fixed a bug or implemented a new feature, we'd love to see your work! Please submit a pull request. Make sure your code follows the existing style and all tests pass.