Skip to main content

twilight_util/builder/message/
file_upload.rs

1use twilight_model::channel::message::component::FileUpload;
2
3/// Create a file upload with a builder.
4#[derive(Clone, Debug, Eq, PartialEq)]
5#[must_use = "must be built into a file upload"]
6pub struct FileUploadBuilder(FileUpload);
7
8impl FileUploadBuilder {
9    /// Create a new file upload builder.
10    pub fn new(custom_id: impl Into<String>) -> Self {
11        Self(FileUpload {
12            id: None,
13            custom_id: custom_id.into(),
14            max_values: None,
15            min_values: None,
16            required: None,
17            file_types: None,
18        })
19    }
20
21    /// Set the identifier of this file upload.
22    pub const fn id(mut self, id: i32) -> Self {
23        self.0.id.replace(id);
24
25        self
26    }
27
28    /// Set the maximum amount of files to upload.
29    pub const fn max_values(mut self, max_values: u8) -> Self {
30        self.0.max_values.replace(max_values);
31
32        self
33    }
34
35    /// Set the minimum amount of files to upload.
36    pub const fn min_values(mut self, min_values: u8) -> Self {
37        self.0.min_values.replace(min_values);
38
39        self
40    }
41
42    /// Set the file types to filter for.
43    ///
44    /// Can be `image`, `video`, `audio`, or any dot-prefixed extension such as `.pdf`.
45    pub fn file_types(mut self, file_types: impl IntoIterator<Item = impl Into<String>>) -> Self {
46        self.0.file_types = Some(file_types.into_iter().map(Into::into).collect());
47
48        self
49    }
50
51    /// Set whether uploading a file is required.
52    pub const fn required(mut self, required: bool) -> Self {
53        self.0.required.replace(required);
54
55        self
56    }
57
58    /// Build into a file upload,
59    pub fn build(self) -> FileUpload {
60        self.0
61    }
62}
63
64impl From<FileUploadBuilder> for FileUpload {
65    fn from(builder: FileUploadBuilder) -> Self {
66        builder.build()
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn builder() {
76        let expected = FileUpload {
77            id: Some(42),
78            custom_id: "custom_id".to_string(),
79            max_values: Some(5),
80            min_values: Some(1),
81            file_types: None,
82            required: Some(true),
83        };
84
85        let actual = FileUploadBuilder::new("custom_id")
86            .id(42)
87            .max_values(5)
88            .min_values(1)
89            .required(true)
90            .build();
91
92        assert_eq!(actual, expected);
93    }
94}