1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::send_grid::EmailAddress;
use serde_derive::{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"}}"#);
}
}