slack_messaging/blocks/rich_text/section.rs
1use crate::blocks::rich_text::types::RichTextElementType;
2use crate::validators::*;
3
4use serde::Serialize;
5use slack_messaging_derive::Builder;
6
7/// [Rich text section element](https://docs.slack.dev/reference/block-kit/blocks/rich-text-block#rich_text_section)
8/// representation.
9///
10/// # Fields and Validations
11///
12/// For more details, see the [official
13/// documentation](https://docs.slack.dev/reference/block-kit/blocks/rich-text-block#rich_text_section).
14///
15/// | Field | Type | Required | Validation |
16/// |-------|------|----------|------------|
17/// | elements | Vec<[RichTextElementType]> | Yes | N/A |
18///
19/// # Example
20///
21/// ```
22/// use slack_messaging::blocks::rich_text::prelude::*;
23/// # use std::error::Error;
24///
25/// # fn try_main() -> Result<(), Box<dyn Error>> {
26/// let section = RichTextSection::builder()
27/// .element(
28/// RichTextElementText::builder()
29/// .text("Hello there, ")
30/// .build()?
31/// )
32/// .element(
33/// RichTextElementText::builder()
34/// .text("I am a bold rich text block!")
35/// .style(
36/// RichTextStyle::builder()
37/// .bold(true)
38/// .build()?
39/// )
40/// .build()?
41/// )
42/// .build()?;
43///
44/// let expected = serde_json::json!({
45/// "type": "rich_text_section",
46/// "elements": [
47/// {
48/// "type": "text",
49/// "text": "Hello there, "
50/// },
51/// {
52/// "type": "text",
53/// "text": "I am a bold rich text block!",
54/// "style": {
55/// "bold": true
56/// }
57/// }
58/// ]
59/// });
60///
61/// let json = serde_json::to_value(section).unwrap();
62///
63/// assert_eq!(json, expected);
64///
65/// // If your object has any validation errors, the build method returns Result::Err
66/// let element = RichTextSection::builder().build();
67/// assert!(element.is_err());
68/// # Ok(())
69/// # }
70/// # fn main() {
71/// # try_main().unwrap()
72/// # }
73/// ```
74#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
75#[serde(tag = "type", rename = "rich_text_section")]
76pub struct RichTextSection {
77 #[builder(push_item = "element", validate("required"))]
78 pub(crate) elements: Option<Vec<RichTextElementType>>,
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use crate::blocks::rich_text::types::test_helpers::*;
85 use crate::errors::*;
86
87 #[test]
88 fn it_implements_builder() {
89 let expected = RichTextSection {
90 elements: Some(vec![el_text("foo"), el_emoji("bar")]),
91 };
92
93 let val = RichTextSection::builder()
94 .set_elements(Some(vec![el_text("foo"), el_emoji("bar")]))
95 .build()
96 .unwrap();
97
98 assert_eq!(val, expected);
99
100 let val = RichTextSection::builder()
101 .elements(vec![el_text("foo"), el_emoji("bar")])
102 .build()
103 .unwrap();
104
105 assert_eq!(val, expected);
106 }
107
108 #[test]
109 fn it_implements_push_item_method() {
110 let expected = RichTextSection {
111 elements: Some(vec![el_text("foo"), el_emoji("bar")]),
112 };
113
114 let val = RichTextSection::builder()
115 .element(text("foo"))
116 .element(emoji("bar"))
117 .build()
118 .unwrap();
119
120 assert_eq!(val, expected);
121 }
122
123 #[test]
124 fn it_requres_elements_field() {
125 let err = RichTextSection::builder().build().unwrap_err();
126 assert_eq!(err.object(), "RichTextSection");
127
128 let errors = err.field("elements");
129 assert!(errors.includes(ValidationErrorKind::Required));
130 }
131}