Skip to main content

slack_messaging/blocks/
markdown.rs

1use crate::validators::*;
2
3use serde::Serialize;
4use slack_messaging_derive::Builder;
5
6/// [Markdown block](https://docs.slack.dev/reference/block-kit/blocks/markdown-block)
7/// representation.
8///
9/// # Fields and Validations
10///
11/// For more details, see the [official
12/// documentation](https://docs.slack.dev/reference/block-kit/blocks/markdown-block).
13///
14/// | Field | Type | Required | Validation |
15/// |-------|------|----------|------------|
16/// | text | String | Yes | Maximum 12000 characters |
17/// | block_id | String | No | Maximum 255 characters |
18///
19/// # Example
20///
21/// ```
22/// use slack_messaging::blocks::Markdown;
23/// # use std::error::Error;
24///
25/// # fn try_main() -> Result<(), Box<dyn Error>> {
26/// let markdown = Markdown::builder()
27///     .block_id("markdown-0")
28///     .text("**Lots of information here!!**")
29///     .build()?;
30///
31/// let expected = serde_json::json!({
32///     "type": "markdown",
33///     "block_id": "markdown-0",
34///     "text": "**Lots of information here!!**"
35/// });
36///
37/// let json = serde_json::to_value(markdown).unwrap();
38///
39/// assert_eq!(json, expected);
40/// #     Ok(())
41/// # }
42/// # fn main() {
43/// #     try_main().unwrap()
44/// # }
45/// ```
46#[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}