delopay 0.20.0

Official Rust SDK for the DeloPay payments API
Documentation
//! A shop's own texts of the emails it sends its buyers.
//!
//! In each language the shop's text goes before the platform's, which goes
//! before the built-in one. Enumerated values arrive as strings, so a value
//! added later does not break decoding; each field lists the values known
//! today.

use std::fmt;

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

/// A template's text in one language, in one of two modes: the branded
/// layout filled with the parts below, or a whole HTML document.
///
/// Sent as `{"mode": "layout", ...}` or `{"mode": "html", ...}`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "mode", rename_all = "snake_case")]
#[non_exhaustive]
pub enum MessageTemplateContent {
    /// The branded layout, filled with a subject, heading, body and button.
    Layout(MessageTemplateLayout),
    /// A whole HTML document.
    Html(MessageTemplateHtml),
}

/// The parts of a text in the branded layout.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateLayout {
    /// The subject line.
    pub subject: String,
    /// The preview text mail clients show after the subject.
    #[serde(default)]
    pub preheader: Option<String>,
    /// The heading.
    #[serde(default)]
    pub heading: Option<String>,
    /// The body.
    pub body: String,
    /// The label of the button, for a template with a link.
    #[serde(default)]
    pub action_label: Option<String>,
    /// The footer.
    #[serde(default)]
    pub footer: Option<String>,
}

impl MessageTemplateLayout {
    /// A layout text with a subject and a body; the other parts are left
    /// empty.
    pub fn new(subject: impl Into<String>, body: impl Into<String>) -> Self {
        Self {
            subject: subject.into(),
            preheader: None,
            heading: None,
            body: body.into(),
            action_label: None,
            footer: None,
        }
    }

    /// Set the preview text.
    #[must_use]
    pub fn preheader(mut self, preheader: impl Into<String>) -> Self {
        self.preheader = Some(preheader.into());
        self
    }

    /// Set the heading.
    #[must_use]
    pub fn heading(mut self, heading: impl Into<String>) -> Self {
        self.heading = Some(heading.into());
        self
    }

    /// Set the button's label.
    #[must_use]
    pub fn action_label(mut self, action_label: impl Into<String>) -> Self {
        self.action_label = Some(action_label.into());
        self
    }

    /// Set the footer.
    #[must_use]
    pub fn footer(mut self, footer: impl Into<String>) -> Self {
        self.footer = Some(footer.into());
        self
    }
}

/// A text written as a whole HTML document.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateHtml {
    /// The subject line.
    pub subject: String,
    /// The HTML document.
    pub html: String,
}

impl MessageTemplateHtml {
    /// An HTML text with a subject.
    pub fn new(subject: impl Into<String>, html: impl Into<String>) -> Self {
        Self {
            subject: subject.into(),
            html: html.into(),
        }
    }
}

/// Where one language of a template takes its text from.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateLanguageSummary {
    /// The language: `en`, `de`, `fr`, `es`, `it`, `nl`, `pt` or `pl`.
    pub language: String,
    /// `shop`, `platform` or `built_in`.
    pub source: String,
    /// `layout` or `html`.
    pub mode: String,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// One template in a list.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateSummary {
    /// The template's stable id, for example `reset_password`.
    pub id: String,
    /// `email`.
    pub channel: String,
    /// Who receives it: `dashboard_user`, `operator`, `buyer` or
    /// `buyer_account`.
    pub audience: String,
    /// Where its text may be stored: `platform`, `merchant`, `shop`.
    #[serde(default)]
    pub editable_scopes: Vec<String>,
    /// The variable the layout's button opens, if the template has a link.
    #[serde(default)]
    pub action_variable: Option<String>,
    /// Whether that link can never be left out.
    #[serde(default)]
    pub action_required: bool,
    /// Every language, in the order of the list's `languages`.
    #[serde(default)]
    pub languages: Vec<MessageTemplateLanguageSummary>,
    /// The languages with a text stored at the requested scope.
    #[serde(default)]
    pub customised_languages: Vec<String>,
    /// How many shops store a text of their own, in a platform list; `None`
    /// otherwise.
    #[serde(default)]
    pub shops_customised: Option<i64>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// Every template that can be edited at one scope.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateList {
    /// `platform`, `merchant` or `shop`.
    pub scope: String,
    /// The shop's profile id, or `platform`.
    pub scope_id: String,
    /// Every language a text can be written in.
    #[serde(default)]
    pub languages: Vec<String>,
    /// The language a message is written in when nothing about its
    /// recipient names one.
    pub default_language: String,
    /// The templates the caller may read.
    #[serde(default)]
    pub templates: Vec<MessageTemplateSummary>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// A variable a template's text can use.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateVariable {
    /// The variable's name.
    pub name: String,
    /// Its alias in the text.
    pub alias: String,
    /// Whether every text must use it.
    pub required: bool,
    /// A sample value, used in previews.
    pub sample: String,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// Who last stored a text.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateActor {
    /// The user's id.
    pub id: String,
    /// The user's name.
    #[serde(default)]
    pub name: Option<String>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// A text stored at the requested scope.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateStored {
    /// The text.
    pub content: MessageTemplateContent,
    /// Its revision; send it back as `expected_revision` to refuse a save
    /// over someone else's change.
    pub revision: i64,
    /// When it was stored.
    pub updated_at: String,
    /// Who stored it.
    #[serde(default)]
    pub updated_by: Option<MessageTemplateActor>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// What applies without a text of the scope's own.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateInherited {
    /// The text.
    pub content: MessageTemplateContent,
    /// `shop`, `platform` or `built_in`.
    pub source: String,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// One language of a template at one scope.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateLanguage {
    /// The language: `en`, `de`, `fr`, `es`, `it`, `nl`, `pt` or `pl`.
    pub language: String,
    /// The scope's own text, when one is stored.
    #[serde(default)]
    pub own: Option<MessageTemplateStored>,
    /// What applies without `own`.
    pub inherited: MessageTemplateInherited,
    /// The text the platform ships.
    pub built_in: MessageTemplateContent,
    /// Which layer the text in force comes from: `shop`, `platform` or
    /// `built_in`.
    pub effective_source: String,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// One template, its variables and its text in every language.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplateDetail {
    /// The template's stable id.
    pub id: String,
    /// `email`.
    pub channel: String,
    /// Who receives it: `dashboard_user`, `operator`, `buyer` or
    /// `buyer_account`.
    pub audience: String,
    /// Where its text may be stored: `platform`, `merchant`, `shop`.
    #[serde(default)]
    pub editable_scopes: Vec<String>,
    /// The variable the layout's button opens, if the template has a link.
    #[serde(default)]
    pub action_variable: Option<String>,
    /// Whether that link can never be left out.
    #[serde(default)]
    pub action_required: bool,
    /// The variables a text can use.
    #[serde(default)]
    pub variables: Vec<MessageTemplateVariable>,
    /// The text in every language.
    #[serde(default)]
    pub languages: Vec<MessageTemplateLanguage>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// A template rendered with sample values.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MessageTemplatePreview {
    /// The language rendered.
    pub language: String,
    /// The rendered subject.
    pub subject: String,
    /// The rendered HTML.
    pub html: String,
    /// The rendered plain text.
    pub text: String,
    /// What the text may get wrong, such as a missing required variable.
    #[serde(default)]
    pub warnings: Vec<String>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// Where a test message was sent.
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct SendTestMessageResponse {
    /// The address the test was sent to.
    pub sent_to: String,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

impl fmt::Debug for SendTestMessageResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SendTestMessageResponse")
            .field("sent_to", &"<redacted>")
            .finish_non_exhaustive()
    }
}