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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//! Create embeds.

use super::image_source::ImageSource;
use std::{
    convert::TryFrom,
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::channel::embed::{
    Embed, EmbedAuthor, EmbedField, EmbedFooter, EmbedImage, EmbedThumbnail,
};

/// Error building an embed.
///
/// This is returned from [`EmbedBuilder::build`].
///
/// [`EmbedBuilder::build`]: struct.EmbedBuilder.html#method.build
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum EmbedBuildError {
    /// The total content of the embed is too large.
    ///
    /// Refer to [`EmbedBuilder::EMBED_LENGTH_LIMIT`] for more information about
    /// what goes into this limit.
    ///
    /// [`EmbedBuilder::EMBED_LENGTH_LIMIT`]: struct.EmbedBuilder.html#const.EMBED_LENGTH_LIMIT
    ContentTooLarge {
        /// The total length of the embed.
        length: usize,
    },
    /// Too many fields were provided.
    ///
    /// Refer to [`EmbedBuilder::EMBED_FIELD_LIMIT`] for more information about
    /// what the limit is.
    ///
    /// [`EmbedBuilder::EMBED_FIELD_LIMIT`]: struct.EmbedBuilder.html#const.EMBED_FIELD_LIMIT
    TooManyFields {
        /// The provided fields.
        fields: Vec<EmbedField>,
    },
}

impl Display for EmbedBuildError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::ContentTooLarge { .. } => f.write_str("the content of the embed is too large"),
            Self::TooManyFields { .. } => f.write_str("more than 25 fields were provided"),
        }
    }
}

impl Error for EmbedBuildError {}

/// Error working with an embed builder.
///
/// This is returned from [`EmbedBuilder::color`].
///
/// [`EmbedBuilder::color`]: struct.EmbedBuilder.html#method.color
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum EmbedColorError {
    /// Color was larger than a valid RGB hexadecimal value.
    NotRgb {
        /// Provided color hex value.
        color: u32,
    },
    /// Color was 0. The value would be thrown out by Discord and is equivalent
    /// to null.
    Zero,
}

impl Display for EmbedColorError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::NotRgb { color } => f.write_fmt(format_args!("the color {} is invalid", color)),
            Self::Zero => f.write_str("the given color value is 0, which is not acceptable"),
        }
    }
}

impl Error for EmbedColorError {}

/// Error adding a description to an embed builder.
///
/// This is returned from [`EmbedBuilder::description`].
///
/// [`EmbedBuilder::description`]: struct.EmbedBuilder.html#method.description
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum EmbedDescriptionError {
    /// Description is empty.
    Empty {
        /// Provided description. Although empty, the same owned allocation is
        /// included.
        description: String,
    },
    /// Description is longer than 2048 UTF-16 code points.
    TooLong {
        /// Provided description.
        description: String,
    },
}

impl Display for EmbedDescriptionError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Empty { .. } => f.write_str("the description is empty"),
            Self::TooLong { .. } => f.write_str("the description is too long"),
        }
    }
}

impl Error for EmbedDescriptionError {}

/// Error adding a title to an embed builder.
///
/// This is returned from [`EmbedBuilder::title`].
///
/// [`EmbedBuilder::title`]: struct.EmbedBuilder.html#method.title
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum EmbedTitleError {
    /// Title is empty.
    Empty {
        /// Provided title. Although empty, the same owned allocation is
        /// included.
        title: String,
    },
    /// Title is longer than 256 UTF-16 code points.
    TooLong {
        /// Provided title.
        title: String,
    },
}

impl Display for EmbedTitleError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match self {
            Self::Empty { .. } => f.write_str("the title is empty"),
            Self::TooLong { .. } => f.write_str("the title is too long"),
        }
    }
}

impl Error for EmbedTitleError {}

/// Create an embed with a builder.
///
/// # Examples
///
/// Refer to the [crate-level documentation] for examples.
///
/// [crate-level documentation]: ../index.html
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use = "must be built into an embed"]
pub struct EmbedBuilder(Embed);

impl EmbedBuilder {
    /// The maximum accepted color value.
    ///
    /// This is used by [`color`].
    ///
    /// [`color`]: #method.color
    pub const COLOR_MAXIMUM: u32 = 0xff_ff_ff;

