use crate::attachment::Attachment;
use crate::mail_settings::MailSettings;
use crate::personalization::Personalization;
use crate::tracking_settings::TrackingSettings;
use crate::{Asm, Contact, Content};
use serde::Serialize;
use std::collections::HashMap;
#[derive(Debug, Serialize)]
pub struct Message {
#[serde(skip_serializing_if = "Vec::is_empty")]
personalizations: Vec<Personalization>,
from: Contact,
#[serde(skip_serializing_if = "Option::is_none")]
reply_to: Option<Contact>,
subject: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
content: Vec<Content>,
#[serde(skip_serializing_if = "Vec::is_empty")]
attachments: Vec<Attachment>,
#[serde(skip_serializing_if = "Option::is_none")]
template_id: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
sections: HashMap<String, String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
headers: HashMap<String, String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
categories: Vec<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
custom_args: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
send_at: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
batch_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
asm: Option<Asm>,
#[serde(skip_serializing_if = "Option::is_none")]
ip_pool_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
mail_settings: Option<MailSettings>,
#[serde(skip_serializing_if = "Option::is_none")]
tracking_settings: Option<TrackingSettings>,
}
impl Message {
pub fn to_json(&self) -> String {
serde_json::to_string(&self).expect("could not properly serialize into JSON")
}
}
pub struct MessageBuilder {
message: Message,
}
impl MessageBuilder {
pub fn new(from: Contact, subject: impl Into<String>) -> Self {
MessageBuilder {
message: Message {
personalizations: vec![],
from,
reply_to: None,
subject: subject.into(),
content: vec![],
attachments: vec![],
template_id: None,
sections: HashMap::new(),
headers: HashMap::new(),
categories: vec![],
custom_args: HashMap::new(),
send_at: None,
batch_id: None,
asm: None,
ip_pool_name: None,
mail_settings: None,
tracking_settings: None,
},
}
}
pub fn personalization(mut self, per: Personalization) -> Self {
self.message.personalizations.push(per);
self
}
pub fn personalizations(mut self, data: Vec<Personalization>) -> Self {
self.message.personalizations = data;
self
}
pub fn reply_to(mut self, contact: Contact) -> Self {
self.message.reply_to = Some(contact);
self
}
pub fn content(mut self, content: Content) -> Self {
self.message.content.push(content);
self
}
pub fn attachment(mut self, attachment: Attachment) -> Self {
self.message.attachments.push(attachment);
self
}
pub fn template_id(mut self, id: impl Into<String>) -> Self {
self.message.template_id = Some(id.into());
self
}
pub fn section<S: Into<String>>(mut self, key: S, value: S) -> Self {
self.message.sections.insert(key.into(), value.into());
self
}
pub fn header<S: Into<String>>(mut self, key: S, value: S) -> Self {
self.message.headers.insert(key.into(), value.into());
self
}
pub fn category(mut self, category: impl Into<String>) -> Self {
self.message.categories.push(category.into());
self
}
pub fn custom_arg<S: Into<String>>(mut self, key: S, value: S) -> Self {
self.message.custom_args.insert(key.into(), value.into());
self
}
pub fn send_at(mut self, time: i32) -> Self {
self.message.send_at = Some(time);
self
}
pub fn batch_id(mut self, id: impl Into<String>) -> Self {
self.message.batch_id = Some(id.into());
self
}
pub fn asm(mut self, asm: Asm) -> Self {
self.message.asm = Some(asm);
self
}
pub fn ip_pool_name(mut self, name: impl Into<String>) -> Self {
self.message.ip_pool_name = Some(name.into());
self
}
pub fn mail_settings(mut self, settings: MailSettings) -> Self {
self.message.mail_settings = Some(settings);
self
}
pub fn tracking_settings(mut self, settings: TrackingSettings) -> Self {
self.message.tracking_settings = Some(settings);
self
}
pub fn build(self) -> Message {
self.message
}
}