use api_bones::pagination::PaginatedResponse;
pub struct FakePaginated<T> {
items: Vec<T>,
total: Option<u64>,
limit: Option<u64>,
offset: u64,
has_more: Option<bool>,
}
impl<T> FakePaginated<T> {
#[must_use]
pub fn new(items: Vec<T>) -> Self {
Self {
items,
total: None,
limit: None,
offset: 0,
has_more: None,
}
}
#[must_use]
pub fn total(mut self, total: u64) -> Self {
self.total = Some(total);
self
}
#[must_use]
pub fn limit(mut self, limit: u64) -> Self {
self.limit = Some(limit);
self
}
#[must_use]
pub fn offset(mut self, offset: u64) -> Self {
self.offset = offset;
self
}
#[must_use]
pub fn has_more(mut self, has_more: bool) -> Self {
self.has_more = Some(has_more);
self
}
#[must_use]
pub fn build(self) -> PaginatedResponse<T> {
let count = self.items.len() as u64;
let total = self.total.unwrap_or(count);
let limit = self.limit.unwrap_or(count);
let has_more = self.has_more.unwrap_or_else(|| self.offset + count < total);
PaginatedResponse {
items: self.items,
total_count: total,
has_more,
limit,
offset: self.offset,
}
}
}