use std::collections::BTreeMap;
use serde_json::{Map, Value};
use crate::generated::{CreateWorkflowNodeTypeName, WorkflowQueuedContactPolicy};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContactId {
Email(String),
UserId(String),
Both {
email: String,
user_id: String,
},
}
impl ContactId {
pub fn email(email: impl Into<String>) -> Self {
Self::Email(email.into())
}
pub fn user_id(user_id: impl Into<String>) -> Self {
Self::UserId(user_id.into())
}
pub fn both(email: impl Into<String>, user_id: impl Into<String>) -> Self {
Self::Both {
email: email.into(),
user_id: user_id.into(),
}
}
pub fn email_address(&self) -> Option<&str> {
match self {
Self::Email(email) | Self::Both { email, .. } => Some(email),
Self::UserId(_) => None,
}
}
pub fn user_id_value(&self) -> Option<&str> {
match self {
Self::UserId(user_id) | Self::Both { user_id, .. } => Some(user_id),
Self::Email(_) => None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
#[must_use]
pub struct ContactFields {
pub first_name: Option<String>,
pub last_name: Option<String>,
pub source: Option<String>,
pub subscribed: Option<bool>,
pub user_group: Option<String>,
pub mailing_lists: BTreeMap<String, bool>,
pub custom_properties: Map<String, Value>,
}
impl ContactFields {
pub fn new() -> Self {
Self::default()
}
pub fn first_name(mut self, value: impl Into<String>) -> Self {
self.first_name = Some(value.into());
self
}
pub fn last_name(mut self, value: impl Into<String>) -> Self {
self.last_name = Some(value.into());
self
}
pub fn source(mut self, value: impl Into<String>) -> Self {
self.source = Some(value.into());
self
}
pub fn subscribed(mut self, value: bool) -> Self {
self.subscribed = Some(value);
self
}
pub fn user_group(mut self, value: impl Into<String>) -> Self {
self.user_group = Some(value.into());
self
}
pub fn mailing_list(mut self, list_id: impl Into<String>, subscribed: bool) -> Self {
self.mailing_lists.insert(list_id.into(), subscribed);
self
}
pub fn property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.custom_properties.insert(key.into(), value.into());
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[must_use]
pub struct Page {
pub per_page: Option<u32>,
pub cursor: Option<String>,
}
impl Page {
pub fn first() -> Self {
Self::default()
}
pub fn per_page(mut self, per_page: u32) -> Self {
self.per_page = Some(per_page);
self
}
pub fn cursor(mut self, cursor: impl Into<String>) -> Self {
self.cursor = Some(cursor.into());
self
}
pub(crate) fn per_page_param(&self) -> Option<String> {
self.per_page.map(|per_page| per_page.to_string())
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
#[must_use]
pub struct EventOptions {
pub event_properties: Map<String, Value>,
pub mailing_lists: BTreeMap<String, bool>,
pub contact_properties: Map<String, Value>,
pub idempotency_key: Option<String>,
}
impl EventOptions {
pub fn new() -> Self {
Self::default()
}
pub fn event_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.event_properties.insert(key.into(), value.into());
self
}
pub fn mailing_list(mut self, list_id: impl Into<String>, subscribed: bool) -> Self {
self.mailing_lists.insert(list_id.into(), subscribed);
self
}
pub fn contact_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.contact_properties.insert(key.into(), value.into());
self
}
pub fn idempotency_key(mut self, key: impl Into<String>) -> Self {
self.idempotency_key = Some(key.into());
self
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
#[must_use]
pub struct TransactionalOptions {
pub data_variables: Map<String, Value>,
pub attachments: Vec<crate::generated::TransactionalRequestAttachment>,
pub add_to_audience: Option<bool>,
pub idempotency_key: Option<String>,
}
impl TransactionalOptions {
pub fn new() -> Self {
Self::default()
}
pub fn data_variable(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.data_variables.insert(key.into(), value.into());
self
}
pub fn attachment(
mut self,
filename: impl Into<String>,
content_type: impl Into<String>,
data: impl Into<String>,
) -> Self {
self.attachments
.push(crate::generated::TransactionalRequestAttachment {
filename: filename.into(),
content_type: content_type.into(),
data: data.into(),
});
self
}
pub fn add_to_audience(mut self, add: bool) -> Self {
self.add_to_audience = Some(add);
self
}
pub fn idempotency_key(mut self, key: impl Into<String>) -> Self {
self.idempotency_key = Some(key.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmailFormat {
Plain,
Styled,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[must_use]
pub struct EmailMessageUpdate {
pub subject: Option<String>,
pub preview_text: Option<String>,
pub from_name: Option<String>,
pub from_email: Option<String>,
pub reply_to_email: Option<String>,
pub lmx: Option<String>,
pub expected_revision_id: Option<String>,
pub cc_email: Option<String>,
pub bcc_email: Option<String>,
pub language_code: Option<String>,
pub email_format: Option<EmailFormat>,
pub contact_properties_fallbacks: Option<BTreeMap<String, Option<String>>>,
pub event_properties_fallbacks: Option<BTreeMap<String, Option<String>>>,
pub data_variables_fallbacks: Option<BTreeMap<String, Option<String>>>,
}
impl EmailMessageUpdate {
pub fn new() -> Self {
Self::default()
}
pub fn subject(mut self, value: impl Into<String>) -> Self {
self.subject = Some(value.into());
self
}
pub fn preview_text(mut self, value: impl Into<String>) -> Self {
self.preview_text = Some(value.into());
self
}
pub fn from_name(mut self, value: impl Into<String>) -> Self {
self.from_name = Some(value.into());
self
}
pub fn from_email(mut self, value: impl Into<String>) -> Self {
self.from_email = Some(value.into());
self
}
pub fn reply_to_email(mut self, value: impl Into<String>) -> Self {
self.reply_to_email = Some(value.into());
self
}
pub fn lmx(mut self, value: impl Into<String>) -> Self {
self.lmx = Some(value.into());
self
}
pub fn expected_revision_id(mut self, value: impl Into<String>) -> Self {
self.expected_revision_id = Some(value.into());
self
}
pub fn cc_email(mut self, value: impl Into<String>) -> Self {
self.cc_email = Some(value.into());
self
}
pub fn bcc_email(mut self, value: impl Into<String>) -> Self {
self.bcc_email = Some(value.into());
self
}
pub fn language_code(mut self, value: impl Into<String>) -> Self {
self.language_code = Some(value.into());
self
}
pub fn email_format(mut self, value: EmailFormat) -> Self {
self.email_format = Some(value);
self
}
pub fn contact_property_fallback(
mut self,
key: impl Into<String>,
value: Option<&str>,
) -> Self {
self.contact_properties_fallbacks
.get_or_insert_with(BTreeMap::new)
.insert(key.into(), value.map(str::to_owned));
self
}
pub fn event_property_fallback(mut self, key: impl Into<String>, value: Option<&str>) -> Self {
self.event_properties_fallbacks
.get_or_insert_with(BTreeMap::new)
.insert(key.into(), value.map(str::to_owned));
self
}
pub fn data_variable_fallback(mut self, key: impl Into<String>, value: Option<&str>) -> Self {
self.data_variables_fallbacks
.get_or_insert_with(BTreeMap::new)
.insert(key.into(), value.map(str::to_owned));
self
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
#[must_use]
pub struct PreviewOptions {
pub contact_properties: Map<String, Value>,
pub event_properties: Map<String, Value>,
pub data_variables: Map<String, Value>,
}
impl PreviewOptions {
pub fn new() -> Self {
Self::default()
}
pub fn contact_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.contact_properties.insert(key.into(), value.into());
self
}
pub fn event_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.event_properties.insert(key.into(), value.into());
self
}
pub fn data_variable(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.data_variables.insert(key.into(), value.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodePlacement {
Between {
from_node_id: String,
to_node_id: String,
},
Before {
to_node_id: String,
},
After {
from_node_id: String,
},
}
impl NodePlacement {
pub fn between(from_node_id: impl Into<String>, to_node_id: impl Into<String>) -> Self {
Self::Between {
from_node_id: from_node_id.into(),
to_node_id: to_node_id.into(),
}
}
pub fn before(to_node_id: impl Into<String>) -> Self {
Self::Before {
to_node_id: to_node_id.into(),
}
}
pub fn after(from_node_id: impl Into<String>) -> Self {
Self::After {
from_node_id: from_node_id.into(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
#[must_use]
pub struct DeleteNodeOptions {
pub dry_run: Option<bool>,
pub queued_contact_policy: Option<WorkflowQueuedContactPolicy>,
pub recursive: bool,
}
impl DeleteNodeOptions {
pub fn new() -> Self {
Self::default()
}
pub fn dry_run(mut self, dry_run: bool) -> Self {
self.dry_run = Some(dry_run);
self
}
pub fn queued_contact_policy(mut self, policy: WorkflowQueuedContactPolicy) -> Self {
self.queued_contact_policy = Some(policy);
self
}
pub fn recursive(mut self, recursive: bool) -> Self {
self.recursive = recursive;
self
}
}
pub(crate) fn node_type_name(node_type: &CreateWorkflowNodeTypeName) -> Value {
serde_json::to_value(node_type).unwrap_or(Value::Null)
}