sendgrid_rs/attachment.rs
1use serde::Serialize;
2
3/// This is a struct for serializing SendGrid API attachments.
4/// Use `AttachmentBuilder` to construct these.
5#[derive(Serialize, Debug)]
6pub struct Attachment {
7 content: String,
8 #[serde(rename = "type")]
9 a_type: Option<String>,
10 filename: String,
11 disposition: Option<String>,
12 content_id: Option<String>,
13}
14
15/// Builder pattern for creating an `Attachment`
16/// Make sure you call `build()` when done to consume the builder and return the underlying `Attachment`
17pub struct AttachmentBuilder {
18 attachment: Attachment,
19}
20
21impl AttachmentBuilder {
22 /// Constructs an `AttachmentBuilder`. The required parameters are the content (base64 string)
23 /// and the filename.
24 ///
25 /// # Examples
26 /// ```
27 /// # use sendgrid_rs::AttachmentBuilder;
28 ///
29 /// let builder = AttachmentBuilder::new(
30 /// "SGVsbG8gV29ybGQh",
31 /// "file.txt");
32 /// ```
33 pub fn new<S: Into<String>>(content: S, filename: S) -> Self {
34 AttachmentBuilder {
35 attachment: Attachment {
36 content: content.into(),
37 a_type: None,
38 filename: filename.into(),
39 disposition: None,
40 content_id: None,
41 },
42 }
43 }
44
45 /// Sets the mime type on the `Attachment`
46 ///
47 /// # Examples
48 /// ```
49 /// # use sendgrid_rs::AttachmentBuilder;
50 ///
51 /// let builder = AttachmentBuilder::new(
52 /// "SGVsbG8gV29ybGQh",
53 /// "file.txt")
54 /// .attachment_type("text/plain");
55 /// ```
56 pub fn attachment_type(mut self, t: impl Into<String>) -> Self {
57 self.attachment.a_type = Some(t.into());
58 self
59 }
60
61 /// Sets the disposition of the `Attachment`
62 ///
63 /// # Examples
64 /// ```
65 /// # use sendgrid_rs::AttachmentBuilder;
66 ///
67 /// let builder = AttachmentBuilder::new(
68 /// "SGVsbG8gV29ybGQh",
69 /// "file.txt")
70 /// .disposition("inline");
71 /// ```
72 pub fn disposition(mut self, disposition: impl Into<String>) -> Self {
73 self.attachment.disposition = Some(disposition.into());
74 self
75 }
76
77 /// Sets the content_id of the `Attachment`
78 ///
79 /// # Examples
80 /// ```
81 /// # use sendgrid_rs::AttachmentBuilder;
82 ///
83 /// let builder = AttachmentBuilder::new(
84 /// "SGVsbG8gV29ybGQh",
85 /// "file.txt")
86 /// .content_id("id");
87 /// ```
88 pub fn content_id(mut self, id: impl Into<String>) -> Self {
89 self.attachment.content_id = Some(id.into());
90 self
91 }
92
93 /// Consumes the `AttachmentBuilder` and returns the underlying `Attachment`
94 ///
95 /// # Examples
96 /// ```
97 /// # use sendgrid_rs::AttachmentBuilder;
98 ///
99 /// let builder = AttachmentBuilder::new(
100 /// "SGVsbG8gV29ybGQh",
101 /// "file.txt")
102 /// .build();
103 /// ```
104 pub fn build(self) -> Attachment {
105 self.attachment
106 }
107}