slack_messaging/blocks/
mod.rs

1use serde::Serialize;
2
3/// Builders for blocks.
4pub mod builders;
5/// Objects from which blocks are composed.
6pub mod elements;
7/// Module for building [RichText] block.
8pub mod rich_text;
9/// Module for building [Table] block.
10pub mod table;
11
12mod actions;
13mod context;
14mod context_actions;
15mod divider;
16mod file;
17mod header;
18mod image;
19mod input;
20mod markdown;
21mod section;
22mod video;
23
24pub use actions::{Actions, ActionsElement};
25pub use context::{Context, ContextElement};
26pub use context_actions::{ContextActions, ContextActionsElement};
27pub use divider::Divider;
28pub use file::{File, FileSource};
29pub use header::Header;
30pub use image::Image;
31pub use input::{Input, InputElement};
32pub use markdown::Markdown;
33pub use rich_text::RichText;
34pub use section::{Accessory, Section};
35pub use table::Table;
36pub use video::Video;
37
38/// Objects that can be set to blocks in [Message](crate::message::Message).
39#[derive(Debug, Clone, Serialize, PartialEq)]
40#[serde(untagged)]
41pub enum Block {
42    /// [Actions block](https://docs.slack.dev/reference/block-kit/blocks/actions-block) representation
43    Actions(Box<Actions>),
44
45    /// [Context block](https://docs.slack.dev/reference/block-kit/blocks/context-block) representation
46    Context(Box<Context>),
47
48    /// [Context actions block](https://docs.slack.dev/reference/block-kit/blocks/context-actions-block) representation
49    ContextActions(Box<ContextActions>),
50
51    /// [Divider block](https://docs.slack.dev/reference/block-kit/blocks/divider-block) representation
52    Divider(Box<Divider>),
53
54    /// [File block](https://docs.slack.dev/reference/block-kit/blocks/file-block) representation
55    File(Box<File>),
56
57    /// [Header block](https://docs.slack.dev/reference/block-kit/blocks/header-block) representation
58    Header(Box<Header>),
59
60    /// [Image block](https://docs.slack.dev/reference/block-kit/blocks/image-block) representation
61    Image(Box<Image>),
62
63    /// [Input block](https://docs.slack.dev/reference/block-kit/blocks/input-block) representation
64    Input(Box<Input>),
65
66    /// [Markdown block](https://docs.slack.dev/reference/block-kit/blocks/markdown-block) representation
67    Markdown(Box<Markdown>),
68
69    /// [Rich text block](https://docs.slack.dev/reference/block-kit/blocks/rich-text-block) representation
70    RichText(Box<RichText>),
71
72    /// [Section block](https://docs.slack.dev/reference/block-kit/blocks/section-block) representation
73    Section(Box<Section>),
74
75    /// [Table block](https://docs.slack.dev/reference/block-kit/blocks/table-block) representation
76    Table(Box<Table>),
77
78    /// [Video block](https://docs.slack.dev/reference/block-kit/blocks/video-block) representation
79    Video(Box<Video>),
80}
81
82macro_rules! block_from {
83    ($($ty:ident,)*) => {
84        $(
85            impl From<$ty> for Block {
86                fn from(value: $ty) -> Self {
87                    Self::$ty(Box::new(value))
88                }
89            }
90         )*
91    }
92}
93
94block_from! {
95    Actions,
96    Context,
97    ContextActions,
98    Divider,
99    File,
100    Header,
101    Image,
102    Input,
103    Markdown,
104    RichText,
105    Section,
106    Table,
107    Video,
108}
109
110#[cfg(test)]
111pub mod test_helpers {
112    use super::rich_text::test_helpers as rich_text_helper;
113    use super::rich_text::types::test_helpers::*;
114    use super::*;
115    use crate::composition_objects::test_helpers::*;
116
117    pub fn header(text: impl Into<String>) -> Header {
118        Header {
119            block_id: None,
120            text: Some(plain_text(text)),
121        }
122    }
123
124    pub fn section(text: impl Into<String>) -> Section {
125        Section {
126            block_id: None,
127            text: Some(mrkdwn_text(text).into()),
128            fields: None,
129            accessory: None,
130            expand: None,
131        }
132    }
133
134    pub fn rich_text() -> RichText {
135        RichText {
136            block_id: Some("rich_text_0".into()),
137            elements: Some(vec![rich_text_helper::section(vec![el_text("foo")]).into()]),
138        }
139    }
140}