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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use std::convert::{TryFrom, TryInto};

use serde::{Deserialize, Serialize};
use validator::Validate;

use crate::{convert,
            elems,
            elems::{select,
                    Button,
                    Checkboxes,
                    DatePicker,
                    Overflow,
                    Radio,
                    TextInput},
            val_helpr::ValidationResult};

/// # Actions Block
///
/// [slack api docs 🔗]
///
/// A block that is used to hold interactive [elements 🔗]
///
/// [slack api docs 🔗]: https://api.slack.com/reference/block-kit/blocks#actions
/// [elements 🔗]: https://api.slack.com/reference/messaging/block-elements
#[derive(Clone,
           Debug,
           Default,
           Deserialize,
           Hash,
           PartialEq,
           Serialize,
           Validate)]
pub struct Contents<'a> {
  #[validate(length(max = 5))]
  elements: Vec<BlockElement<'a>>,

  #[serde(skip_serializing_if = "Option::is_none")]
  #[validate(length(max = 255))]
  block_id: Option<String>,
}

impl<'a> Contents<'a> {
  /// Create an empty Actions block (shorthand for `Default::default()`)
  ///
  /// # Example
  /// ```
  /// use slack_blocks::blocks::{actions, Block};
  ///
  /// let actions = actions::Contents::new();
  /// let block: Block = actions.into();
  /// // < send block to slack's API >
  /// ```
  pub fn new() -> Self {
    Default::default()
  }

  /// Set the `block_id` for interactions on an existing `actions::Contents`
  ///
  /// # Arguments
  /// - `block_id` - A string acting as a unique identifier for a block.
  ///     You can use this `block_id` when you receive an interaction payload
  ///     to [identify the source of the action 🔗].
  ///     If not specified, a `block_id` will be generated.
  ///     Maximum length for this field is 255 characters.
  ///
  /// [identify the source of the action 🔗]: https://api.slack.com/interactivity/handling#payloads
  ///
  /// # Example
  /// ```
  /// use slack_blocks::blocks::{actions, Block};
  ///
  /// let actions = actions::Contents::new().with_block_id("tally_ho");
  /// let block: Block = actions.into();
  /// // < send block to slack's API >
  /// ```
  pub fn with_block_id(mut self, block_id: impl ToString) -> Self {
    self.block_id = Some(block_id.to_string());
    self
  }

  /// Populate an Actions block with a collection of `elems::BlockElement`s,
  /// which may not be supported by `Actions` blocks.
  ///
  /// If you _can_ create a collection of `actions::BlockElement`,
  /// either by creating them directly or invoking `elems::BlockElement::into`,
  /// use `from_action_elements`.
  ///
  /// # Arguments
  /// - `elements` - An array of interactive [element objects 🔗]
  ///     For a list of `BlockElement` types that are, see `BlockElement`.
  ///     There is a maximum of 5 elements in each action block.
  ///
  /// [element objects 🔗]: https://api.slack.com/reference/messaging/block-elements
  ///
  /// # Errors
  /// Errors if the `elems::BlockElement` is one that is not supported by
  /// `Actions` blocks.
  ///
  /// For a list of `BlockElement` types that are supported, see `::blocks::actions::BlockElement`.
  ///
  /// # Runtime Validation
  ///
  /// **only** validates that the block elements are compatible with `Actions`,
  /// for full runtime model validation see the `validate` method.
  ///
  /// # Example
  /// ```
  /// use slack_blocks::{blocks::{actions, Block},
  ///                    compose,
  ///                    elems};
  ///
  /// # pub fn main() -> Result<(), ()> {
  /// let btn = elems::Button::from_text_and_action_id("Button", "123");
  /// let actions = actions::Contents::from_elements(vec![btn.into()])?;
  /// let block: Block = actions.into();
  /// // < send block to slack's API >
  /// # Ok(())
  /// # }
  /// ```
  pub fn from_elements<Iter>(elements: Iter) -> Result<Self, ()>
    where Iter: IntoIterator<Item = elems::BlockElement<'a>>
  {
    elements.into_iter().collect::<Vec<_>>().try_into()
  }

  /// Populate an Actions block with a collection of `BlockElement`s that
  /// are supported by `Actions` blocks.
  ///
  /// This also can be called via the `From<Vec<self::BlockElement>>` implementation.
  ///
  /// If you have a collection of elements that may not be supported,
  /// see `from_elements`.
  ///
  /// # Arguments
  /// - `elements` - An array of interactive [element objects 🔗]
  ///     For a list of `BlockElement` types that are supported, see `BlockElement`.
  ///     There is a maximum of 5 elements in each action block.
  ///     Note that if you only ever want 1 item you can choose to pass it `Some(element)` OR `std::iter::once(element)`
  ///     instead of a `Vec`, bypassing an expensive allocation.
  ///     [Iterator and Option implement IntoIterator 🔗].
  ///
  /// [element objects 🔗]: https://api.slack.com/reference/messaging/block-elements
  /// [Iterator and Option implement IntoIterator 🔗]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#impl-IntoIterator-28
  ///
  /// # Errors
  /// Errors if the `elems::BlockElement` is one that is not supported by
  /// `Actions` blocks.
  ///
  /// # Runtime Validation
  /// **only** validates that the block elements are compatible with `Actions`,
  /// for full runtime model validation see the `validate` method.
  ///
  /// # Example
  /// ```
  /// use slack_blocks::{blocks::{actions, Block},
  ///                    compose,
  ///                    elems};
  ///
  /// # pub fn main() {
  /// let btn = elems::Button::from_text_and_action_id("Button", "123");
  /// let actions = actions::Contents::from_action_elements(vec![btn.into()]);
  /// let block: Block = actions.into();
  ///
  /// // < send block to slack's API >
  /// # }
  /// ```
  pub fn from_action_elements<Iter>(elements: Iter) -> Self
    where Iter: IntoIterator<Item = self::BlockElement<'a>>
  {
    elements.into_iter().collect::<Vec<_>>().into()
  }

