Skip to main content

twilight_util/builder/embed/
footer.rs

1//! Create embed footers.
2
3use super::ImageSource;
4use twilight_model::channel::message::embed::EmbedFooter;
5
6/// Create an embed footer with a builder.
7///
8/// This can be passed into [`EmbedBuilder::footer`].
9///
10/// [`EmbedBuilder::footer`]: crate::builder::embed::EmbedBuilder::footer
11#[derive(Clone, Debug, Eq, PartialEq)]
12#[must_use = "must be built into an embed footer"]
13pub struct EmbedFooterBuilder(EmbedFooter);
14
15impl EmbedFooterBuilder {
16    /// Create a new embed footer builder.
17    ///
18    /// Refer to [`FOOTER_TEXT_LENGTH`] for the maximum number of UTF-16 code
19    /// points that can be in a footer's text.
20    ///
21    /// [`FOOTER_TEXT_LENGTH`]: twilight_validate::embed::FOOTER_TEXT_LENGTH
22    pub fn new(text: impl Into<String>) -> Self {
23        Self(EmbedFooter {
24            icon_url: None,
25            proxy_icon_url: None,
26            text: text.into(),
27        })
28    }
29
30    /// Build into an embed footer.
31    #[must_use = "should be used as part of an embed builder"]
32    pub fn build(self) -> EmbedFooter {
33        self.0
34    }
35
36    /// Add a footer icon.
37    ///
38    /// # Examples
39    ///
40    /// Create a footer by Twilight with a URL to an image of its logo:
41    ///
42    /// ```
43    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
44    /// use twilight_util::builder::embed::{EmbedFooterBuilder, ImageSource};
45    ///
46    /// let icon_url =
47    ///     ImageSource::url("https://raw.githubusercontent.com/twilight-rs/twilight/main/logo.png")?;
48    /// let footer = EmbedFooterBuilder::new("Twilight")
49    ///     .icon_url(icon_url)
50    ///     .build();
51    /// # Ok(()) }
52    /// ```
53    pub fn icon_url(mut self, image_source: ImageSource) -> Self {
54        self.0.icon_url.replace(image_source.0);
55
56        self
57    }
58}
59
60impl From<EmbedFooterBuilder> for EmbedFooter {
61    /// Convert an embed footer builder into an embed footer.
62    ///
63    /// This is equivalent to calling [`EmbedFooterBuilder::build`].
64    fn from(builder: EmbedFooterBuilder) -> Self {
65        builder.build()
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use static_assertions::assert_impl_all;
73    use std::fmt::Debug;
74
75    assert_impl_all!(EmbedFooterBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
76    assert_impl_all!(EmbedFooter: From<EmbedFooterBuilder>);
77
78    #[test]
79    fn builder() {
80        let expected = EmbedFooter {
81            icon_url: Some("https://example.com/1.png".to_owned()),
82            proxy_icon_url: None,
83            text: "a footer".to_owned(),
84        };
85        let image = ImageSource::url("https://example.com/1.png").unwrap();
86        let actual = EmbedFooterBuilder::new("a footer").icon_url(image).build();
87        assert_eq!(actual, expected);
88    }
89}