Skip to main content

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    #[must_use = "should be used as part of an embed builder"]
28    pub fn build(self) -> EmbedAuthor {
29        self.0
30    }
31
32    /// Add an author icon.
33    pub fn icon_url(mut self, image_source: ImageSource) -> Self {
34        self.0.icon_url.replace(image_source.0);
35
36        self
37    }
38
39    /// The author's url.
40    pub fn url(mut self, url: impl Into<String>) -> Self {
41        self.0.url = Some(url.into());
42
43        self
44    }
45}
46
47impl From<EmbedAuthorBuilder> for EmbedAuthor {
48    /// Convert an embed author builder into an embed author.
49    ///
50    /// This is equivalent to calling [`EmbedAuthorBuilder::build`].
51    fn from(builder: EmbedAuthorBuilder) -> Self {
52        builder.build()
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use static_assertions::assert_impl_all;
60    use std::fmt::Debug;
61
62    assert_impl_all!(EmbedAuthorBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
63    assert_impl_all!(EmbedAuthor: From<EmbedAuthorBuilder>);
64
65    #[test]
66    fn builder() {
67        let expected = EmbedAuthor {
68            icon_url: Some("https://example.com/1.png".to_owned()),
69            name: "an author".to_owned(),
70            proxy_icon_url: None,
71            url: Some("https://example.com".to_owned()),
72        };
73
74        let source = ImageSource::url("https://example.com/1.png").unwrap();
75        let actual = EmbedAuthorBuilder::new("an author")
76            .icon_url(source)
77            .url("https://example.com")
78            .build();
79
80        assert_eq!(actual, expected);
81    }
82}