pub struct Paginated<T> {
pub items: Vec<T>,
pub total: i64,
pub limit: i64,
pub offset: i64,
}Expand description
A page of rows plus the total, for pagination links and JSON envelopes.
Query::paginate builds one with two queries (the rows and
COUNT(*)). Serializes as
{"items": [...], "total": 42, "limit": 20, "offset": 0}.
§Free plan
The count reads every matching row once more: on large tables, prefer
plain “next page” links without a total (Page::next
after Query::all) or cache the total.
§Examples
use ocre::{Page, Paginated};
let page = Paginated { items: vec!["a", "b"], total: 5, limit: 2, offset: 2 };
assert_eq!(page.current_page(), 2);
assert_eq!(page.total_pages(), 3);
assert!(page.has_next() && page.has_previous());
assert_eq!(page.next_page(), Some(Page { limit: 2, offset: 4 }));
assert_eq!(page.previous_page(), Some(Page { limit: 2, offset: 0 }));
assert_eq!(serde_json::to_string(&page).unwrap(), r#"{"items":["a","b"],"total":5,"limit":2,"offset":2}"#);Fields§
§items: Vec<T>The rows of this page.
total: i64Rows matching the query, all pages together.
limit: i64Rows per page.
offset: i64Rows skipped before this page.
Implementations§
Source§impl<T> Paginated<T>
impl<T> Paginated<T>
Sourcepub fn current_page(&self) -> i64
pub fn current_page(&self) -> i64
1-based number of this page.
§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 0, limit: 10, offset: 0 };
assert_eq!(page.current_page(), 1);Sourcepub fn total_pages(&self) -> i64
pub fn total_pages(&self) -> i64
Number of pages (at least 1, even with no row).
§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 21, limit: 10, offset: 0 };
assert_eq!(page.total_pages(), 3);Sourcepub fn has_next(&self) -> bool
pub fn has_next(&self) -> bool
Whether rows follow this page.
§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 10, limit: 10, offset: 0 };
assert!(!page.has_next());Sourcepub fn has_previous(&self) -> bool
pub fn has_previous(&self) -> bool
Whether rows come before this page.
§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 10, limit: 10, offset: 0 };
assert!(!page.has_previous());Sourcepub fn next_page(&self) -> Option<Page>
pub fn next_page(&self) -> Option<Page>
The next page, if any.
§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 3, limit: 2, offset: 2 };
assert_eq!(page.next_page(), None);Sourcepub fn previous_page(&self) -> Option<Page>
pub fn previous_page(&self) -> Option<Page>
The previous page, if any (never a negative offset).
§Examples
let page = ocre::Paginated::<()> { items: vec![], total: 30, limit: 10, offset: 5 };
assert_eq!(page.previous_page(), Some(ocre::Page { limit: 10, offset: 0 }));Trait Implementations§
Source§impl<T: PartialEq> PartialEq for Paginated<T>
impl<T: PartialEq> PartialEq for Paginated<T>
impl<T: PartialEq> StructuralPartialEq for Paginated<T>
Auto Trait Implementations§
impl<T> Freeze for Paginated<T>
impl<T> RefUnwindSafe for Paginated<T>where
T: RefUnwindSafe,
impl<T> Send for Paginated<T>where
T: Send,
impl<T> Sync for Paginated<T>where
T: Sync,
impl<T> Unpin for Paginated<T>where
T: Unpin,
impl<T> UnsafeUnpin for Paginated<T>
impl<T> UnwindSafe for Paginated<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more