use crate::Cursor;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Edge<R, V> {
node: R,
cursor: Cursor<V>,
}
impl<R, V> Edge<R, V> {
#[inline]
#[must_use]
pub const fn new(node: R, cursor: Cursor<V>) -> Self {
Self { node, cursor }
}
#[inline]
#[must_use]
pub const fn node(&self) -> &R {
&self.node
}
#[inline]
#[must_use]
pub const fn cursor(&self) -> &Cursor<V> {
&self.cursor
}
#[inline]
#[must_use]
pub fn into_parts(self) -> (R, Cursor<V>) {
(self.node, self.cursor)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Page<R, V> {
edges: Box<[Edge<R, V>]>,
has_next: bool,
has_previous: bool,
}
impl<R, V> Page<R, V> {
#[inline]
#[must_use]
pub fn new(edges: impl Into<Box<[Edge<R, V>]>>, has_next: bool, has_previous: bool) -> Self {
Self {
edges: edges.into(),
has_next,
has_previous,
}
}
#[inline]
#[must_use]
pub fn edges(&self) -> &[Edge<R, V>] {
&self.edges
}
#[inline]
#[must_use]
pub fn into_edges(self) -> Box<[Edge<R, V>]> {
self.edges
}
#[inline]
#[must_use]
pub const fn has_next_page(&self) -> bool {
self.has_next
}
#[inline]
#[must_use]
pub const fn has_previous_page(&self) -> bool {
self.has_previous
}
#[inline]
#[must_use]
pub fn start_cursor(&self) -> Option<&Cursor<V>> {
self.edges.first().map(Edge::cursor)
}
#[inline]
#[must_use]
pub fn end_cursor(&self) -> Option<&Cursor<V>> {
self.edges.last().map(Edge::cursor)
}
#[inline]
#[must_use]
pub const fn len(&self) -> usize {
self.edges.len()
}
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
self.edges.is_empty()
}
}
#[cfg(test)]
mod tests;