anycms-core 0.4.2

A unified API response library supporting multiple Rust web frameworks
Documentation
use serde::{Deserialize, Serialize};

/// Pagination metadata for list responses
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResultPagination {
    pub total: i64,
    pub page: i64,
    pub page_size: i64,
    pub current_page: i64,
}

impl ResultPagination {
    /// Create new pagination metadata
    pub fn new(total: i64, page: i64, page_size: i64) -> Self {
        Self {
            total,
            page,
            page_size,
            current_page: page,
        }
    }

    /// Compute total number of pages
    pub fn total_pages(&self) -> i64 {
        if self.page_size <= 0 {
            return 0;
        }
        (self.total + self.page_size - 1) / self.page_size
    }

    /// Whether there is a next page
    pub fn has_next(&self) -> bool {
        self.page < self.total_pages()
    }

    /// Whether there is a previous page
    pub fn has_prev(&self) -> bool {
        self.page > 1
    }
}