slack_messaging/blocks/
markdown.rs1use crate::validators::*;
2
3use serde::Serialize;
4use slack_messaging_derive::Builder;
5
6#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
47#[serde(tag = "type", rename = "markdown")]
48pub struct Markdown {
49 #[builder(validate("required", "text::max_12000"))]
50 pub(crate) text: Option<String>,
51
52 #[serde(skip_serializing_if = "Option::is_none")]
53 #[builder(validate("text::max_255"))]
54 pub(crate) block_id: Option<String>,
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use crate::errors::*;
61
62 #[test]
63 fn it_implements_builder() {
64 let expected = Markdown {
65 text: Some("**Lots of information here!!**".into()),
66 block_id: Some("markdown_0".into()),
67 };
68
69 let val = Markdown::builder()
70 .set_text(Some("**Lots of information here!!**"))
71 .set_block_id(Some("markdown_0"))
72 .build()
73 .unwrap();
74
75 assert_eq!(val, expected);
76
77 let val = Markdown::builder()
78 .text("**Lots of information here!!**")
79 .block_id("markdown_0")
80 .build()
81 .unwrap();
82
83 assert_eq!(val, expected);
84 }
85
86 #[test]
87 fn it_requires_text_field() {
88 let err = Markdown::builder().build().unwrap_err();
89 assert_eq!(err.object(), "Markdown");
90
91 let errors = err.field("text");
92 assert!(errors.includes(ValidationErrorKind::Required));
93 }
94
95 #[test]
96 fn it_requires_text_less_than_12000_characters_long() {
97 let err = Markdown::builder()
98 .text("a".repeat(12001))
99 .build()
100 .unwrap_err();
101 assert_eq!(err.object(), "Markdown");
102
103 let errors = err.field("text");
104 assert!(errors.includes(ValidationErrorKind::MaxTextLength(12000)));
105 }
106
107 #[test]
108 fn it_requires_block_id_less_than_255_characters_long() {
109 let err = Markdown::builder()
110 .text("foo")
111 .block_id("a".repeat(256))
112 .build()
113 .unwrap_err();
114 assert_eq!(err.object(), "Markdown");
115
116 let errors = err.field("block_id");
117 assert!(errors.includes(ValidationErrorKind::MaxTextLength(255)));
118 }
119}