slack_messaging/blocks/elements/
icon_button.rs

1use crate::blocks::elements::types::Icon;
2use crate::composition_objects::{ConfirmationDialog, Plain, Text};
3use crate::validators::*;
4
5use serde::Serialize;
6use slack_messaging_derive::Builder;
7
8/// [Icon button element](https://docs.slack.dev/reference/block-kit/block-elements/icon-button-element) representation.
9///
10/// # Fields and Validations
11///
12/// For more details, see the [official
13/// documentation](https://docs.slack.dev/reference/block-kit/block-elements/icon-button-element).
14///
15/// | Field | Type | Required | Validation |
16/// |-------|------|----------|------------|
17/// | icon | [Icon] | Yes | N/A |
18/// | text | [Text]<[Plain]> | Yes | N/A |
19/// | action_id | String | No | Max length 255 characters |
20/// | value | String | No | Max length 2000 characters |
21/// | confirm | [ConfirmationDialog] | No | N/A |
22/// | accessibility_label | String | No | Max length 75 characters |
23/// | visible_to_user_ids | `Vec<String>` | No | N/A |
24///
25/// # Example
26///
27/// ```
28/// use slack_messaging::plain_text;
29/// use slack_messaging::blocks::elements::{IconButton, types::Icon};
30/// # use std::error::Error;
31///
32/// # fn try_main() -> Result<(), Box<dyn Error>> {
33/// let button = IconButton::builder()
34///     .icon(Icon::Trash)
35///     .text(plain_text!("Delete")?)
36///     .action_id("delete_button")
37///     .value("delete_item")
38///     .build()?;
39///
40/// let expected = serde_json::json!({
41///     "type": "icon_button",
42///     "icon": "trash",
43///     "text": {
44///       "type": "plain_text",
45///       "text": "Delete"
46///     },
47///     "action_id": "delete_button",
48///     "value": "delete_item"
49/// });
50///
51/// let json = serde_json::to_value(button).unwrap();
52///
53/// assert_eq!(json, expected);
54///
55/// // If your object has any validation errors, the build method returns Result::Err
56/// let button = IconButton::builder()
57///     .icon(Icon::Trash)
58///     .build();
59///
60/// assert!(button.is_err());
61/// #     Ok(())
62/// # }
63/// # fn main() {
64/// #     try_main().unwrap()
65/// # }
66/// ```
67#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
68#[serde(tag = "type", rename = "icon_button")]
69pub struct IconButton {
70    #[builder(validate("required"))]
71    pub(crate) icon: Option<Icon>,
72
73    #[builder(validate("required"))]
74    pub(crate) text: Option<Text<Plain>>,
75
76    #[serde(skip_serializing_if = "Option::is_none")]
77    #[builder(validate("text::max_255"))]
78    pub(crate) action_id: Option<String>,
79
80    #[serde(skip_serializing_if = "Option::is_none")]
81    #[builder(validate("text::max_2000"))]
82    pub(crate) value: Option<String>,
83
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub(crate) confirm: Option<ConfirmationDialog>,
86
87    #[serde(skip_serializing_if = "Option::is_none")]
88    #[builder(validate("text::max_75"))]
89    pub(crate) accessibility_label: Option<String>,
90
91    #[serde(skip_serializing_if = "Option::is_none")]
92    #[builder(push_item = "visible_to_user_id")]
93    pub(crate) visible_to_user_ids: Option<Vec<String>>,
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use crate::composition_objects::test_helpers::*;
100    use crate::errors::*;
101
102    #[test]
103    fn it_implements_builder() {
104        let expected = IconButton {
105            icon: Some(Icon::Trash),
106            text: Some(plain_text("Delete")),
107            action_id: Some("icon_button_0".into()),
108            value: Some("delete_item".into()),
109            confirm: Some(confirm()),
110            accessibility_label: Some("Delete!".into()),
111            visible_to_user_ids: Some(vec!["USER0".into(), "USER1".into()]),
112        };
113
114        let val = IconButton::builder()
115            .set_icon(Some(Icon::Trash))
116            .set_text(Some(plain_text("Delete")))
117            .set_action_id(Some("icon_button_0"))
118            .set_value(Some("delete_item"))
119            .set_confirm(Some(confirm()))
120            .set_accessibility_label(Some("Delete!"))
121            .set_visible_to_user_ids(Some(vec!["USER0".into(), "USER1".into()]))
122            .build()
123            .unwrap();
124
125        assert_eq!(val, expected);
126
127        let val = IconButton::builder()
128            .icon(Icon::Trash)
129            .text(plain_text("Delete"))
130            .action_id("icon_button_0")
131            .value("delete_item")
132            .confirm(confirm())
133            .accessibility_label("Delete!")
134            .visible_to_user_ids(vec!["USER0".into(), "USER1".into()])
135            .build()
136            .unwrap();
137
138        assert_eq!(val, expected);
139    }
140
141    #[test]
142    fn it_implements_push_item_method() {
143        let expected = IconButton {
144            icon: Some(Icon::Trash),
145            text: Some(plain_text("Delete")),
146            action_id: None,
147            value: None,
148            confirm: None,
149            accessibility_label: None,
150            visible_to_user_ids: Some(vec!["USER0".into(), "USER1".into()]),
151        };
152
153        let val = IconButton::builder()
154            .icon(Icon::Trash)
155            .text(plain_text("Delete"))
156            .visible_to_user_id("USER0")
157            .visible_to_user_id("USER1")
158            .build()
159            .unwrap();
160
161        assert_eq!(val, expected);
162    }
163
164    #[test]
165    fn it_requires_icon_field() {
166        let err = IconButton::builder()
167            .text(plain_text("Delete"))
168            .build()
169            .unwrap_err();
170        assert_eq!(err.object(), "IconButton");
171
172        let errors = err.field("icon");
173        assert!(errors.includes(ValidationErrorKind::Required));
174    }
175
176    #[test]
177    fn it_requires_text_field() {
178        let err = IconButton::builder().icon(Icon::Trash).build().unwrap_err();
179        assert_eq!(err.object(), "IconButton");
180
181        let errors = err.field("text");
182        assert!(errors.includes(ValidationErrorKind::Required));
183    }
184
185    #[test]
186    fn it_requires_action_id_less_than_255_characters_long() {
187        let err = IconButton::builder()
188            .text(plain_text("Delete"))
189            .icon(Icon::Trash)
190            .action_id("a".repeat(256))
191            .build()
192            .unwrap_err();
193        assert_eq!(err.object(), "IconButton");
194
195        let errors = err.field("action_id");
196        assert!(errors.includes(ValidationErrorKind::MaxTextLength(255)));
197    }
198
199    #[test]
200    fn it_requires_value_less_than_2000_characters_long() {
201        let err = IconButton::builder()
202            .text(plain_text("Delete"))
203            .icon(Icon::Trash)
204            .value("a".repeat(2001))
205            .build()
206            .unwrap_err();
207        assert_eq!(err.object(), "IconButton");
208
209        let errors = err.field("value");
210        assert!(errors.includes(ValidationErrorKind::MaxTextLength(2000)));
211    }
212
213    #[test]
214    fn it_requires_accessibility_label_less_than_75_characters_long() {
215        let err = IconButton::builder()
216            .text(plain_text("Delete"))
217            .icon(Icon::Trash)
218            .accessibility_label("a".repeat(76))
219            .build()
220            .unwrap_err();
221        assert_eq!(err.object(), "IconButton");
222
223        let errors = err.field("accessibility_label");
224        assert!(errors.includes(ValidationErrorKind::MaxTextLength(75)));
225    }
226}