slack_messaging/blocks/
alert.rs1use crate::composition_objects::TextContent;
2use crate::validators::*;
3
4use serde::Serialize;
5use slack_messaging_derive::Builder;
6
7#[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#[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}