    /// The maximum number of UTF-16 code points that can be in a description.
    ///
    /// This is used by [`description`].
    ///
    /// [`description`]: #method.description
    pub const DESCRIPTION_LENGTH_LIMIT: usize = 2048;

    /// The maximum number of fields that can be in an embed.
    ///
    /// This is used by [`build`].
    ///
    /// [`build`]: #method.build
    pub const EMBED_FIELD_LIMIT: usize = 25;

    /// The maximum total textual length of the embed in UTF-16 code points.
    ///
    /// This combines the text of the author name, description, footer text,
    /// field names and values, and title.
    ///
    /// This is used by [`build`].
    ///
    /// [`build`]: #method.build
    pub const EMBED_LENGTH_LIMIT: usize = 6000;

    /// The maximum number of UTF-16 code points that can be in a title.
    ///
    /// This is used by [`title`].
    ///
    /// [`title`]: #method.title
    pub const TITLE_LENGTH_LIMIT: usize = 256;

    /// Create a new default embed builder.
    ///
    /// See the [crate-level documentation] for examples and additional
    /// information.
    ///
    /// This is equivalent to the [default implementation].
    ///
    /// [crate-level documentation]: ../index.html
    /// [default implementation]: #impl-Default
    pub fn new() -> Self {
        Self::default()
    }

    /// Build this into an embed.
    ///
    /// # Errors
    ///
    /// Returns [`EmbedBuildError::ContentTooLarge`] if the textual content of
    /// the embed is too large. Refer to [`EMBED_LENGTH_LIMIT`] for the limit
    /// value and what counts towards it.
    ///
    /// Returns [`EmbedBuildError::TooManyFields`] if there are too many fields
    /// in the embed. Refer to [`EMBED_FIELD_LIMIT`] for the limit value.
    ///
    /// [`EMBED_FIELD_LIMIT`]: #const.EMBED_FIELD_LIMIT
    /// [`EMBED_LENGTH_LIMIT`]: #const.EMBED_LENGTH_LIMIT
    /// [`EmbedBuildError::TooManyFields`]: enum.EmbedBuildError.html#variant.TooManyFields
    #[must_use = "should be used as part of something like a message"]
    pub fn build(self) -> Result<Embed, EmbedBuildError> {
        if self.0.fields.len() > Self::EMBED_FIELD_LIMIT {
            return Err(EmbedBuildError::TooManyFields {
                fields: self.0.fields,
            });
        }

        let mut total = 0;

        if let Some(name) = self
            .0
            .author
            .as_ref()
            .and_then(|author| author.name.as_ref())
        {
            total += name.chars().count();
        }

        if let Some(description) = self.0.description.as_ref() {
            total += description.chars().count();
        }

        if let Some(footer) = self.0.footer.as_ref() {
            total += footer.text.chars().count();
        }

        for field in &self.0.fields {
            total += field.name.chars().count() + field.value.chars().count();
        }

        if let Some(title) = self.0.title.as_ref() {
            total += title.chars().count();
        }

        if total > Self::EMBED_LENGTH_LIMIT {
            return Err(EmbedBuildError::ContentTooLarge { length: total });
        }

        Ok(self.0)
    }

    /// Set the author.
    ///
    /// # Examples
    ///
    /// Create an embed author:
    ///
    /// ```rust
    /// use twilight_embed_builder::{EmbedAuthorBuilder, EmbedBuilder};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let author = EmbedAuthorBuilder::new()
    ///     .name("Twilight")?
    ///     .url("https://github.com/twilight-rs/twilight")
    ///     .build();
    ///
    /// let embed = EmbedBuilder::new().author(author).build();
    /// # Ok(()) }
    /// ```
    pub fn author(self, author: impl Into<EmbedAuthor>) -> Self {
        self._author(author.into())
    }

    fn _author(mut self, author: EmbedAuthor) -> Self {
        self.0.author.replace(author);

        self
    }

