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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! The builder for [`ChatPostMessage`].

use sfr_types as st;

use crate::api::chat_post_message::ChatPostMessage;

/// The builder for [`ChatPostMessage`].
#[derive(Debug, Default)]
pub struct ChatPostMessageBuilder {
    /// The [`channel`][`st::ChatPostMessageRequest::channel`].
    channel: Option<String>,

    /// The value of [`content`][`st::ChatPostMessageRequest::content`].
    attachments: Option<Vec<(String, String)>>,

    /// The value of [`content`][`st::ChatPostMessageRequest::content`].
    blocks: Option<Vec<st::Block>>,

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

    /// The [`as_user`][`st::ChatPostMessageRequest::as_user`].
    as_user: Option<bool>,

    /// The [`icon_emoji`][`st::ChatPostMessageRequest::icon_emoji`].
    icon_emoji: Option<String>,

    /// The [`icon_url`][`st::ChatPostMessageRequest::icon_url`].
    icon_url: Option<String>,

    /// The [`link_names`][`st::ChatPostMessageRequest::link_names`].
    link_names: Option<bool>,

    /// The [`metadata`][`st::ChatPostMessageRequest::metadata`].
    metadata: Option<String>,

    /// The [`mrkdwn`][`st::ChatPostMessageRequest::mrkdwn`].
    mrkdwn: Option<bool>,

    /// The [`parse`][`st::ChatPostMessageRequest::parse`].
    parse: Option<String>,

    /// The [`reply_broadcast`][`st::ChatPostMessageRequest::reply_broadcast`].
    reply_broadcast: Option<bool>,

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

    /// The [`unfurl_links`][`st::ChatPostMessageRequest::unfurl_links`].
    unfurl_links: Option<bool>,

    /// The [`unfurl_media`][`st::ChatPostMessageRequest::unfurl_media`].
    unfurl_media: Option<bool>,

    /// The [`username`][`st::ChatPostMessageRequest::username`].
    username: Option<String>,
}

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

    /// Builds [`ChatPostMessage`].
    pub fn build(self) -> Result<ChatPostMessage, st::Error> {
        let channel = self
            .channel
            .ok_or_else(|| st::Error::required_value_is_missing(&["channel"]))?;

        let content = match (self.attachments, self.blocks, self.text) {
            (Some(attachments), _, _) => {
                let attachments = attachments
                    .into_iter()
                    .map(|attachments| st::Attachments {
                        pretext: attachments.0,
                        text: attachments.1,
                    })
                    .collect();
                st::ChatPostMessageContent::Attachments(attachments)
            }

            (None, Some(blocks), _) => st::ChatPostMessageContent::Blocks(blocks.clone()),

            (None, None, Some(text)) => st::ChatPostMessageContent::Text(text.clone()),

            (None, None, None) => {
                return Err(st::Error::required_value_is_missing(&[
                    "attachments",
                    "blocks",
                    "text",
                ]))
            }
        };

        let result = st::ChatPostMessageRequest {
            channel,
            content,
            as_user: self.as_user,
            icon_emoji: self.icon_emoji,
            icon_url: self.icon_url,
            link_names: self.link_names,
            metadata: self.metadata,
            mrkdwn: self.mrkdwn,
            parse: self.parse,
            reply_broadcast: self.reply_broadcast,
            thread_ts: self.thread_ts,
            unfurl_links: self.unfurl_links,
            unfurl_media: self.unfurl_media,
            username: self.username,
        };

        Ok(result.into())
    }

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

    /// Sets [`Attachments`][`st::ChatPostMessageContent::Attachments`] to [`content`][`st::ChatPostMessageRequest::content`].
    pub fn attachments(mut self, attachments: Vec<(String, String)>) -> Self {
        self.attachments = Some(attachments);
        self.blocks = None;
        self.text = None;
        self
    }

    /// Sets [`Blocks`][`st::ChatPostMessageContent::Blocks`] to [`content`][`st::ChatPostMessageRequest::content`].
    pub fn blocks(mut self, blocks: Vec<st::Block>) -> Self {
        self.attachments = None;
        self.blocks = Some(blocks);
        self.text = None;
        self
    }

    /// Sets [`Text`][`st::ChatPostMessageContent::Text`] to [`content`][`st::ChatPostMessageRequest::content`].
    pub fn text(mut self, text: String) -> Self {
        self.attachments = None;
        self.blocks = None;
        self.text = Some(text);
        self
    }

    /// Sets [`as_user`][`st::ChatPostMessageRequest::as_user`].
    #[allow(clippy::wrong_self_convention)]
    pub fn as_user(mut self, as_user: bool) -> Self {
        self.as_user = Some(as_user);
        self
    }

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

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

    /// Sets [`link_names`][`st::ChatPostMessageRequest::link_names`].
    pub fn link_names(mut self, link_names: bool) -> Self {
        self.link_names = Some(link_names);
        self
    }

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

    /// Sets [`mrkdwn`][`st::ChatPostMessageRequest::mrkdwn`].
    pub fn mrkdwn(mut self, mrkdwn: bool) -> Self {
        self.mrkdwn = Some(mrkdwn);
        self
    }

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

    /// Sets [`reply_broadcast`][`st::ChatPostMessageRequest::reply_broadcast`].
    pub fn reply_broadcast(mut self, reply_broadcast: bool) -> Self {
        self.reply_broadcast = Some(reply_broadcast);
        self
    }

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

    /// Sets [`unfurl_links`][`st::ChatPostMessageRequest::unfurl_links`].
    pub fn unfurl_links(mut self, unfurl_links: bool) -> Self {
        self.unfurl_links = Some(unfurl_links);
        self
    }

    /// Sets [`unfurl_media`][`st::ChatPostMessageRequest::unfurl_media`].
    pub fn unfurl_media(mut self, unfurl_media: bool) -> Self {
        self.unfurl_media = Some(unfurl_media);
        self
    }

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