use serde::Deserialize;
#[derive(Debug, Clone)]
pub struct Page<T> {
pub items: Vec<T>,
pub current_page: i64,
pub total_pages: Option<i64>,
pub total_count: Option<i64>,
}
impl<T> Page<T> {
pub fn has_next_page(&self) -> bool {
match self.total_pages {
Some(tp) => self.current_page < tp,
None => false,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OffsetPageInfo {
pub current_page: i64,
pub total_pages: Option<i64>,
pub total_count: Option<i64>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Connection<T> {
pub edges: Vec<Edge<T>>,
pub page_info: OffsetPageInfo,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Edge<T> {
pub node: T,
}
impl<T> Connection<T> {
pub fn into_page(self) -> Page<T> {
Page {
items: self.edges.into_iter().map(|e| e.node).collect(),
current_page: self.page_info.current_page,
total_pages: self.page_info.total_pages,
total_count: self.page_info.total_count,
}
}
}