rustorm-core 0.1.0

Core traits, types and utilities for RustORM
Documentation
/// Результат постраничного запроса.
#[derive(Debug, Clone)]
pub struct Page<T> {
    pub items: Vec<T>,
    pub total: i64,
    pub current_page: i64,
    pub per_page: i64,
    pub total_pages: i64,
}

impl<T> Page<T> {
    pub fn new(items: Vec<T>, total: i64, current_page: i64, per_page: i64) -> Self {
        let per_page = per_page.max(1);
        let total_pages = (total + per_page - 1) / per_page;
        Self {
            items,
            total,
            current_page,
            per_page,
            total_pages,
        }
    }

    pub fn has_next(&self) -> bool {
        self.current_page < self.total_pages
    }

    pub fn has_prev(&self) -> bool {
        self.current_page > 1
    }

    pub fn next_page(&self) -> Option<i64> {
        if self.has_next() {
            Some(self.current_page + 1)
        } else {
            None
        }
    }

    pub fn prev_page(&self) -> Option<i64> {
        if self.has_prev() {
            Some(self.current_page - 1)
        } else {
            None
        }
    }

    /// Маппинг элементов без изменения метаданных пагинации.
    pub fn map<U>(self, f: impl FnMut(T) -> U) -> Page<U> {
        Page {
            items: self.items.into_iter().map(f).collect(),
            total: self.total,
            current_page: self.current_page,
            per_page: self.per_page,
            total_pages: self.total_pages,
        }
    }
}

/// Результат курсорной пагинации.
#[derive(Debug, Clone)]
pub struct CursorPage<T> {
    pub items: Vec<T>,
    pub next_cursor: Option<i64>,
    pub has_more: bool,
}