wave-api 0.1.0

Typed Rust client for the Wave Accounting GraphQL API
Documentation
use serde::Deserialize;

/// Flattened page of results from a Wave connection type.
#[derive(Debug, Clone)]
pub struct Page<T> {
    /// Items on this page.
    pub items: Vec<T>,
    /// Current page number (1-based).
    pub current_page: i64,
    /// Total number of pages.
    pub total_pages: Option<i64>,
    /// Total number of items across all pages.
    pub total_count: Option<i64>,
}

impl<T> Page<T> {
    /// Whether there are more pages after this one.
    pub fn has_next_page(&self) -> bool {
        match self.total_pages {
            Some(tp) => self.current_page < tp,
            None => false,
        }
    }
}

/// Raw offset page info from the API.
#[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>,
}

/// Raw connection wrapper for deserialization. Can be used directly for
/// custom queries, but most users should use the flattened [`Page<T>`] returned
/// by client methods.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Connection<T> {
    pub edges: Vec<Edge<T>>,
    pub page_info: OffsetPageInfo,
}

/// Raw edge wrapper for deserialization.
#[derive(Debug, Clone, Deserialize)]
pub struct Edge<T> {
    pub node: T,
}

impl<T> Connection<T> {
    /// Flatten a Connection into a Page.
    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,
        }
    }
}