Skip to main content

slack_messaging/blocks/
alert.rs

1use crate::composition_objects::TextContent;
2use crate::validators::*;
3
4use serde::Serialize;
5use slack_messaging_derive::Builder;
6
7/// [Alert block](https://docs.slack.dev/reference/block-kit/blocks/alert-block) representation.
8///
9/// # Fields and Validations
10///
11/// For more details, see the [official
12/// documentation](https://docs.slack.dev/reference/block-kit/blocks/alert-block).
13///
14/// | Field | Type | Required | Validation |
15/// |-------|------|----------|------------|
16/// | text | [TextContent] | Yes | N/A |
17/// | level | [AlertLevel] | No | N/A |
18/// | block_id | String | No | Maximum 255 characters |
19///
20/// # Example
21///
22/// ```
23/// use slack_messaging::blocks::{Alert, AlertLevel};
24/// use slack_messaging::mrkdwn;
25/// # use std::error::Error;
26///
27/// # fn try_main() -> Result<(), Box<dyn Error>> {
28/// let alert = Alert::builder()
29///     .text(mrkdwn!("The work is mysterious and important.")?)
30///     .level(AlertLevel::Info)
31///     .build()?;
32///
33/// let expected = serde_json::json!({
34///     "type": "alert",
35///     "text": {
36///         "type": "mrkdwn",
37///         "text": "The work is mysterious and important."
38///     },
39///     "level": "info"
40/// });
41///
42/// let json = serde_json::to_value(alert).unwrap();
43///
44/// assert_eq!(json, expected);
45/// #     Ok(())
46/// # }
47/// # fn main() {
48/// #     try_main().unwrap()
49/// # }
50/// ```
51#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
52#[serde(tag = "type", rename = "alert")]
53pub struct Alert {
54    #[builder(validate("required"))]
55    pub(crate) text: Option<TextContent>,
56
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub(crate) level: Option<AlertLevel>,
59
60    #[serde(skip_serializing_if = "Option::is_none")]
61    #[builder(validate("text::max_255"))]
62    pub(crate) block_id: Option<String>,
63}
64
65/// Values that can be set to the `level` field of [Alert].
66#[derive(Debug, Copy, Clone, Serialize, PartialEq)]
67#[serde(rename_all = "snake_case")]
68pub enum AlertLevel {
69    Default,
70    Info,
71    Warning,
72    Error,
73    Success,
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79    use crate::composition_objects::test_helpers::*;
80    use crate::errors::*;
81
82    #[test]
83    fn it_implements_builder() {
84        let expected = Alert {
85            text: Some(plain_text("The work is mysterious and important.").into()),
86            level: Some(AlertLevel::Info),
87            block_id: Some("alert_block_0".into()),
88        };
89
90        let val = Alert::builder()
91            .set_text(Some(plain_text("The work is mysterious and important.")))
92            .set_level(Some(AlertLevel::Info))
93            .set_block_id(Some("alert_block_0"))
94            .build()
95            .unwrap();
96
97        assert_eq!(val, expected);
98
99        let val = Alert::builder()
100            .text(plain_text("The work is mysterious and important."))
101            .level(AlertLevel::Info)
102            .block_id("alert_block_0")
103            .build()
104            .unwrap();
105
106        assert_eq!(val, expected);
107    }
108
109    #[test]
110    fn it_requires_text_field() {
111        let err = Alert::builder().build().unwrap_err();
112        assert_eq!(err.object(), "Alert");
113
114        let errors = err.field("text");
115        assert!(errors.includes(ValidationErrorKind::Required));
116    }
117
118    #[test]
119    fn it_requires_block_id_less_than_255_characters_long() {
120        let err = Alert::builder()
121            .text(plain_text("The work is mysterious and important."))
122            .block_id("a".repeat(256))
123            .build()
124            .unwrap_err();
125        assert_eq!(err.object(), "Alert");
126
127        let errors = err.field("block_id");
128        assert!(errors.includes(ValidationErrorKind::MaxTextLength(255)));
129    }
130}