1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! The builder for [`FilesUpload`].

use sfr_types as st;

use crate::api::files_upload::FilesUpload;

/// The builder for [`FilesUpload`].
#[derive(Debug, Default)]
pub struct FilesUploadBuilder {
    /// The [`channels`][`st::FilesUploadRequest::channels`].
    channels: Vec<String>,

    /// The value of [`content`][`st::FilesUploadRequest::content`].
    content: Option<String>,

    /// The value of [`content`][`st::FilesUploadRequest::content`].
    file: Option<String>,

    /// The value of [`content`][`st::FilesUploadRequest::content`].
    file_in_memory: Option<Vec<u8>>,

    /// The [`filename`][`st::FilesUploadRequest::filename`].
    filename: Option<String>,

    /// The [`filetype`][`st::FilesUploadRequest::filetype`].
    filetype: Option<String>,

    /// The [`initial_comment`][`st::FilesUploadRequest::initial_comment`].
    initial_comment: Option<String>,

    /// The [`thread_ts`][`st::FilesUploadRequest::thread_ts`].
    thread_ts: Option<String>,

    /// The [`title`][`st::FilesUploadRequest::title`].
    title: Option<String>,
}

impl FilesUploadBuilder {
    /// The constructor.
    pub fn new() -> Self {
        Self::default()
    }

    /// Builds [`FilesUpload`].
    pub fn build(self) -> Result<FilesUpload, st::Error> {
        let content = match (self.content, self.file, self.file_in_memory, &self.filename) {
            (Some(content), _, _, _) => st::FilesUploadRequestContent::Content(content),
            (None, Some(path), _, Some(filename)) => {
                st::FilesUploadRequestContent::File(path, filename.clone())
            }
            (None, None, Some(bytes), Some(filename)) => {
                st::FilesUploadRequestContent::FileInMemory(bytes, filename.clone())
            }
            _ => {
                return Err(st::Error::required_value_is_missing(&[
                    "content",
                    "file/filename",
                ]))
            }
        };

        let result = st::FilesUploadRequest {
            channels: self.channels,
            content,
            filename: self.filename,
            filetype: self.filetype,
            initial_comment: self.initial_comment,
            thread_ts: self.thread_ts,
            title: self.title,
        };

        Ok(result.into())
    }

    /// Sets [`channels`][`st::FilesUploadRequest::channels`].
    pub fn channels(mut self, channels: Vec<String>) -> Self {
        self.channels = channels;
        self
    }

    /// Sets [`Content`][`st::FilesUploadRequestContent::Content`] to [`content`][`st::FilesUploadRequest::content`].
    pub fn content(mut self, content: String) -> Self {
        self.content = Some(content);
        self.file = None;
        self
    }

    /// Sets [`File`][`st::FilesUploadRequestContent::File`] to [`content`][`st::FilesUploadRequest::content`].
    pub fn file(mut self, file: String) -> Self {
        self.file = Some(file);
        self.content = None;
        self
    }

    /// Sets [`filename`][`st::FilesUploadRequest::filename`].
    pub fn filename(mut self, filename: String) -> Self {
        self.filename = Some(filename);
        self
    }

    /// Sets [`filetype`][`st::FilesUploadRequest::filetype`].
    pub fn filetype(mut self, filetype: String) -> Self {
        self.filetype = Some(filetype);
        self
    }

    /// Sets [`initial_comment`][`st::FilesUploadRequest::initial_comment`].
    pub fn initial_comment(mut self, initial_comment: String) -> Self {
        self.initial_comment = Some(initial_comment);
        self
    }

    /// Sets [`thread_ts`][`st::FilesUploadRequest::thread_ts`].
    pub fn thread_ts(mut self, thread_ts: String) -> Self {
        self.thread_ts = Some(thread_ts);
        self
    }

    /// Sets [`title`][`st::FilesUploadRequest::title`].
    pub fn title(mut self, title: String) -> Self {
        self.title = Some(title);
        self
    }
}