use std::convert::TryInto; use serde::{Deserialize, Serialize}; /// Wrapper around progressively fetched items /// /// The paginator is a wrapper around a list of items that are fetched /// in pages from the YouTube API (e.g. playlist items, /// video recommendations or comments). #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[non_exhaustive] pub struct Paginator { /// Total number of items if finite and known. /// /// Note that this number may not be 100% accurate, as this is the /// number returned by the YouTube API at the initial fetch. /// /// It is intended to be shown to the user (e.g. 1261 comments, /// 18 Videos) and for progress estimation. /// /// Don't use this number to check if all items were fetched or for /// iterating over the items. pub count: Option, /// Content of the paginator pub items: Vec, /// The continuation token is passed to the YouTube API to fetch /// more items. /// /// If it is None, it means that no more items can be fetched. pub ctoken: Option, } impl Default for Paginator { fn default() -> Self { Self { count: Some(0), items: Vec::new(), ctoken: None, } } } impl Paginator { pub(crate) fn new(count: Option, items: Vec, ctoken: Option) -> Self { Self { count: match ctoken { Some(_) => count, None => items.len().try_into().ok(), }, items, ctoken, } } /// Check if the paginator is exhausted, meaning that no more /// items can be fetched. /// /// Equivalent to `paginator.ctoken.is_none()`. pub fn is_exhausted(&self) -> bool { self.ctoken.is_none() } /// Check if the paginator does not contain any data, meaning that it /// is exhausted and does not contain any items. pub fn is_empty(&self) -> bool { self.items.is_empty() && self.is_exhausted() } }