sfr_slack_api/builder/
files_upload.rs

1//! The builder for [`FilesUpload`].
2
3use sfr_types as st;
4
5use crate::api::files_upload::FilesUpload;
6
7/// The builder for [`FilesUpload`].
8#[derive(Debug, Default)]
9pub struct FilesUploadBuilder {
10    /// The [`channels`][`st::FilesUploadRequest::channels`].
11    channels: Vec<String>,
12
13    /// The value of [`content`][`st::FilesUploadRequest::content`].
14    content: Option<String>,
15
16    /// The value of [`content`][`st::FilesUploadRequest::content`].
17    file: Option<String>,
18
19    /// The value of [`content`][`st::FilesUploadRequest::content`].
20    file_in_memory: Option<Vec<u8>>,
21
22    /// The [`filename`][`st::FilesUploadRequest::filename`].
23    filename: Option<String>,
24
25    /// The [`filetype`][`st::FilesUploadRequest::filetype`].
26    filetype: Option<String>,
27
28    /// The [`initial_comment`][`st::FilesUploadRequest::initial_comment`].
29    initial_comment: Option<String>,
30
31    /// The [`thread_ts`][`st::FilesUploadRequest::thread_ts`].
32    thread_ts: Option<String>,
33
34    /// The [`title`][`st::FilesUploadRequest::title`].
35    title: Option<String>,
36}
37
38impl FilesUploadBuilder {
39    /// The constructor.
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// Builds [`FilesUpload`].
45    pub fn build(self) -> Result<FilesUpload, st::Error> {
46        let content = match (self.content, self.file, self.file_in_memory, &self.filename) {
47            (Some(content), _, _, _) => st::FilesUploadRequestContent::Content(content),
48            (None, Some(path), _, Some(filename)) => {
49                st::FilesUploadRequestContent::File(path, filename.clone())
50            }
51            (None, None, Some(bytes), Some(filename)) => {
52                st::FilesUploadRequestContent::FileInMemory(bytes, filename.clone())
53            }
54            _ => {
55                return Err(st::Error::required_value_is_missing(&[
56                    "content",
57                    "file/filename",
58                ]))
59            }
60        };
61
62        let result = st::FilesUploadRequest {
63            channels: self.channels,
64            content,
65            filename: self.filename,
66            filetype: self.filetype,
67            initial_comment: self.initial_comment,
68            thread_ts: self.thread_ts,
69            title: self.title,
70        };
71
72        Ok(result.into())
73    }
74
75    /// Sets [`channels`][`st::FilesUploadRequest::channels`].
76    pub fn channels(mut self, channels: Vec<String>) -> Self {
77        self.channels = channels;
78        self
79    }
80
81    /// Sets [`Content`][`st::FilesUploadRequestContent::Content`] to [`content`][`st::FilesUploadRequest::content`].
82    pub fn content(mut self, content: String) -> Self {
83        self.content = Some(content);
84        self.file = None;
85        self
86    }
87
88    /// Sets [`File`][`st::FilesUploadRequestContent::File`] to [`content`][`st::FilesUploadRequest::content`].
89    pub fn file(mut self, file: String) -> Self {
90        self.file = Some(file);
91        self.content = None;
92        self
93    }
94
95    /// Sets [`filename`][`st::FilesUploadRequest::filename`].
96    pub fn filename(mut self, filename: String) -> Self {
97        self.filename = Some(filename);
98        self
99    }
100
101    /// Sets [`filetype`][`st::FilesUploadRequest::filetype`].
102    pub fn filetype(mut self, filetype: String) -> Self {
103        self.filetype = Some(filetype);
104        self
105    }
106
107    /// Sets [`initial_comment`][`st::FilesUploadRequest::initial_comment`].
108    pub fn initial_comment(mut self, initial_comment: String) -> Self {
109        self.initial_comment = Some(initial_comment);
110        self
111    }
112
113    /// Sets [`thread_ts`][`st::FilesUploadRequest::thread_ts`].
114    pub fn thread_ts(mut self, thread_ts: String) -> Self {
115        self.thread_ts = Some(thread_ts);
116        self
117    }
118
119    /// Sets [`title`][`st::FilesUploadRequest::title`].
120    pub fn title(mut self, title: String) -> Self {
121        self.title = Some(title);
122        self
123    }
124}