    /// Set the color.
    ///
    /// This must be a valid hexadecimal RGB value. `0x000000` is not an
    /// acceptable value as it would be thrown out by Discord.
    ///
    /// # Examples
    ///
    /// Set the color of an embed to `0xfd69b3`:
    ///
    /// ```rust
    /// use twilight_embed_builder::EmbedBuilder;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new()
    ///     .color(0xfd_69_b3)?
    ///     .description("a description")?
    ///     .build();
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`EmbedColorError::NotRgb`] if the provided color is not a valid
    /// RGB integer. Refer to [`COLOR_MAXIMUM`] to know what the maximum
    /// accepted value is.
    ///
    /// Returns [`EmbedColorError::Zero`] if the provided color is 0, which is not
    /// an acceptable value.
    ///
    /// [`COLOR_MAXIMUM`]: #const.COLOR_MAXIMUM
    /// [`EmbedColorError::NotRgb`]: enum.EmbedColorError.html#variant.NotRgb
    /// [`EmbedColorError::Zero`]: enum.EmbedColorError.html#variant.Zero
    pub fn color(mut self, color: u32) -> Result<Self, EmbedColorError> {
        if color == 0 {
            return Err(EmbedColorError::Zero);
        }

        if color > Self::COLOR_MAXIMUM {
            return Err(EmbedColorError::NotRgb { color });
        }

        self.0.color.replace(color);

        Ok(self)
    }

    /// Set the description.
    ///
    /// Refer to [`DESCRIPTION_LENGTH_LIMIT`] for the maximum number of UTF-16
    /// code points that can be in a description.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use twilight_embed_builder::EmbedBuilder;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new().description("this is an embed")?.build();
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`EmbedDescriptionError::TooLong`] if the provided
    /// description is longer than the maximum number of code points.
    ///
    /// [`DESCRIPTION_LENGTH_LIMIT`]: #const.DESCRIPTION_LENGTH_LIMIT
    /// [`EmbedDescriptionError::TooLong`]: enum.EmbedDescriptionError.html#variant.TooLong
    pub fn description(
        self,
        description: impl Into<String>,
    ) -> Result<Self, EmbedDescriptionError> {
        self._description(description.into())
    }

    fn _description(mut self, description: String) -> Result<Self, EmbedDescriptionError> {
        if description.is_empty() {
            return Err(EmbedDescriptionError::Empty { description });
        }

        if description.chars().count() > Self::DESCRIPTION_LENGTH_LIMIT {
            return Err(EmbedDescriptionError::TooLong { description });
        }

        self.0.description.replace(description);

        Ok(self)
    }

    /// Add a field to the embed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use twilight_embed_builder::{EmbedBuilder, EmbedFieldBuilder};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new()
    ///     .description("this is an embed")?
    ///     .field(EmbedFieldBuilder::new("a field", "and its value")?)
    ///     .build()?;
    /// # Ok(()) }
    /// ```
    pub fn field(self, field: impl Into<EmbedField>) -> Self {
        self._field(field.into())
    }

    fn _field(mut self, field: EmbedField) -> Self {
        self.0.fields.push(field);

        self
    }

    /// Set the footer of the embed.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use twilight_embed_builder::{EmbedBuilder, EmbedFooterBuilder};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new()
    ///     .description("this is an embed")?
    ///     .footer(EmbedFooterBuilder::new("a footer")?)
    ///     .build()?;
    /// # Ok(()) }
    /// ```
    pub fn footer(self, footer: impl Into<EmbedFooter>) -> Self {
        self._footer(footer.into())
    }

    fn _footer(mut self, footer: EmbedFooter) -> Self {
        self.0.footer.replace(footer);

        self
    }

    /// Set the image.
    ///
    /// # Examples
    ///
    /// Set the image source to a URL:
    ///
    /// ```rust
    /// use twilight_embed_builder::{EmbedBuilder, EmbedFooterBuilder, ImageSource};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let source = ImageSource::url("https://raw.githubusercontent.com/twilight-rs/twilight/trunk/logo.png")?;
    /// let embed = EmbedBuilder::new()
    ///     .footer(EmbedFooterBuilder::new("twilight")?)
    ///     .image(source)
    ///     .build()?;
    /// # Ok(()) }
    /// ```
    pub fn image(mut self, image_source: ImageSource) -> Self {
        self.0.image.replace(EmbedImage {
            height: None,
            proxy_url: None,
            url: Some(image_source.0),
            width: None,
        });

        self
    }

    /// Add a thumbnail.
    ///
    /// # Examples
    ///
    /// Set the thumbnail to an image attachment with the filename
    /// `"twilight.png"`:
    ///
    /// ```rust
    /// use twilight_embed_builder::{EmbedBuilder, ImageSource};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new()
    ///     .description("a picture of twilight")?
    ///     .thumbnail(ImageSource::attachment("twilight.png")?)
    ///     .build()?;
    /// # Ok(()) }
    /// ```
    pub fn thumbnail(mut self, image_source: ImageSource) -> Self {
        self.0.thumbnail.replace(EmbedThumbnail {
            height: None,
            proxy_url: None,
            url: Some(image_source.0),
            width: None,
        });

        self
    }

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

    fn _timestamp(mut self, timestamp: String) -> Self {
        self.0.timestamp.replace(timestamp);

        self
    }

    /// Set the title.
    ///
    /// Refer to [`TITLE_LENGTH_LIMIT`] for the maximum number of UTF-16 code
    /// points that can be in a title.
    ///
    /// # Examples
    ///
    /// Set the title to "twilight":
    ///
    /// ```rust
    /// use twilight_embed_builder::EmbedBuilder;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new()
    ///     .title("twilight")?
    ///     .url("https://github.com/twilight-rs/twilight")
    ///     .build()?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`EmbedTitleError::Empty`] if the provided title is empty.
    ///
    /// Returns [`EmbedTitleError::TooLong`] if the provided title is longer
    /// than the limit defined at [`TITLE_LENGTH_LIMIT`].
    ///
    /// [`TITLE_LENGTH_LIMIT`]: #const.TITLE_LENGTH_LIMIT
    /// [`EmbedTitleError::Empty`]: enum.EmbedTitleError.html#variant.Empty
    /// [`EmbedTitleError::TooLong`]: enum.EmbedTitleError.html#variant.TooLong
    pub fn title(self, title: impl Into<String>) -> Result<Self, EmbedTitleError> {
        self._title(title.into())
    }

    fn _title(mut self, title: String) -> Result<Self, EmbedTitleError> {
        if title.is_empty() {
            return Err(EmbedTitleError::Empty { title });
        }

        if title.chars().count() > Self::TITLE_LENGTH_LIMIT {
            return Err(EmbedTitleError::TooLong { title });
        }

        self.0.title.replace(title);

        Ok(self)
    }

    /// Set the URL.
    ///
    /// # Examples
    ///
    /// Set the URL to [twilight's repository]:
    ///
    /// ```rust
    /// use twilight_embed_builder::{EmbedBuilder, EmbedFooterBuilder};
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let embed = EmbedBuilder::new()
    ///     .description("twilight's repository")?
    ///     .url("https://github.com/twilight-rs/twilight")
    ///     .build()?;
    /// # Ok(()) }
    /// ```
    ///
    /// [twilight's repository]: https://github.com/twilight-rs/twilight
    pub fn url(self, url: impl Into<String>) -> Self {
        self._url(url.into())
    }

    fn _url(mut self, url: String) -> Self {
        self.0.url.replace(url);

        self
    }
}

