fauna-api 1.0.789

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::authentification::CredentialsOrApiKey;
use crate::error::Result;
use crate::http::{client, ok};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AddressKind {
    Email,
    Phone,
    Discord,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Address {
    pub kind: AddressKind,
    /// Lowercase email, E.164 phone (+12395551234), or Discord handle without @.
    pub value: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Contact {
    pub id: String,
    pub display_name: String,
    #[serde(default)]
    pub addresses: Vec<Address>,
    /// One of its emails belongs to a Fauna account: the verified badge.
    #[serde(default)]
    pub registered: bool,
    /// That Fauna account's picture.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
}

/// One of the inbox's owners, behind one of their verified addresses: show "You" when
/// `owner` is your own email, else their name.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Owner {
    /// The owner's email.
    pub owner: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
    /// The address as it appeared.
    pub address: String,
}

impl Owner {
    pub fn from_json(json: &Value) -> Owner {
        let text = |key: &str| json[key].as_str().unwrap_or_default().to_string();
        Owner {
            owner: text("owner"),
            display_name: crate::tickets::optional(&json["displayName"]),
            icon: crate::tickets::optional(&json["icon"]),
            address: text("address"),
        }
    }
}

pub(crate) async fn post(
    authorization: &CredentialsOrApiKey,
    path: &str,
    body: Option<Value>,
) -> Result<reqwest::Response> {
    let request = client()
        .post(format!("{}{path}", super::get_url()))
        .header("Content-Type", "application/json")
        .header(
            "Authorization",
            match authorization {
                CredentialsOrApiKey::Credentials(credentials) => {
                    format!("Token {}", credentials.token)
                }
                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
            },
        );
    let request = match body {
        Some(body) => request.json(&body),
        None => request,
    };
    ok(request.send().await?).await
}

/// Every contact, sorted by display name.
pub async fn list(authorization: &CredentialsOrApiKey) -> Result<Vec<Contact>> {
    Ok(post(authorization, "/cicada/contacts", None)
        .await?
        .json()
        .await?)
}

/// `{ id?, displayName, addresses }` in the TypeScript SDK.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ContactDraft {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub display_name: String,
    pub addresses: Vec<Address>,
}

/// Creates a contact, or with `id` renames one and replaces its addresses.
pub async fn save(authorization: &CredentialsOrApiKey, contact: &ContactDraft) -> Result<Contact> {
    Ok(post(
        authorization,
        "/cicada/contacts/save",
        Some(serde_json::to_value(contact)?),
    )
    .await?
    .json()
    .await?)
}

pub async fn remove(authorization: &CredentialsOrApiKey, id: &str) -> Result<()> {
    post(
        authorization,
        "/cicada/contacts/delete",
        Some(json!({ "id": id })),
    )
    .await?;
    Ok(())
}