Skip to main content

slack_messaging/blocks/elements/
feedback_buttons.rs

1use crate::blocks::elements::types::FeedbackButton;
2use crate::validators::*;
3
4use serde::Serialize;
5use slack_messaging_derive::Builder;
6
7/// [Feedback buttons element](https://docs.slack.dev/reference/block-kit/block-elements/feedback-buttons-element) representation.
8///
9/// # Fields and Validations
10///
11/// For more details, see the [official
12/// documentation](https://docs.slack.dev/reference/block-kit/block-elements/feedback-buttons-element).
13///
14/// | Field | Type | Required | Validation |
15/// |-------|------|----------|------------|
16/// | action_id | String | No | Max length 255 characters |
17/// | positive_button | [FeedbackButton] | Yes | N/A |
18/// | negative_button | [FeedbackButton] | Yes | N/A |
19///
20/// # Example
21///
22/// ```
23/// use slack_messaging::plain_text;
24/// use slack_messaging::blocks::elements::{types::FeedbackButton, FeedbackButtons};
25/// # use std::error::Error;
26///
27/// # fn try_main() -> Result<(), Box<dyn Error>> {
28/// let buttons = FeedbackButtons::builder()
29///     .action_id("feedback_buttons_1")
30///     .positive_button(
31///         FeedbackButton::builder()
32///             .text(plain_text!("Good")?)
33///             .value("positive_feedback")
34///             .accessibility_label("Mark this response as good")
35///             .build()?
36///     )
37///     .negative_button(
38///         FeedbackButton::builder()
39///             .text(plain_text!("Bad")?)
40///             .value("negative_feedback")
41///             .accessibility_label("Mark this response as bad")
42///             .build()?
43///     )
44///     .build()?;
45///
46/// let expected = serde_json::json!({
47///     "type": "feedback_buttons",
48///     "action_id": "feedback_buttons_1",
49///     "positive_button": {
50///         "text": {
51///             "type": "plain_text",
52///             "text": "Good"
53///         },
54///         "value": "positive_feedback",
55///         "accessibility_label": "Mark this response as good"
56///     },
57///     "negative_button": {
58///         "text": {
59///             "type": "plain_text",
60///             "text": "Bad"
61///         },
62///         "value": "negative_feedback",
63///         "accessibility_label": "Mark this response as bad"
64///     }
65/// });
66///
67/// let json = serde_json::to_value(buttons).unwrap();
68///
69/// assert_eq!(json, expected);
70///
71/// // If your object has any validation errors, the build method returns Result::Err
72/// let buttons = FeedbackButtons::builder()
73///     .positive_button(
74///         FeedbackButton::builder()
75///             .text(plain_text!("Good")?)
76///             .value("positive_feedback")
77///             .accessibility_label("Mark this response as good")
78///             .build()?
79///     )
80///     .build();
81/// assert!(buttons.is_err());
82/// #     Ok(())
83/// # }
84/// # fn main() {
85/// #     try_main().unwrap()
86/// # }
87/// ```
88#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
89#[serde(tag = "type", rename = "feedback_buttons")]
90pub struct FeedbackButtons {
91    #[serde(skip_serializing_if = "Option::is_none")]
92    #[builder(validate("text::max_255"))]
93    pub(crate) action_id: Option<String>,
94
95    #[builder(validate("required"))]
96    pub(crate) positive_button: Option<FeedbackButton>,
97
98    #[builder(validate("required"))]
99    pub(crate) negative_button: Option<FeedbackButton>,
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105    use crate::blocks::elements::test_helpers::*;
106    use crate::errors::*;
107
108    #[test]
109    fn it_implements_builder() {
110        let expected = FeedbackButtons {
111            action_id: Some("feedback_buttons_0".into()),
112            positive_button: Some(fb_btn("Good", "positive")),
113            negative_button: Some(fb_btn("Bad", "negative")),
114        };
115
116        let val = FeedbackButtons::builder()
117            .set_action_id(Some("feedback_buttons_0"))
118            .set_positive_button(Some(fb_btn("Good", "positive")))
119            .set_negative_button(Some(fb_btn("Bad", "negative")))
120            .build()
121            .unwrap();
122
123        assert_eq!(val, expected);
124
125        let val = FeedbackButtons::builder()
126            .action_id("feedback_buttons_0")
127            .positive_button(fb_btn("Good", "positive"))
128            .negative_button(fb_btn("Bad", "negative"))
129            .build()
130            .unwrap();
131
132        assert_eq!(val, expected);
133    }
134
135    #[test]
136    fn it_requires_action_id_less_than_255_characters_long() {
137        let err = FeedbackButtons::builder()
138            .action_id("a".repeat(256))
139            .positive_button(fb_btn("Good", "positive"))
140            .negative_button(fb_btn("Bad", "negative"))
141            .build()
142            .unwrap_err();
143        assert_eq!(err.object(), "FeedbackButtons");
144
145        let errors = err.field("action_id");
146        assert!(errors.includes(ValidationErrorKind::MaxTextLength(255)));
147    }
148
149    #[test]
150    fn it_requires_positive_button_field() {
151        let err = FeedbackButtons::builder()
152            .negative_button(fb_btn("Bad", "negative"))
153            .build()
154            .unwrap_err();
155        assert_eq!(err.object(), "FeedbackButtons");
156
157        let errors = err.field("positive_button");
158        assert!(errors.includes(ValidationErrorKind::Required));
159    }
160
161    #[test]
162    fn it_requires_negative_button_field() {
163        let err = FeedbackButtons::builder()
164            .positive_button(fb_btn("Good", "positive"))
165            .build()
166            .unwrap_err();
167        assert_eq!(err.object(), "FeedbackButtons");
168
169        let errors = err.field("negative_button");
170        assert!(errors.includes(ValidationErrorKind::Required));
171    }
172}