sendgrid_rs/personalization.rs
1use crate::Contact;
2use serde::Serialize;
3use std::collections::HashMap;
4
5/// Used to structure and serialize the personalization node in Sendgrid's API call. Use
6/// `PersonalizationBuilder` to construct this.
7#[derive(Serialize, Default, Debug)]
8pub struct Personalization {
9 #[serde(skip_serializing_if = "Vec::is_empty")]
10 to: Vec<Contact>,
11 #[serde(skip_serializing_if = "Vec::is_empty")]
12 cc: Vec<Contact>,
13 #[serde(skip_serializing_if = "Vec::is_empty")]
14 bcc: Vec<Contact>,
15 #[serde(skip_serializing_if = "Option::is_none")]
16 subject: Option<String>,
17 #[serde(skip_serializing_if = "HashMap::is_empty")]
18 headers: HashMap<String, String>,
19 #[serde(skip_serializing_if = "HashMap::is_empty")]
20 substitutions: HashMap<String, String>,
21 #[serde(skip_serializing_if = "HashMap::is_empty")]
22 dynamic_template_data: HashMap<String, String>,
23 #[serde(skip_serializing_if = "HashMap::is_empty")]
24 custom_args: HashMap<String, String>,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 send_at: Option<i32>,
27}
28
29/// Builder pattern for `Personalization`. Make sure you call `build()` when done to consume this
30/// and return the underlying `Personalization`. Use default() to construct.
31#[derive(Default)]
32pub struct PersonalizationBuilder {
33 personalization: Personalization,
34}
35
36impl PersonalizationBuilder {
37 /// Add a `To` contact. Use `ContactBuilder` to construct this.
38 ///
39 /// # Examples
40 /// ```
41 /// # use sendgrid_rs::{ContactBuilder, PersonalizationBuilder};
42 ///
43 /// let builder = PersonalizationBuilder::default()
44 /// .to(ContactBuilder::new("to@example.com").build());
45 /// ```
46 pub fn to(mut self, contact: Contact) -> Self {
47 self.personalization.to.push(contact);
48 self
49 }
50
51 /// Add a `CC` contact. Use `ContactBuilder` to construct this.
52 ///
53 /// # Examples
54 /// ```
55 /// # use sendgrid_rs::{ContactBuilder, PersonalizationBuilder};
56 ///
57 /// let builder = PersonalizationBuilder::default()
58 /// .cc(ContactBuilder::new("cc@example.com").build());
59 /// ```
60 pub fn cc(mut self, contact: Contact) -> Self {
61 self.personalization.cc.push(contact);
62 self
63 }
64
65 /// Add a `BCC` contact. Use `ContactBuilder` to construct this.
66 ///
67 /// # Examples
68 /// ```
69 /// # use sendgrid_rs::{ContactBuilder, PersonalizationBuilder};
70 ///
71 /// let builder = PersonalizationBuilder::default()
72 /// .bcc(ContactBuilder::new("bcc@example.com").build());
73 /// ```
74 pub fn bcc(mut self, contact: Contact) -> Self {
75 self.personalization.bcc.push(contact);
76 self
77 }
78
79 /// Set a custom subject line.
80 ///
81 /// # Examples
82 /// ```
83 /// # use sendgrid_rs::PersonalizationBuilder;
84 ///
85 /// let builder = PersonalizationBuilder::default()
86 /// .subject("Subject line");
87 /// ```
88 pub fn subject(mut self, subject: impl Into<String>) -> Self {
89 self.personalization.subject = Some(subject.into());
90 self
91 }
92
93 /// Set an email header.
94 ///
95 /// # Parameters
96 /// key: impl Into<String>
97 /// value: impl Into<String>
98 ///
99 /// # Examples
100 /// ```
101 /// # use sendgrid_rs::PersonalizationBuilder;
102 ///
103 /// let builder = PersonalizationBuilder::default()
104 /// .header("Key", "Value");
105 /// ```
106 pub fn header<S: Into<String>>(mut self, key: S, value: S) -> Self {
107 self.personalization
108 .headers
109 .insert(key.into(), value.into());
110 self
111 }
112
113 /// Assign multiple email headers
114 ///
115 /// # Parameters
116 /// data: HashMap<String, String>
117 ///
118 /// # Examples
119 /// ```
120 /// # use sendgrid_rs::PersonalizationBuilder;
121 /// # use std::collections::HashMap;
122 ///
123 /// let headers: HashMap<String, String> = HashMap::new();
124 /// let builder = PersonalizationBuilder::default()
125 /// .headers(headers);
126 /// ```
127 pub fn headers(mut self, data: HashMap<String, String>) -> Self {
128 self.personalization.headers = data;
129 self
130 }
131
132 /// Set a substitution
133 ///
134 /// # Parameters
135 /// key: impl Into<String>
136 /// value: impl Into<String>
137 ///
138 /// # Examples
139 /// ```
140 /// # use sendgrid_rs::PersonalizationBuilder;
141 ///
142 /// let builder = PersonalizationBuilder::default()
143 /// .substitution("Key", "Value");
144 /// ```
145 pub fn substitution<S: Into<String>>(mut self, key: S, value: S) -> Self {
146 self.personalization
147 .substitutions
148 .insert(key.into(), value.into());
149 self
150 }
151
152 /// Add a single dynamic template substitution
153 ///
154 /// # Parameters
155 /// key: impl Into<String>
156 /// value: impl Into<String>
157 ///
158 /// # Examples
159 /// ```
160 /// # use sendgrid_rs::PersonalizationBuilder;
161 ///
162 /// let builder = PersonalizationBuilder::default()
163 /// .dynamic_template_datum("Key", "Value");
164 /// ```
165 pub fn dynamic_template_datum<S: Into<String>>(mut self, key: S, value: S) -> Self {
166 self.personalization
167 .dynamic_template_data
168 .insert(key.into(), value.into());
169 self
170 }
171
172 /// Assign multiple dynamic template substitutions, overwriting all synamic template
173 /// substitutions with supplied data
174 ///
175 /// # Parameters
176 /// data: HashMap<String, String>
177 ///
178 /// # Examples
179 /// ```
180 /// # use sendgrid_rs::PersonalizationBuilder;
181 /// # use std::collections::HashMap;
182 ///
183 /// let substitutions: HashMap<String, String> = HashMap::new();
184 /// let builder = PersonalizationBuilder::default()
185 /// .dynamic_template_data(substitutions);
186 /// ```
187 pub fn dynamic_template_data(mut self, data: HashMap<String, String>) -> Self {
188 self.personalization.dynamic_template_data = data;
189 self
190 }
191
192 /// Set a custom_arg
193 ///
194 /// # Parameters
195 /// key: impl Into<String>
196 /// value: impl Into<String>
197 ///
198 /// # Examples
199 /// ```
200 /// # use sendgrid_rs::PersonalizationBuilder;
201 ///
202 /// let builder = PersonalizationBuilder::default()
203 /// .custom_arg("Key", "Value");
204 /// ```
205 pub fn custom_arg<S: Into<String>>(mut self, key: S, value: S) -> Self {
206 self.personalization
207 .custom_args
208 .insert(key.into(), value.into());
209 self
210 }
211
212 /// Set a send_at time in seconds
213 ///
214 /// # Examples
215 /// ```
216 /// # use sendgrid_rs::PersonalizationBuilder;
217 ///
218 /// let builder = PersonalizationBuilder::default()
219 /// .send_at(3600);
220 /// ```
221 pub fn send_at(mut self, time: i32) -> Self {
222 self.personalization.send_at = Some(time);
223 self
224 }
225
226 /// Consume the builder and return the underlying 'Personalization'
227 ///
228 /// # Examples
229 /// ```
230 /// # use sendgrid_rs::PersonalizationBuilder;
231 ///
232 /// let builder = PersonalizationBuilder::default()
233 /// .build();
234 /// ```
235 pub fn build(self) -> Personalization {
236 self.personalization
237 }
238}