azure_functions/send_grid/
personalization.rs

1use crate::send_grid::EmailAddress;
2use serde::{Deserialize, Serialize};
3use serde_json::{Map, Value};
4use std::collections::HashMap;
5
6/// Represents a personalization of an email message.
7///
8/// Defines who should receive an individual message and how that message should be handled.
9///
10/// Fields in personalizations will override the fields of the same name from the message level.
11#[derive(Debug, Default, Clone, Serialize, Deserialize)]
12pub struct Personalization {
13    /// The list of email recipients.
14    #[serde(skip_serializing_if = "Vec::is_empty")]
15    pub to: Vec<EmailAddress>,
16    /// The list of recipients who will receive a carbon copy of the email.
17    #[serde(skip_serializing_if = "Vec::is_empty")]
18    pub cc: Vec<EmailAddress>,
19    /// The list of recipients who will receive a blind carbon copy of the email.
20    #[serde(skip_serializing_if = "Vec::is_empty")]
21    pub bcc: Vec<EmailAddress>,
22    /// The subject line of the email.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub subject: Option<String>,
25    /// The email message headers.
26    #[serde(skip_serializing_if = "HashMap::is_empty")]
27    pub headers: HashMap<String, String>,
28    /// The map of substitution tags to substitution values.
29    /// Substitutions will apply to the content of the email, in addition to the subject and reply-to parameters.
30    #[serde(skip_serializing_if = "HashMap::is_empty")]
31    pub substitutions: HashMap<String, String>,
32    /// The map of custom argument to value.
33    /// Custom arguments will be carried along with the email, activity data, and links.
34    #[serde(skip_serializing_if = "HashMap::is_empty")]
35    pub custom_args: HashMap<String, String>,
36    /// The unix timestamp specifying when the email should be sent from Twilio SendGrid.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub send_at: Option<i64>,
39    /// The template data to use for Handlebars based templates.
40    #[serde(
41        rename = "dynamic_template_data",
42        skip_serializing_if = "Option::is_none"
43    )]
44    pub template_data: Option<Map<String, Value>>,
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use serde_json::to_string;
51
52    #[test]
53    fn it_serializes_to_json() {
54        let mut headers = HashMap::new();
55        headers.insert("foo".to_owned(), "bar".to_owned());
56
57        let mut substitutions = HashMap::new();
58        substitutions.insert("key".to_owned(), "value".to_owned());
59
60        let mut custom_args = HashMap::new();
61        custom_args.insert("baz".to_owned(), "jam".to_owned());
62
63        let mut template_data = Map::<String, Value>::new();
64        template_data.insert("hello".to_owned(), Value::String("world".to_owned()));
65
66        let json = to_string(&Personalization {
67            to: vec![
68                EmailAddress {
69                    email: "foo@example.com".to_owned(),
70                    ..Default::default()
71                },
72                EmailAddress {
73                    email: "bar@example.com".to_owned(),
74                    name: Some("Bar Baz".to_owned()),
75                },
76            ],
77            cc: vec![
78                EmailAddress {
79                    email: "baz@example.com".to_owned(),
80                    ..Default::default()
81                },
82                EmailAddress {
83                    email: "jam@example.com".to_owned(),
84                    name: Some("Jam".to_owned()),
85                },
86            ],
87            bcc: vec![
88                EmailAddress {
89                    email: "cake@example.com".to_owned(),
90                    ..Default::default()
91                },
92                EmailAddress {
93                    email: "lie@example.com".to_owned(),
94                    name: Some("Lie".to_owned()),
95                },
96            ],
97            subject: Some("hello world".to_owned()),
98            headers,
99            substitutions,
100            custom_args,
101            send_at: Some(12345),
102            template_data: Some(template_data),
103        })
104        .unwrap();
105
106        assert_eq!(
107            json,
108            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"}}"#
109        );
110    }
111}