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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use twilight_model::channel::embed::*;

/// Create an embed via a builder.
///
/// If uploading an image as an attachment, set as the image or thumbnail with
/// `attachment://{filename}.{extension}`. Refer to [the discord docs] for more information.
///
/// # Examples
///
/// A simple embed:
///
/// ```rust,no_run
/// use twilight_builders::embed::EmbedBuilder;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let embed = EmbedBuilder::new()
///     .description("Here's a list of reasons why Twilight is the best pony:")
///     .add_field("Wings", "She has wings.")
///         .inline()
///         .commit()
///     .add_field("Horn", "She can do magic, and she's really good at it.")
///         .commit()
///     .build();
/// # Ok(()) }
/// ```
///
/// An embed with images:
///
/// ```rust,no_run
/// use twilight_builders::embed::EmbedBuilder;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let embed = EmbedBuilder::new()
///     .description("Here's a cool image of Twilight Sparkle")
///     .image("attachment://bestpony.png")
///     .build();
///
/// # Ok(()) }
/// ```
///
/// [the discord docs]: https://discord.com/developers/docs/resources/channel#create-message-using-attachments-within-embeds
#[derive(Clone, Debug)]
#[must_use = "The embed is not constructed. You need to call build to construct the embed."]
pub struct EmbedBuilder(Embed);

impl EmbedBuilder {
    pub fn new() -> Self {
        EmbedBuilder(Embed {
            author: None,
            color: None,
            description: None,
            fields: vec![],
            footer: None,
            image: None,
            kind: String::from("rich"),
            provider: None,
            thumbnail: None,
            timestamp: None,
            title: None,
            url: None,
            video: None,
        })
    }

    /// Return a new [`AuthorBuilder`].
    ///
    /// [`AuthorBuilder`]: ./struct.AuthorBuilder.html
    pub fn author(self) -> AuthorBuilder {
        AuthorBuilder::new(self)
    }

    /// Set the color.
    ///
    /// Use hexadecimal syntax to specify an integer: `0xD4A4E8`
    pub fn color(mut self, color: u32) -> Self {
        self.0.color.replace(color);
        self
    }

    /// Set the description.
    ///
    /// Limited to 2048 UTF-16 code points.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.0.description.replace(description.into());
        self
    }

    /// Add a field with a new [`FieldBuilder`].
    ///
    /// The name is limited to 256 UTF-16 code points, and the value is limited to 1024. The amount
    /// of fields is limited to 25.
    ///
    /// [`FieldBuilder`]: ./struct.FieldBuilder.html
    pub fn add_field(self, name: impl Into<String>, value: impl Into<String>) -> FieldBuilder {
        FieldBuilder::new(self, name.into(), value.into())
    }

    /// Return a new [`FooterBuilder`].
    ///
    /// The text is limited to 2048 UTF-16 code points.
    ///
    /// [`FooterBuilder`]: ./struct.FooterBuilder.html
    pub fn footer(self, text: impl Into<String>) -> FooterBuilder {
        FooterBuilder::new(self, text.into())
    }

    /// Add an image.
    ///
    /// Either the URL to an image or an `attachment://` path.
    pub fn image(mut self, url: impl Into<String>) -> Self {
        let image = EmbedImage {
            height: None,
            proxy_url: None,
            url: Some(url.into()),
            width: None,
        };
        self.0.image.replace(image);
        self
    }

    /// Add a thumbnail.
    ///
    /// Either the URL to an image or an `attachment://` path.
    pub fn thumbnail(mut self, url: impl Into<String>) -> Self {
        let image = EmbedThumbnail {
            height: None,
            proxy_url: None,
            url: Some(url.into()),
            width: None,
        };
        self.0.thumbnail.replace(image);
        self
    }

    /// Set the ISO 8601 timestamp.
    pub fn timestamp(mut self, timestamp: impl Into<String>) -> Self {
        self.0.timestamp.replace(timestamp.into());
        self
    }

    /// Set the title.
    ///
    /// Limited to 256 UTF-16 code points.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.0.title.replace(title.into());
        self
    }

    /// Set the URL.
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.0.url.replace(url.into());
        self
    }

    /// Return an `Embed`.
    pub fn build(self) -> Embed {
        self.0
    }
}

impl Default for EmbedBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[must_use = "If commit is not run the author will not be changed."]
pub struct AuthorBuilder(EmbedAuthor, EmbedBuilder);

impl AuthorBuilder {
    fn new(ebb: EmbedBuilder) -> Self {
        AuthorBuilder(
            EmbedAuthor {
                icon_url: None,
                name: None,
                proxy_icon_url: None,
                url: None,
            },
            ebb,
        )
    }

    /// The author's name.
    ///
    /// Limited to 256 UTF-16 code points.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.0.name.replace(name.into());
        self
    }

    /// Add an author icon.
    ///
    /// Either the URL to an image or an `attachment://` path.
    pub fn icon_url(mut self, icon_url: impl Into<String>) -> Self {
        self.0.icon_url.replace(icon_url.into());
        self
    }

    /// The author's url.
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.0.url.replace(url.into());
        self
    }

    /// Build the author, and return the embed builder.
    pub fn commit(mut self) -> EmbedBuilder {
        (self.1).0.author.replace(self.0);
        self.1
    }
}

#[must_use = "If commit is not run the field will not be added."]
pub struct FieldBuilder(EmbedField, EmbedBuilder);

impl FieldBuilder {
    fn new(ebb: EmbedBuilder, name: String, value: String) -> Self {
        FieldBuilder(
            EmbedField {
                inline: false,
                name,
                value,
            },
            ebb,
        )
    }

    /// Call if the field should be inline.
    pub fn inline(mut self) -> Self {
        self.0.inline = true;
        self
    }

    /// Build the field, and return the embed builder.
    pub fn commit(mut self) -> EmbedBuilder {
        (self.1).0.fields.push(self.0);
        self.1
    }
}

#[must_use = "If commit is not run the footer will not be added."]
pub struct FooterBuilder(EmbedFooter, EmbedBuilder);

impl FooterBuilder {
    fn new(ebb: EmbedBuilder, text: String) -> Self {
        FooterBuilder(
            EmbedFooter {
                icon_url: None,
                proxy_icon_url: None,
                text,
            },
            ebb,
        )
    }

    /// Add a footer icon.
    ///
    /// Either the URL to an image or an `attachment://` path.
    pub fn icon_url(mut self, url: impl Into<String>) -> Self {
        self.0.icon_url.replace(url.into());
        self
    }

    /// Build the footer, and return the embed builder.
    pub fn commit(mut self) -> EmbedBuilder {
        (self.1).0.footer.replace(self.0);
        self.1
    }
}

#[test]
fn builder_test() {
    let embed = EmbedBuilder::new()
        .color(0x0043FF)
        .description("Description")
        .timestamp("123")
        .footer("Warn")
        .icon_url("icon")
        .commit()
        .add_field("name", "title")
        .inline()
        .commit()
        .build();

    let expected = Embed {
        author: None,
        color: Some(17407),
        description: Some("Description".to_string()),
        fields: [EmbedField {
            inline: true,
            name: "name".to_string(),
            value: "title".to_string(),
        }]
        .to_vec(),
        footer: Some(EmbedFooter {
            icon_url: Some("icon".to_string()),
            proxy_icon_url: None,
            text: "Warn".to_string(),
        }),
        image: None,
        kind: "rich".to_string(),
        provider: None,
        thumbnail: None,
        timestamp: Some("123".to_string()),
        title: None,
        url: None,
        video: None,
    };

    assert_eq!(embed, expected);
}