emailit 2.0.3

The official Rust SDK for the Emailit Email API
Documentation
//! Generic paginated collection returned by list endpoints.

use serde::Deserialize;

/// A paginated collection of items returned by list API endpoints.
///
/// Supports iteration via [`IntoIterator`] and provides convenience methods
/// for pagination.
///
/// # Example
///
/// ```no_run
/// # async fn example() -> emailit::Result<()> {
/// # let emailit = emailit::Emailit::new("key");
/// let page = emailit.domains.list(None).await?;
/// println!("showing {} of {} domains", page.len(), page.total.unwrap_or(0));
/// for domain in &page {
///     println!("{:?}", domain);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Deserialize)]
pub struct Collection<T> {
    /// The items in the current page.
    pub data: Vec<T>,
    /// Current page number (1-based), if returned by the API.
    #[serde(default)]
    pub page: Option<i64>,
    /// Number of items per page, if returned by the API.
    #[serde(default)]
    pub per_page: Option<i64>,
    /// Total number of items across all pages, if returned by the API.
    #[serde(default)]
    pub total: Option<i64>,
    /// Total number of pages, if returned by the API.
    #[serde(default)]
    pub total_pages: Option<i64>,
    /// URL for the next page, or `None` if this is the last page.
    #[serde(default)]
    pub next_page_url: Option<String>,
}

impl<T> Collection<T> {
    /// Returns `true` if there are more pages after this one.
    pub fn has_more(&self) -> bool {
        self.next_page_url.is_some()
    }

    /// Returns the number of items in the current page.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns `true` if the current page contains no items.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Returns an iterator over references to the items in this page.
    pub fn iter(&self) -> std::slice::Iter<'_, T> {
        self.data.iter()
    }
}

impl<T> IntoIterator for Collection<T> {
    type Item = T;
    type IntoIter = std::vec::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.into_iter()
    }
}

impl<'a, T> IntoIterator for &'a Collection<T> {
    type Item = &'a T;
    type IntoIter = std::slice::Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.iter()
    }
}