impl Default for EmbedBuilder {
    /// Create an embed builder with a default embed.
    ///
    /// All embeds have a "rich" type.
    fn default() -> Self {
        EmbedBuilder(Embed {
            author: None,
            color: None,
            description: None,
            fields: Vec::new(),
            footer: None,
            image: None,
            kind: String::from("rich"),
            provider: None,
            thumbnail: None,
            timestamp: None,
            title: None,
            url: None,
            video: None,
        })
    }
}

impl TryFrom<EmbedBuilder> for Embed {
    type Error = EmbedBuildError;

    /// Convert an embed builder into an embed.
    ///
    /// This is equivalent to calling [`EmbedBuilder::build`].
    ///
    /// [`EmbedBuilder::build`]: #method.build
    fn try_from(builder: EmbedBuilder) -> Result<Self, Self::Error> {
        builder.build()
    }
}

#[cfg(test)]
mod tests {
    use super::{
        EmbedBuildError, EmbedBuilder, EmbedColorError, EmbedDescriptionError, EmbedTitleError,
    };
    use crate::{field::EmbedFieldBuilder, footer::EmbedFooterBuilder, image_source::ImageSource};
    use static_assertions::{assert_fields, assert_impl_all, const_assert};
    use std::{convert::TryFrom, error::Error, fmt::Debug};
    use twilight_model::channel::embed::{Embed, EmbedField, EmbedFooter};

