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
use serde::Serialize;

/// This is a struct for serializing SendGrid API attachments.
/// Use `AttachmentBuilder` to construct these.
#[derive(Serialize, Debug)]
pub struct Attachment {
    content: String,
    #[serde(rename = "type")]
    a_type: Option<String>,
    filename: String,
    disposition: Option<String>,
    content_id: Option<String>,
}

/// Builder pattern for creating an `Attachment`
/// Make sure you call `build()` when done to consume the builder and return the underlying `Attachment`
pub struct AttachmentBuilder {
    attachment: Attachment,
}

impl AttachmentBuilder {
    /// Constructs an `AttachmentBuilder`. The required parameters are the content (base64 string)
    /// and the filename.
    ///
    /// # Examples
    /// ```
    /// # use sendgrid_rs::AttachmentBuilder;
    ///
    /// let builder = AttachmentBuilder::new(
    ///                 "SGVsbG8gV29ybGQh",
    ///                 "file.txt");
    /// ```
    pub fn new<S: Into<String>>(content: S, filename: S) -> Self {
        AttachmentBuilder {
            attachment: Attachment {
                content: content.into(),
                a_type: None,
                filename: filename.into(),
                disposition: None,
                content_id: None,
            },
        }
    }

    /// Sets the mime type on the `Attachment`
    ///
    /// # Examples
    /// ```
    /// # use sendgrid_rs::AttachmentBuilder;
    ///
    /// let builder = AttachmentBuilder::new(
    ///                 "SGVsbG8gV29ybGQh",
    ///                 "file.txt")
    ///               .attachment_type("text/plain");
    /// ```
    pub fn attachment_type(mut self, t: impl Into<String>) -> Self {
        self.attachment.a_type = Some(t.into());
        self
    }

    /// Sets the disposition of the `Attachment`
    ///
    /// # Examples
    /// ```
    /// # use sendgrid_rs::AttachmentBuilder;
    ///
    /// let builder = AttachmentBuilder::new(
    ///                 "SGVsbG8gV29ybGQh",
    ///                 "file.txt")
    ///               .disposition("inline");
    /// ```
    pub fn disposition(mut self, disposition: impl Into<String>) -> Self {
        self.attachment.disposition = Some(disposition.into());
        self
    }

    /// Sets the content_id of the `Attachment`
    ///
    /// # Examples
    /// ```
    /// # use sendgrid_rs::AttachmentBuilder;
    ///
    /// let builder = AttachmentBuilder::new(
    ///                 "SGVsbG8gV29ybGQh",
    ///                 "file.txt")
    ///               .content_id("id");
    /// ```
    pub fn content_id(mut self, id: impl Into<String>) -> Self {
        self.attachment.content_id = Some(id.into());
        self
    }

    /// Consumes the `AttachmentBuilder` and returns the underlying `Attachment`
    ///
    /// # Examples
    /// ```
    /// # use sendgrid_rs::AttachmentBuilder;
    ///
    /// let builder = AttachmentBuilder::new(
    ///                 "SGVsbG8gV29ybGQh",
    ///                 "file.txt")
    ///               .build();
    /// ```
    pub fn build(self) -> Attachment {
        self.attachment
    }
}