twilight_util/builder/message/
file_display.rs

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