sendgrid_async/sendgrid/
message.rs1use data_encoding::BASE64;
2use serde::Serialize;
3#[derive(Default, Serialize)]
6pub struct Message {
7 from: Address,
8 subject: String,
9 personalizations: Vec<super::Personalization>,
10
11 #[serde(skip_serializing_if = "Option::is_none")]
12 content: Option<Vec<Content>>,
13
14 #[serde(skip_serializing_if = "Option::is_none")]
15 attachments: Option<Vec<Attachment>>,
16
17 #[serde(skip_serializing_if = "Option::is_none")]
18 template_id: Option<String>,
19}
20
21impl Message {
22 pub fn new() -> Message {
24 Message::default()
25 }
26
27 pub fn set_from(mut self, from: Address) -> Message {
29 self.from = from;
30 self
31 }
32
33 pub fn set_subject(mut self, subject: &str) -> Message {
35 self.subject = String::from(subject);
36 self
37 }
38
39 pub fn set_template_id(mut self, template_id: &str) -> Message {
41 self.template_id = Some(String::from(template_id));
42 self
43 }
44
45 pub fn add_content(mut self, c: Content) -> Message {
47 match self.content {
48 None => {
49 let mut content = Vec::new();
50 content.push(c);
51 self.content = Some(content);
52 }
53 Some(ref mut content) => content.push(c),
54 };
55 self
56 }
57
58 pub fn add_personalization(mut self, p: super::Personalization) -> Message {
60 self.personalizations.push(p);
61 self
62 }
63
64 pub fn add_attachment(mut self, a: Attachment) -> Message {
66 match self.attachments {
67 None => {
68 let mut attachments = Vec::new();
69 attachments.push(a);
70 self.attachments = Some(attachments);
71 }
72 Some(ref mut attachments) => attachments.push(a),
73 };
74 self
75 }
76}
77#[derive(Clone, Default, Serialize)]
79pub struct Address {
80 email: String,
81
82 #[serde(skip_serializing_if = "Option::is_none")]
83 name: Option<String>,
84}
85
86impl Address {
87 pub fn new() -> Address {
89 Address::default()
90 }
91
92 pub fn set_email(mut self, email: &str) -> Address {
94 self.email = String::from(email);
95 self
96 }
97
98 pub fn set_name(mut self, name: &str) -> Address {
100 self.name = Some(String::from(name));
101 self
102 }
103}
104
105#[derive(Clone, Default, Serialize)]
107pub struct Content {
108 #[serde(rename = "type")]
109 content_type: String,
110 value: String,
111}
112
113impl Content {
114 pub fn new() -> Content {
116 Content::default()
117 }
118
119 pub fn set_content_type(mut self, content_type: &str) -> Content {
121 self.content_type = String::from(content_type);
122 self
123 }
124
125 pub fn set_value(mut self, value: &str) -> Content {
127 self.value = String::from(value);
128 self
129 }
130}
131
132#[derive(Default, Serialize)]
136pub struct Attachment {
137 content: String,
138
139 filename: String,
140
141 #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
142 mime_type: Option<String>,
143
144 #[serde(skip_serializing_if = "Option::is_none")]
145 disposition: Option<String>,
146
147 #[serde(skip_serializing_if = "Option::is_none")]
148 content_id: Option<String>,
149}
150
151impl Attachment {
152 pub fn new() -> Attachment {
154 Attachment::default()
155 }
156
157 pub fn set_content(mut self, c: &[u8]) -> Attachment {
159 self.content = BASE64.encode(c);
160 self
161 }
162
163 pub fn set_base64_content(mut self, c: &str) -> Attachment {
165 self.content = String::from(c);
166 self
167 }
168
169 pub fn set_filename(mut self, filename: &str) -> Attachment {
171 self.filename = filename.into();
172 self
173 }
174
175 pub fn set_mime_type(mut self, mime: &str) -> Attachment {
178 self.mime_type = Some(String::from(mime));
179 self
180 }
181}