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        })
18    }
19
20    /// Set the identifier of this file upload.
21    pub const fn id(mut self, id: i32) -> Self {
22        self.0.id.replace(id);
23
24        self
25    }
26
27    /// Set the maximum amount of files to upload.
28    pub const fn max_values(mut self, max_values: u8) -> Self {
29        self.0.max_values.replace(max_values);
30
31        self
32    }
33
34    /// Set the minimum amount of files to upload.
35    pub const fn min_values(mut self, min_values: u8) -> Self {
36        self.0.min_values.replace(min_values);
37
38        self
39    }
40
41    /// Set whether uploading a file is required.
42    pub const fn required(mut self, required: bool) -> Self {
43        self.0.required.replace(required);
44
45        self
46    }
47
48    /// Build into a file upload,
49    pub fn build(self) -> FileUpload {
50        self.0
51    }
52}
53
54impl From<FileUploadBuilder> for FileUpload {
55    fn from(builder: FileUploadBuilder) -> Self {
56        builder.build()
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn builder() {
66        let expected = FileUpload {
67            id: Some(42),
68            custom_id: "custom_id".to_string(),
69            max_values: Some(5),
70            min_values: Some(1),
71            required: Some(true),
72        };
73
74        let actual = FileUploadBuilder::new("custom_id")
75            .id(42)
76            .max_values(5)
77            .min_values(1)
78            .required(true)
79            .build();
80
81        assert_eq!(actual, expected);
82    }
83}