rust-umami 0.1.1

An ergonomic Rust client for Umami Analytics event ingestion
Documentation
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// Response returned after sending one analytics track.
///
/// Umami can intentionally ignore bot or disabled traffic and return a small
/// response without session identifiers. Use [`TrackResponse::is_tracked`] to
/// distinguish that case without relying on undocumented fields.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TrackResponse {
    /// Cache value that can be supplied with a later request from this session.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache: Option<String>,
    /// Derived Umami session ID.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    /// Derived Umami visit ID.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub visit_id: Option<String>,
    /// Whether collection was disabled by the server or gateway.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub disabled: bool,
    /// Additional forward-compatible response fields.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

impl TrackResponse {
    /// Returns true when Umami recorded the track and issued session metadata.
    #[must_use]
    pub fn is_tracked(&self) -> bool {
        self.session_id.is_some() && self.visit_id.is_some()
    }

    /// Returns true when Umami accepted the request but did not record it.
    #[must_use]
    pub fn is_ignored(&self) -> bool {
        !self.is_tracked()
    }
}

/// Per-item error returned inside a batch response.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BatchError {
    /// Zero-based index of the failed item in the submitted batch.
    pub index: usize,
    /// Umami's response for the failed item.
    pub response: Value,
}

/// Summary returned by Umami's batch ingestion endpoint.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BatchResponse {
    /// Number of submitted items.
    pub size: usize,
    /// Number of successfully processed items.
    pub processed: usize,
    /// Number of failed items.
    pub errors: usize,
    /// Details for each failed item.
    #[serde(default)]
    pub details: Vec<BatchError>,
    /// Cache value returned for the first successfully processed item.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache: Option<String>,
}

impl BatchResponse {
    /// Returns true when every item was processed successfully.
    #[must_use]
    pub fn is_success(&self) -> bool {
        self.errors == 0
    }
}