    assert_impl_all!(
        EmbedBuildError: Clone,
        Debug,
        Error,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    assert_fields!(EmbedBuildError::ContentTooLarge: length);
    assert_fields!(EmbedBuildError::TooManyFields: fields);
    assert_impl_all!(
        EmbedColorError: Clone,
        Debug,
        Error,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    assert_fields!(EmbedColorError::NotRgb: color);
    assert_impl_all!(
        EmbedDescriptionError: Clone,
        Debug,
        Error,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    assert_fields!(EmbedDescriptionError::Empty: description);
    assert_fields!(EmbedDescriptionError::TooLong: description);
    assert_impl_all!(
        EmbedTitleError: Clone,
        Debug,
        Error,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    assert_fields!(EmbedTitleError::Empty: title);
    assert_fields!(EmbedTitleError::TooLong: title);
    assert_impl_all!(
        EmbedBuilder: Clone,
        Debug,
        Default,
        Eq,
        PartialEq,
        Send,
        Sync
    );
    const_assert!(EmbedBuilder::COLOR_MAXIMUM == 0xff_ff_ff);
    const_assert!(EmbedBuilder::DESCRIPTION_LENGTH_LIMIT == 2048);
    const_assert!(EmbedBuilder::EMBED_FIELD_LIMIT == 25);
    const_assert!(EmbedBuilder::EMBED_LENGTH_LIMIT == 6000);
    const_assert!(EmbedBuilder::TITLE_LENGTH_LIMIT == 256);
    assert_impl_all!(Embed: TryFrom<EmbedBuilder>);

    #[test]
    fn test_color_error() -> Result<(), Box<dyn Error>> {
        assert!(matches!(
            EmbedBuilder::new().color(0).unwrap_err(),
            EmbedColorError::Zero
        ));
        assert!(matches!(
            EmbedBuilder::new().color(u32::MAX).unwrap_err(),
            EmbedColorError::NotRgb { color }
            if color == u32::MAX
        ));

        Ok(())
    }

    #[test]
    fn test_description_error() {
        assert!(matches!(
            EmbedBuilder::new().description("").unwrap_err(),
            EmbedDescriptionError::Empty { description }
            if description.is_empty()
        ));
        let description_too_long = EmbedBuilder::DESCRIPTION_LENGTH_LIMIT + 1;
        assert!(matches!(
            EmbedBuilder::new().description("a".repeat(description_too_long)).unwrap_err(),
            EmbedDescriptionError::TooLong { description }
            if description.len() == description_too_long
        ));
    }

    #[test]
    fn test_title_error() {
        assert!(matches!(
            EmbedBuilder::new().title("").unwrap_err(),
            EmbedTitleError::Empty { title }
            if title.is_empty()
        ));
        let title_too_long = EmbedBuilder::TITLE_LENGTH_LIMIT + 1;
        assert!(matches!(
            EmbedBuilder::new().title("a".repeat(title_too_long)).unwrap_err(),
            EmbedTitleError::TooLong { title }
            if title.len() == title_too_long
        ));
    }

    #[test]
    fn test_builder() -> Result<(), Box<dyn Error>> {
        let footer_image = ImageSource::url(
            "https://raw.githubusercontent.com/twilight-rs/twilight/trunk/logo.png",
        )?;
        let embed = EmbedBuilder::new()
            .color(0x00_43_ff)?
            .description("Description")?
            .timestamp("123")
            .footer(EmbedFooterBuilder::new("Warn")?.icon_url(footer_image))
            .field(EmbedFieldBuilder::new("name", "title")?.inline())
            .build()?;

        let expected = Embed {
            author: None,
            color: Some(0x00_43_ff),
            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(
                    "https://raw.githubusercontent.com/twilight-rs/twilight/trunk/logo.png"
                        .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);

        Ok(())
    }
}