use data_encoding::BASE64;
use serde::Serialize;
#[derive(Default, Serialize)]
pub struct Message {
from: Address,
subject: String,
personalizations: Vec<super::Personalization>,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<Vec<Content>>,
#[serde(skip_serializing_if = "Option::is_none")]
attachments: Option<Vec<Attachment>>,
#[serde(skip_serializing_if = "Option::is_none")]
template_id: Option<String>,
}
impl Message {
pub fn new() -> Message {
Message::default()
}
pub fn set_from(mut self, from: Address) -> Message {
self.from = from;
self
}
pub fn set_subject(mut self, subject: &str) -> Message {
self.subject = String::from(subject);
self
}
pub fn set_template_id(mut self, template_id: &str) -> Message {
self.template_id = Some(String::from(template_id));
self
}
pub fn add_content(mut self, c: Content) -> Message {
match self.content {
None => {
let mut content = Vec::new();
content.push(c);
self.content = Some(content);
}
Some(ref mut content) => content.push(c),
};
self
}
pub fn add_personalization(mut self, p: super::Personalization) -> Message {
self.personalizations.push(p);
self
}
pub fn add_attachment(mut self, a: Attachment) -> Message {
match self.attachments {
None => {
let mut attachments = Vec::new();
attachments.push(a);
self.attachments = Some(attachments);
}
Some(ref mut attachments) => attachments.push(a),
};
self
}
}
#[derive(Clone, Default, Serialize)]
pub struct Address {
email: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
}
impl Address {
pub fn new() -> Address {
Address::default()
}
pub fn set_email(mut self, email: &str) -> Address {
self.email = String::from(email);
self
}
pub fn set_name(mut self, name: &str) -> Address {
self.name = Some(String::from(name));
self
}
}
#[derive(Clone, Default, Serialize)]
pub struct Content {
#[serde(rename = "type")]
content_type: String,
value: String,
}
impl Content {
pub fn new() -> Content {
Content::default()
}
pub fn set_content_type(mut self, content_type: &str) -> Content {
self.content_type = String::from(content_type);
self
}
pub fn set_value(mut self, value: &str) -> Content {
self.value = String::from(value);
self
}
}
#[derive(Default, Serialize)]
pub struct Attachment {
content: String,
filename: String,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
disposition: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
content_id: Option<String>,
}
impl Attachment {
pub fn new() -> Attachment {
Attachment::default()
}
pub fn set_content(mut self, c: &[u8]) -> Attachment {
self.content = BASE64.encode(c);
self
}
pub fn set_base64_content(mut self, c: &str) -> Attachment {
self.content = String::from(c);
self
}
pub fn set_filename(mut self, filename: &str) -> Attachment {
self.filename = filename.into();
self
}
pub fn set_mime_type(mut self, mime: &str) -> Attachment {
self.mime_type = Some(String::from(mime));
self
}
}