  /// Validate that this Section block agrees with Slack's model requirements
  ///
  /// # Errors
  /// - If `with_block_id` was called with a block id longer
  ///     than 255 chars
  /// - If `from_elements` or `from_action_elements` was
  ///     called with more than 5 elements.
  ///
  /// # Example
  /// ```
  /// use slack_blocks::{blocks, compose};
  ///
  /// let long_string = std::iter::repeat(' ').take(256).collect::<String>();
  ///
  /// let block = blocks::actions
  ///     ::Contents
  ///     ::from_action_elements(vec![])
  ///     .with_block_id(long_string);
  ///
  /// assert!(matches!(block.validate(), Err(_)));
  /// ```
  pub fn validate(&self) -> ValidationResult {
    Validate::validate(self)
  }
}

/// The Block Elements supported in an Action Block.
///
/// This list was pulled from the docs for all [block elements 🔗],
/// where each declares the blocks it is usable in.
///
/// [block elements 🔗]: https://api.slack.com/reference/block-kit/block-elements
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
pub enum BlockElement<'a> {
  Button(Button),
  Checkboxes(Checkboxes<'a>),
  DatePicker(DatePicker<'a>),
  Overflow(Overflow<'a>),
  TextInput(TextInput<'a>),
  RadioButtons(Radio<'a>),

  /// All Select types are supported.
  SelectPublicChannel(select::PublicChannel<'a>),

  /// All Select types are supported.
  SelectConversation(select::Conversation<'a>),

  /// All Select types are supported.
  SelectUser(select::User<'a>),

  /// All Select types are supported.
  SelectExternal(select::External<'a>),

  /// All Select types are supported.
  SelectStatic(select::Static<'a>),
}

convert!(impl<'a> From<Vec<self::BlockElement<'a>>> for Contents<'a>
    => |elements| Self {
        elements,
        ..Default::default()
    }
);

impl<'a> TryFrom<elems::BlockElement<'a>> for Contents<'a> {
  type Error = ();
  fn try_from(element: elems::BlockElement<'a>) -> Result<Self, Self::Error> {
    self::BlockElement::<'a>::try_from(element)
      .map(|el| Self::from_action_elements(std::iter::once(el)))
  }
}

impl<'a> TryFrom<Vec<elems::BlockElement<'a>>> for Contents<'a> {
  type Error = ();
  fn try_from(elements: Vec<elems::BlockElement<'a>>)
              -> Result<Self, Self::Error> {
    elements.into_iter()
            .map(self::BlockElement::<'a>::try_from)
            .collect::<Result<Vec<_>, _>>()
            .map(self::Contents::<'a>::from)
  }
}

impl<'a> TryFrom<elems::BlockElement<'a>> for self::BlockElement<'a> {
  type Error = ();
  fn try_from(el: elems::BlockElement<'a>) -> Result<Self, Self::Error> {
    use elems::BlockElement as El;

    use self::BlockElement::*;

    match el {
      | El::SelectPublicChannel(sel) => Ok(SelectPublicChannel(sel)),
      | El::SelectConversation(sel) => Ok(SelectConversation(sel)),
      | El::SelectExternal(sel) => Ok(SelectExternal(sel)),
      | El::SelectStatic(sel) => Ok(SelectStatic(sel)),
      | El::SelectUser(sel) => Ok(SelectUser(sel)),
      | El::Overflow(o) => Ok(Overflow(o)),
      | El::RadioButtons(r) => Ok(RadioButtons(r)),
      | El::Button(cts) => Ok(Button(cts)),
      | El::TextInput(t) => Ok(TextInput(t)),
      | El::Checkboxes(c) => Ok(Checkboxes(c)),
      | El::DatePicker(d) => Ok(DatePicker(d)),
      | _ => Err(()),
    }
  }
}

convert!(impl<'a> From<select::PublicChannel<'a>> for self::BlockElement<'a> => |s| self::BlockElement::SelectPublicChannel(s));
convert!(impl<'a> From<select::Conversation<'a>> for self::BlockElement<'a>  => |s| self::BlockElement::SelectConversation(s));
convert!(impl<'a> From<select::User<'a>> for self::BlockElement<'a>  => |s| self::BlockElement::SelectUser(s));
convert!(impl<'a> From<select::External<'a>> for self::BlockElement<'a>  => |s| self::BlockElement::SelectExternal(s));
convert!(impl<'a> From<select::Static<'a>> for self::BlockElement<'a>  => |s| self::BlockElement::SelectStatic(s));
convert!(impl     From<Button> for self::BlockElement<'static> => |b| self::BlockElement::Button(b));
convert!(impl<'a> From<Radio<'a>> for self::BlockElement<'a> => |b| self::BlockElement::RadioButtons(b));
convert!(impl<'a> From<TextInput<'a>> for self::BlockElement<'a> => |t| self::BlockElement::TextInput(t));
convert!(impl<'a> From<DatePicker<'a>> for self::BlockElement<'a> => |t| self::BlockElement::DatePicker(t));