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
//! # Block Elements - interactive components
//! [slack api docs 🔗](https://api.slack.com/reference/block-kit/block-elements)
//!
//! Block elements can be used inside of `section`, `context`, and `actions` [layout blocks 🔗].
//! Inputs can only be used inside of `input` blocks.
//!
//! Our overview of [app surfaces that support Block Kit 🔗] shows you where those blocks might be relevant.
//!
//! Finally, our [handling user interactivity guide 🔗] will help you prepare your app to allow
//! for the use of the interactive components listed below.
//!
//! [app surfaces that support Block Kit 🔗]: https://api.slack.com/messaging/composing/layouts
//! [handling user interactivity guide 🔗]: https://api.slack.com/interactivity/handling
//! [layout blocks 🔗]: https://api.slack.com/reference/block-kit/blocks

use serde::{Deserialize, Serialize};

use crate::{convert, val_helpr::ValidationResult};

pub mod button;
pub mod checkboxes;
pub mod date_picker;
pub mod image;
pub mod overflow;
pub mod radio;
pub mod select;
pub mod text_input;

#[doc(inline)]
pub use button::Button;
#[doc(inline)]
pub use checkboxes::Checkboxes;
#[doc(inline)]
pub use date_picker::DatePicker;
#[doc(inline)]
pub use image::Image;
#[doc(inline)]
pub use overflow::Overflow;
#[doc(inline)]
pub use radio::Radio;
#[doc(inline)]
pub use select::Select;
#[doc(inline)]
pub use text_input::TextInput;

/// # Block Elements - interactive components
/// [slack api docs 🔗](https://api.slack.com/reference/block-kit/block-elements)
///
/// Block elements can be used inside of `section`, `context`, and `actions` [layout blocks 🔗].
/// Inputs can only be used inside of `input` blocks.
///
/// Our overview of [app surfaces that support Block Kit 🔗] shows you where those blocks might be relevant.
///
/// Finally, our [handling user interactivity guide 🔗] will help you prepare your app to allow
/// for the use of the interactive components listed below.
///
/// [app surfaces that support Block Kit 🔗]: https://api.slack.com/messaging/composing/layouts
/// [handling user interactivity guide 🔗]: https://api.slack.com/interactivity/handling
/// [layout blocks 🔗]: https://api.slack.com/reference/block-kit/blocks
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BlockElement<'a> {
  Button(Button),
  Checkboxes(Checkboxes<'a>),
  Image(Image<'a>),

  #[serde(rename = "datepicker")]
  DatePicker(DatePicker<'a>),

  #[serde(rename = "overflow_menu")]
  Overflow(Overflow<'a>),

  RadioButtons(Radio<'a>),

  #[serde(rename = "plain_text_input")]
  TextInput(TextInput<'a>),

  #[serde(rename = "channels_select")]
  SelectPublicChannel(select::PublicChannel<'a>),

  #[serde(rename = "conversations_select")]
  SelectConversation(select::Conversation<'a>),

  #[serde(rename = "users_select")]
  SelectUser(select::User<'a>),

  #[serde(rename = "external_select")]
  SelectExternal(select::External<'a>),

  #[serde(rename = "static_select")]
  SelectStatic(select::Static<'a>),

  #[serde(rename = "multi_static_select")]
  MultiSelectStatic(select::multi::Static<'a>),

  #[serde(rename = "multi_static_user")]
  MultiSelectUser(select::multi::User<'a>),

  #[serde(rename = "multi_static_external")]
  MultiSelectExternal(select::multi::External<'a>),

  #[serde(rename = "multi_static_conversation")]
  MultiSelectConversation(select::multi::Conversation<'a>),
}

impl<'a> BlockElement<'a> {
  pub fn validate(&self) -> ValidationResult {
    match self {
      | Self::Button(cts) => cts.validate(),
      | Self::SelectPublicChannel(cts) => cts.validate(),
      | Self::SelectConversation(cts) => cts.validate(),
      | Self::SelectUser(cts) => cts.validate(),
      | Self::SelectExternal(cts) => cts.validate(),
      | Self::SelectStatic(cts) => cts.validate(),
      | Self::RadioButtons(cts) => cts.validate(),
      | Self::Overflow(cts) => cts.validate(),
      | Self::Checkboxes(cts) => cts.validate(),
      | rest => todo!("validation not implemented for {:?}", rest),
    }
  }
}

convert!(impl From<Button> for BlockElement<'static> => |b| BlockElement::Button(b));
convert!(impl<'a> From<Radio<'a>> for BlockElement<'a> => |b| BlockElement::RadioButtons(b));
convert!(impl<'a> From<TextInput<'a>> for BlockElement<'a> => |t| BlockElement::TextInput(t));
convert!(impl<'a> From<Overflow<'a>> for BlockElement<'a> => |t| BlockElement::Overflow(t));
convert!(impl<'a> From<DatePicker<'a>> for BlockElement<'a> => |t| BlockElement::DatePicker(t));
convert!(impl<'a> From<Checkboxes<'a>> for BlockElement<'a> => |t| BlockElement::Checkboxes(t));

convert!(impl<'a> From<Select<'a>> for BlockElement<'a>
    => |s| match s {
        Select::PublicChannel(s) => s.into(),
        Select::Conversation(s) => s.into(),
        Select::User(s) => s.into(),
        Select::External(s) => s.into(),
        Select::Static(s) => s.into(),
    }
);

convert!(impl<'a> From<select::Static<'a>> for BlockElement<'a> => |s| BlockElement::SelectStatic(s));
convert!(impl<'a> From<select::External<'a>> for BlockElement<'a> => |s| BlockElement::SelectExternal(s));
convert!(impl<'a> From<select::PublicChannel<'a>> for BlockElement<'a> => |s| BlockElement::SelectPublicChannel(s));
convert!(impl<'a> From<select::Conversation<'a>> for BlockElement<'a> => |s| BlockElement::SelectConversation(s));
convert!(impl<'a> From<select::User<'a>> for BlockElement<'a> => |s| BlockElement::SelectUser(s));

convert!(impl<'a> From<select::multi::Static<'a>> for BlockElement<'a> => |s| BlockElement::MultiSelectStatic(s));
convert!(impl<'a> From<select::multi::User<'a>> for BlockElement<'a> => |s| BlockElement::MultiSelectUser(s));
convert!(impl<'a> From<select::multi::Conversation<'a>> for BlockElement<'a> => |s| BlockElement::MultiSelectConversation(s));
convert!(impl<'a> From<select::multi::External<'a>> for BlockElement<'a> => |s| BlockElement::MultiSelectExternal(s));