1use crate::{Attachment, Result, SlackText};
2use reqwest::Url;
3use serde::{Serialize, Serializer};
4
5#[derive(Serialize, Debug, Default)]
9pub struct Payload {
10 #[serde(skip_serializing_if = "Option::is_none")]
13 pub text: Option<SlackText>,
14 #[serde(skip_serializing_if = "Option::is_none")]
18 pub channel: Option<String>,
19 #[serde(skip_serializing_if = "Option::is_none")]
21 pub username: Option<String>,
22 #[serde(skip_serializing_if = "Option::is_none")]
24 pub icon_url: Option<Url>,
25 #[serde(skip_serializing_if = "Option::is_none")]
28 pub icon_emoji: Option<String>,
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub attachments: Option<Vec<Attachment>>,
32 #[serde(skip_serializing_if = "Option::is_none")]
35 pub unfurl_links: Option<bool>,
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub unfurl_media: Option<bool>,
39 #[serde(skip_serializing_if = "Option::is_none")]
41 pub link_names: Option<u8>,
42 #[serde(skip_serializing_if = "Option::is_none")]
44 pub parse: Option<Parse>,
45}
46
47#[derive(Debug)]
49pub enum Parse {
50 Full,
52 None,
54}
55
56impl Serialize for Parse {
57 fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
58 where
59 S: Serializer,
60 {
61 let st = match *self {
62 Parse::Full => "full",
63 Parse::None => "none",
64 };
65 serializer.serialize_str(st)
66 }
67}
68#[derive(Debug)]
70#[must_use]
71pub struct PayloadBuilder {
72 inner: Result<Payload>,
73}
74
75impl Default for PayloadBuilder {
76 fn default() -> PayloadBuilder {
77 PayloadBuilder {
78 inner: Ok(Default::default()),
79 }
80 }
81}
82
83impl PayloadBuilder {
84 pub fn new() -> Self {
86 Default::default()
87 }
88
89 pub fn text<S: Into<SlackText>>(mut self, text: S) -> Self {
91 if let Ok(inner) = &mut self.inner {
92 inner.text = Some(text.into());
93 }
94 self
95 }
96
97 pub fn channel<S: Into<String>>(mut self, channel: S) -> Self {
99 if let Ok(inner) = &mut self.inner {
100 inner.channel = Some(channel.into());
101 }
102 self
103 }
104
105 pub fn username<S: Into<String>>(mut self, username: S) -> Self {
107 if let Ok(inner) = &mut self.inner {
108 inner.username = Some(username.into());
109 }
110 self
111 }
112
113 pub fn icon_emoji<S: Into<String>>(mut self, icon_emoji: S) -> Self {
115 if let Ok(inner) = &mut self.inner {
116 inner.icon_emoji = Some(icon_emoji.into());
117 }
118 self
119 }
120
121 url_builder_fn! {
122 icon_url, Self
124 }
125
126 pub fn attachments(mut self, attachments: Vec<Attachment>) -> Self {
128 if let Ok(inner) = &mut self.inner {
129 inner.attachments = Some(attachments);
130 }
131 self
132 }
133
134 pub fn unfurl_links(mut self, b: bool) -> Self {
137 if let Ok(inner) = &mut self.inner {
138 inner.unfurl_links = Some(b);
139 }
140 self
141 }
142
143 pub fn unfurl_media(mut self, b: bool) -> Self {
145 if let Ok(inner) = &mut self.inner {
146 inner.unfurl_media = Some(b);
147 }
148 self
149 }
150
151 pub fn link_names(mut self, b: bool) -> Self {
155 if let Ok(inner) = &mut self.inner {
156 inner.link_names = Some(u8::from(b));
157 }
158 self
159 }
160
161 pub fn parse(mut self, p: Parse) -> Self {
163 if let Ok(inner) = &mut self.inner {
164 inner.parse = Some(p);
165 }
166 self
167 }
168
169 pub fn build(self) -> Result<Payload> {
171 self.inner
172 }
173}