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