twilight_util/builder/embed/
author.rs

1//! Create embed authors.
2
3use super::ImageSource;
4use twilight_model::channel::message::embed::EmbedAuthor;
5
6/// Create an embed author with a builder.
7///
8/// This can be passed into [`EmbedBuilder::author`].
9///
10/// [`EmbedBuilder::author`]: crate::builder::embed::EmbedBuilder::author
11#[derive(Clone, Debug, Eq, PartialEq)]
12#[must_use = "must be built into an embed author"]
13pub struct EmbedAuthorBuilder(EmbedAuthor);
14
15impl EmbedAuthorBuilder {
16    /// Create a new embed author builder.
17    pub fn new(name: impl Into<String>) -> Self {
18        Self(EmbedAuthor {
19            icon_url: None,
20            name: name.into(),
21            proxy_icon_url: None,
22            url: None,
23        })
24    }
25
26    /// Build into an embed author.
27    #[allow(clippy::missing_const_for_fn)]
28    #[must_use = "should be used as part of an embed builder"]
29    pub fn build(self) -> EmbedAuthor {
30        self.0
31    }
32
33    /// Add an author icon.
34    pub fn icon_url(mut self, image_source: ImageSource) -> Self {
35        self.0.icon_url.replace(image_source.0);
36
37        self
38    }
39
40    /// The author's url.
41    pub fn url(mut self, url: impl Into<String>) -> Self {
42        self.0.url = Some(url.into());
43
44        self
45    }
46}
47
48impl From<EmbedAuthorBuilder> for EmbedAuthor {
49    /// Convert an embed author builder into an embed author.
50    ///
51    /// This is equivalent to calling [`EmbedAuthorBuilder::build`].
52    fn from(builder: EmbedAuthorBuilder) -> Self {
53        builder.build()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use static_assertions::assert_impl_all;
61    use std::fmt::Debug;
62
63    assert_impl_all!(EmbedAuthorBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
64    assert_impl_all!(EmbedAuthor: From<EmbedAuthorBuilder>);
65
66    #[test]
67    fn builder() {
68        let expected = EmbedAuthor {
69            icon_url: Some("https://example.com/1.png".to_owned()),
70            name: "an author".to_owned(),
71            proxy_icon_url: None,
72            url: Some("https://example.com".to_owned()),
73        };
74
75        let source = ImageSource::url("https://example.com/1.png").unwrap();
76        let actual = EmbedAuthorBuilder::new("an author")
77            .icon_url(source)
78            .url("https://example.com")
79            .build();
80
81        assert_eq!(actual, expected);
82    }
83}