use crate::send_grid::EmailAddress;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::HashMap;
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Personalization {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub to: Vec<EmailAddress>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub cc: Vec<EmailAddress>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub bcc: Vec<EmailAddress>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subject: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub headers: HashMap<String, String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub substitutions: HashMap<String, String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub custom_args: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub send_at: Option<i64>,
#[serde(
rename = "dynamic_template_data",
skip_serializing_if = "Option::is_none"
)]
pub template_data: Option<Map<String, Value>>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::to_string;
#[test]
fn it_serializes_to_json() {
let mut headers = HashMap::new();
headers.insert("foo".to_owned(), "bar".to_owned());
let mut substitutions = HashMap::new();
substitutions.insert("key".to_owned(), "value".to_owned());
let mut custom_args = HashMap::new();
custom_args.insert("baz".to_owned(), "jam".to_owned());
let mut template_data = Map::<String, Value>::new();
template_data.insert("hello".to_owned(), Value::String("world".to_owned()));
let json = to_string(&Personalization {
to: vec![
EmailAddress {
email: "foo@example.com".to_owned(),
..Default::default()
},
EmailAddress {
email: "bar@example.com".to_owned(),
name: Some("Bar Baz".to_owned()),
},
],
cc: vec![
EmailAddress {
email: "baz@example.com".to_owned(),
..Default::default()
},
EmailAddress {
email: "jam@example.com".to_owned(),
name: Some("Jam".to_owned()),
},
],
bcc: vec![
EmailAddress {
email: "cake@example.com".to_owned(),
..Default::default()
},
EmailAddress {
email: "lie@example.com".to_owned(),
name: Some("Lie".to_owned()),
},
],
subject: Some("hello world".to_owned()),
headers,
substitutions,
custom_args,
send_at: Some(12345),
template_data: Some(template_data),
})
.unwrap();
assert_eq!(
json,
r#"{"to":[{"email":"foo@example.com"},{"email":"bar@example.com","name":"Bar Baz"}],"cc":[{"email":"baz@example.com"},{"email":"jam@example.com","name":"Jam"}],"bcc":[{"email":"cake@example.com"},{"email":"lie@example.com","name":"Lie"}],"subject":"hello world","headers":{"foo":"bar"},"substitutions":{"key":"value"},"custom_args":{"baz":"jam"},"send_at":12345,"dynamic_template_data":{"hello":"world"}}"#
);
}
}