slack_blocks/elems/select/multi/
external.rs

1//! # Multi-Select menu with external data source
2//! [slack api docs 🔗]
3//!
4//! This select menu will load its options from an external data source,
5//! allowing for a dynamic list of options.
6//!
7//! ## Setup
8//! [Slack API doc guide for setting up an external data source 🔗](https://api.slack.com/reference/block-kit/block-elements#external_select__setup)
9//!
10//! [slack api docs 🔗]: https://api.slack.com/reference/block-kit/block-elements#external_select
11
12use std::borrow::Cow;
13
14use serde::{Deserialize, Serialize};
15#[cfg(feature = "validation")]
16use validator::Validate;
17
18#[cfg(feature = "validation")]
19use crate::val_helpr::ValidationResult;
20use crate::{compose,
21            compose::{opt::NoUrl, Confirm},
22            elems::select::external::build,
23            text};
24
25type OptOrOptGroup<'a> = compose::OptOrOptGroup<'a, text::Plain, NoUrl>;
26
27/// # Multi-Select menu with external data source
28/// [slack api docs 🔗]
29///
30/// This select menu will load its options from an external data source,
31/// allowing for a dynamic list of options.
32///
33/// ## Setup
34/// [Slack API doc guide for setting up an external data source 🔗](https://api.slack.com/reference/block-kit/block-elements#external_select__setup)
35///
36/// [slack api docs 🔗]: https://api.slack.com/reference/block-kit/block-elements#external_select
37#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
38#[cfg_attr(feature = "validation", derive(Validate))]
39pub struct External<'a> {
40  #[cfg_attr(feature = "validation",
41             validate(custom = "crate::elems::select::validate::placeholder"))]
42  pub(in crate::elems::select) placeholder: text::Text,
43
44  #[cfg_attr(feature = "validation", validate(length(max = 255)))]
45  pub(in crate::elems::select) action_id: Cow<'a, str>,
46
47  #[serde(skip_serializing_if = "Option::is_none")]
48  pub(in crate::elems::select) min_query_length: Option<u64>,
49
50  #[serde(skip_serializing_if = "Option::is_none")]
51  #[cfg_attr(feature = "validation", validate)]
52  pub(in crate::elems::select) confirm: Option<Confirm>,
53
54  #[serde(skip_serializing_if = "Option::is_none")]
55  pub(in crate::elems::select) initial_options:
56    Option<Cow<'a, [OptOrOptGroup<'a>]>>,
57
58  #[cfg_attr(feature = "validation", validate(range(min = 1)))]
59  #[serde(skip_serializing_if = "Option::is_none")]
60  pub(in crate::elems::select) max_selected_items: Option<u32>,
61}
62
63impl<'a> External<'a> {
64  /// Build a new external multi-select element
65  ///
66  /// # Examples
67  /// ```
68  /// // TODO(#130)
69  /// ```
70  pub fn builder() -> build::MultiExternalBuilderInit<'a> {
71    build::MultiExternalBuilderInit::new()
72  }
73
74  /// Validate that this user select agrees with Slack's model requirements
75  ///
76  /// # Errors
77  /// - If `placeholder` longer than 150 chars
78  /// - If `action_id` longer than 255 chars
79  ///
80  /// # Example
81  /// ```
82  /// use slack_blocks::elems::select;
83  ///
84  /// let placeholder = r#"Hey I really would appreciate it if you chose
85  ///         a channel relatively soon, so that we can figure out
86  ///         where we need to send this poll, ok? it's kind of
87  ///         important that you specify where this poll should be
88  ///         sent, in case we haven't made that super clear.
89  ///         If you understand, could you pick a channel, already??"#;
90  ///
91  /// let select = select::multi::External::builder().placeholder(placeholder)
92  ///                                                .action_id("ABC123")
93  ///                                                .build();
94  ///
95  /// assert!(matches!(select.validate(), Err(_)))
96  /// ```
97  #[cfg(feature = "validation")]
98  #[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
99  pub fn validate(&self) -> ValidationResult {
100    Validate::validate(self)
101  }
102}