mailcycle 0.3.0

Rust client for the Mailcycle API. Create addresses that receive mail, read and decrypt it on your machine, send, and listen for events.
Documentation
//! The shapes a caller sees.
//!
//! Each one carries the fields the API documents. Anything the API adds later
//! is kept in `extra` rather than dropped, so a new server field does not need
//! a new release to reach a caller.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::trackers::BlockedTracker;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Plan {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub name: String,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

#[derive(Debug, Clone)]
pub struct Account {
    pub id: String,
    pub plan_id: String,
    pub created_at: String,
    pub plan: Plan,
}

#[derive(Debug, Clone)]
pub struct Address {
    pub id: String,
    pub email_address: String,
    /// `active` when assigned to a device, `unassigned` otherwise.
    pub status: String,
    pub created_at: String,
    pub retention_days: u32,
    pub assigned_worker_id: Option<String>,
    pub paused_at: Option<String>,
    /// The label set in the app. `None` without the phrase, or if it will not open.
    pub label: Option<String>,
    /// Whether the address can receive mail: it was created with its public key.
    pub receives: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Domain {
    #[serde(default)]
    pub domain: String,
    /// True for a domain that belongs to this account alone.
    #[serde(default)]
    pub reserved: bool,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

#[derive(Debug, Clone)]
pub struct Attachment {
    pub index: usize,
    pub filename: String,
    pub mime_type: String,
    pub size: u64,
    /// Set on a part the HTML shows inline, where it says `cid:` and this id.
    pub content_id: Option<String>,
    /// False when the part had no body. Fetching it would answer 404.
    pub stored: bool,
}

#[derive(Debug, Clone)]
pub struct Message {
    pub id: String,
    pub address_id: String,
    pub received_at: String,
    pub read: bool,
    pub starred: bool,
    pub folder: String,
    pub size_bytes: u64,
    /// For sent mail: `app`, `api`, `device`, `agent` or `member` (a team member).
    pub sent_via: Option<String>,
    /// The address key epoch it was sealed under. 0 unless the address's keys were rotated.
    pub key_epoch: u32,
    /// False when the content could not be opened: no phrase, or the wrong one.
    pub opened: bool,
    pub from_address: String,
    pub from_name: String,
    /// Where a reply goes: Reply-To if the sender set one, otherwise From.
    pub reply_to: String,
    pub to: String,
    pub subject: String,
    pub text: String,
    /// The HTML part as sent, remote images and all. Do not render this one.
    pub html: String,
    /// The HTML with everything remote removed, as the app shows it.
    pub safe_html: String,
    /// What `safe_html` had taken out of it, and who it would have told.
    pub blocked: Vec<BlockedTracker>,
    /// The sender's Message-ID, for threading a reply.
    pub message_id: String,
    pub attachments: Vec<Attachment>,
}

#[derive(Debug, Clone)]
pub struct MessagePage {
    pub messages: Vec<Message>,
    /// Opaque. Pass it back as it is to get the next page; `None` on the last.
    pub next_cursor: Option<String>,
}

/// What a send returns.
#[derive(Debug, Clone)]
pub struct SendResult {
    /// The Message-ID the sending service gave it.
    pub message_id: String,
    /// The id of the copy kept in Sent, which the message calls take. `None`
    /// when no copy was kept, as for an address without a public key.
    pub sent_copy_id: Option<String>,
}

#[derive(Debug, Clone)]
pub struct Device {
    pub id: String,
    pub platform: String,
    pub kind: String,
    pub send_mode: String,
    pub status: String,
    pub created_at: String,
    pub last_active_at: Option<String>,
    pub paused_at: Option<String>,
    /// The name set in the app. `None` without the phrase.
    pub name: Option<String>,
    pub tags: Vec<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ActivityEntry {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub kind: String,
    #[serde(default, rename = "createdAt")]
    pub created_at: String,
    #[serde(default, rename = "inboxId")]
    pub address_id: Option<String>,
    #[serde(default, rename = "workerId")]
    pub worker_id: Option<String>,
}

/// One page of the event log, newest first.
#[derive(Debug, Clone)]
pub struct ActivityPage {
    pub activity: Vec<ActivityEntry>,
    /// Opaque. Pass it back as it is to get the page before; `None` on the last.
    pub next_cursor: Option<String>,
}

/// One event from the live stream. Events carry ids, never content.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct AccountEvent {
    #[serde(default)]
    pub id: String,
    #[serde(default, rename = "type")]
    pub event_type: String,
    #[serde(default, rename = "createdAt")]
    pub created_at: String,
    #[serde(default)]
    pub payload: BTreeMap<String, Value>,
}

/// An API key. The token itself is shown once, when it is created.
#[derive(Debug, Clone)]
pub struct ApiKey {
    pub id: String,
    /// The name set when it was made, opened with the phrase. `None` if it
    /// will not open.
    pub name: Option<String>,
    /// The token's last four characters.
    pub hint: String,
    pub created_at: String,
    /// Rounded down to five minutes. `None` until the key is used.
    pub last_used_at: Option<String>,
}

/// A new API key and its token. The token is not shown again.
#[derive(Debug, Clone)]
pub struct CreatedApiKey {
    pub api_key: ApiKey,
    pub token: String,
}

#[derive(Debug, Clone)]
pub struct Webhook {
    pub id: String,
    /// The name set in the app, opened with the phrase. `None` if it will not
    /// open.
    pub name: Option<String>,
    pub url: String,
    /// Event types, or `["*"]` for every event.
    pub events: Vec<String>,
    /// `active` or `disabled`.
    pub status: String,
    pub disabled_reason: Option<String>,
    /// Set while every attempt since the last success has failed.
    pub failing_since: Option<String>,
    pub last_success_at: Option<String>,
    pub created_at: String,
}

/// The account's webhooks, and every event type one can ask for.
#[derive(Debug, Clone)]
pub struct WebhookList {
    pub webhooks: Vec<Webhook>,
    pub event_types: Vec<String>,
}

/// A new webhook and its signing secret. The secret is not shown again.
#[derive(Debug, Clone)]
pub struct CreatedWebhook {
    pub webhook: Webhook,
    pub secret: String,
}

/// What to change on a webhook. `Default` changes nothing.
#[derive(Debug, Clone, Default)]
pub struct WebhookUpdate {
    pub url: Option<String>,
    pub events: Option<Vec<String>>,
    pub name: Option<String>,
    /// Switches it on or off.
    pub enabled: Option<bool>,
}

/// One attempt to deliver an event to a webhook.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WebhookDelivery {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub event_id: String,
    #[serde(default)]
    pub event_type: String,
    /// The body as sent: type, ids and times, never content.
    #[serde(default)]
    pub event: AccountEvent,
    /// `pending`, `succeeded` or `failed`.
    #[serde(default)]
    pub status: String,
    #[serde(default)]
    pub attempts: u32,
    #[serde(default)]
    pub response_status: Option<u16>,
    #[serde(default)]
    pub error: Option<String>,
    #[serde(default)]
    pub created_at: String,
    #[serde(default)]
    pub last_attempt_at: Option<String>,
    #[serde(default)]
    pub next_attempt_at: Option<String>,
}

#[derive(Debug, Clone)]
pub struct DeliveryPage {
    pub deliveries: Vec<WebhookDelivery>,
    /// Opaque. Pass it back as it is to get the next page; `None` on the last.
    pub next_cursor: Option<String>,
}

/// The delivery a test or a redelivery queued.
#[derive(Debug, Clone)]
pub struct QueuedDelivery {
    pub delivery_id: String,
    /// Set for a test, which makes a new event. A redelivery sends the
    /// original event again, id and all.
    pub event_id: Option<String>,
}

/// A signed-in session on the account.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Session {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub created_at: String,
    #[serde(default)]
    pub expires_at: String,
    /// True for the session this client is using.
    #[serde(default)]
    pub current: bool,
}

/// The account's subscription, balance, and any pause after a missed payment.
#[derive(Debug, Clone)]
pub struct Subscription {
    pub id: String,
    pub plan_id: String,
    pub status: String,
    pub renewal_date: String,
    /// The plan the account moves to when the paid month runs out.
    pub pending_plan_id: Option<String>,
    /// Account balance, in minor units.
    pub balance_minor: i64,
    /// What a missed payment has paused, and when it is deleted. `None` when
    /// nothing is paused.
    pub pause: Option<Value>,
}

/// The kind of name the server rolls for a new address.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NameStyle {
    /// Like `maya.holt`.
    Person,
    /// Like `harbor-supply42`.
    Business,
    /// Like `support_desk`.
    Team,
    /// Like `quiet_falcon`. What the server rolls when no style is given.
    #[default]
    Neutral,
}

impl NameStyle {
    /// The name the API uses.
    pub fn as_str(self) -> &'static str {
        match self {
            NameStyle::Person => "person",
            NameStyle::Business => "business",
            NameStyle::Team => "team",
            NameStyle::Neutral => "neutral",
        }
    }
}