twilight_util/builder/message/
thumbnail.rs

1use twilight_model::channel::message::component::{Thumbnail, UnfurledMediaItem};
2
3/// Create a thumbnail with a builder.
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[must_use = "must be built into a thumbnail"]
6pub struct ThumbnailBuilder(Thumbnail);
7
8impl ThumbnailBuilder {
9    /// Create a new thumbnail builder.
10    pub const fn new(media: UnfurledMediaItem) -> Self {
11        Self(Thumbnail {
12            id: None,
13            media,
14            description: None,
15            spoiler: None,
16        })
17    }
18
19    /// Set the identifier of this thumbnail.
20    pub fn id(mut self, id: i32) -> Self {
21        self.0.id.replace(id);
22
23        self
24    }
25
26    /// Set the media of this thumbnail.
27    pub fn media(mut self, media: impl Into<UnfurledMediaItem>) -> Self {
28        self.0.media = media.into();
29
30        self
31    }
32
33    /// Set the description of this thumbnail.
34    pub fn description(mut self, description: impl Into<String>) -> Self {
35        self.0.description.replace(Some(description.into()));
36
37        self
38    }
39
40    /// Build into a thumbnail.
41    pub fn build(self) -> Thumbnail {
42        self.0
43    }
44}
45
46impl From<ThumbnailBuilder> for Thumbnail {
47    fn from(builder: ThumbnailBuilder) -> Self {
48        builder.build()
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use static_assertions::assert_impl_all;
56    use std::fmt::Debug;
57
58    assert_impl_all!(ThumbnailBuilder: Clone, Debug, Eq, PartialEq, Send, Sync);
59    assert_impl_all!(Thumbnail: From<ThumbnailBuilder>);
60
61    #[test]
62    fn builder() {
63        let media = UnfurledMediaItem {
64            url: "http://www.example.com/example.png".to_string(),
65            proxy_url: None,
66            height: None,
67            width: None,
68            content_type: None,
69        };
70        let expected = Thumbnail {
71            id: None,
72            media: media.clone(),
73            description: None,
74            spoiler: None,
75        };
76
77        let actual = ThumbnailBuilder::new(media).build();
78
79        assert_eq!(actual, expected);
80    }
81}