Skip to main content

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 alert;
14mod card;
15mod carousel;
16mod context;
17mod context_actions;
18mod divider;
19mod file;
20mod header;
21mod image;
22mod input;
23mod markdown;
24mod plan;
25mod section;
26mod task_card;
27mod video;
28
29pub use actions::{Actions, ActionsElement};
30pub use alert::{Alert, AlertLevel};
31pub use card::Card;
32pub use carousel::Carousel;
33pub use context::{Context, ContextElement};
34pub use context_actions::{ContextActions, ContextActionsElement};
35pub use divider::Divider;
36pub use file::{File, FileSource};
37pub use header::Header;
38pub use image::Image;
39pub use input::{Input, InputElement};
40pub use markdown::Markdown;
41pub use plan::Plan;
42pub use rich_text::RichText;
43pub use section::{Accessory, Section};
44pub use table::Table;
45pub use task_card::{TaskCard, TaskStatus};
46pub use video::Video;
47
48/// Objects that can be set to blocks in [Message](crate::message::Message).
49#[derive(Debug, Clone, Serialize, PartialEq)]
50#[serde(untagged)]
51pub enum Block {
52    /// [Actions block](https://docs.slack.dev/reference/block-kit/blocks/actions-block) representation
53    Actions(Box<Actions>),
54
55    /// [Alert block](https://docs.slack.dev/reference/block-kit/blocks/alert-block) representation
56    Alert(Box<Alert>),
57
58    /// [Card block](https://docs.slack.dev/reference/block-kit/blocks/card-block)
59    /// representation
60    Card(Box<Card>),
61
62    /// [Carousel block](https://docs.slack.dev/reference/block-kit/blocks/carousel-block)
63    /// representation
64    Carousel(Box<Carousel>),
65
66    /// [Context block](https://docs.slack.dev/reference/block-kit/blocks/context-block) representation
67    Context(Box<Context>),
68
69    /// [Context actions block](https://docs.slack.dev/reference/block-kit/blocks/context-actions-block) representation
70    ContextActions(Box<ContextActions>),
71
72    /// [Divider block](https://docs.slack.dev/reference/block-kit/blocks/divider-block) representation
73    Divider(Box<Divider>),
74
75    /// [File block](https://docs.slack.dev/reference/block-kit/blocks/file-block) representation
76    File(Box<File>),
77
78    /// [Header block](https://docs.slack.dev/reference/block-kit/blocks/header-block) representation
79    Header(Box<Header>),
80
81    /// [Image block](https://docs.slack.dev/reference/block-kit/blocks/image-block) representation
82    Image(Box<Image>),
83
84    /// [Input block](https://docs.slack.dev/reference/block-kit/blocks/input-block) representation
85    Input(Box<Input>),
86
87    /// [Markdown block](https://docs.slack.dev/reference/block-kit/blocks/markdown-block) representation
88    Markdown(Box<Markdown>),
89
90    /// [Plan block](https://docs.slack.dev/reference/block-kit/blocks/plan-block) representation
91    Plan(Box<Plan>),
92
93    /// [Rich text block](https://docs.slack.dev/reference/block-kit/blocks/rich-text-block) representation
94    RichText(Box<RichText>),
95
96    /// [Section block](https://docs.slack.dev/reference/block-kit/blocks/section-block) representation
97    Section(Box<Section>),
98
99    /// [Table block](https://docs.slack.dev/reference/block-kit/blocks/table-block) representation
100    Table(Box<Table>),
101
102    /// [Video block](https://docs.slack.dev/reference/block-kit/blocks/video-block) representation
103    Video(Box<Video>),
104}
105
106macro_rules! block_from {
107    ($($ty:ident,)*) => {
108        $(
109            impl From<$ty> for Block {
110                fn from(value: $ty) -> Self {
111                    Self::$ty(Box::new(value))
112                }
113            }
114         )*
115    }
116}
117
118block_from! {
119    Actions,
120    Alert,
121    Card,
122    Carousel,
123    Context,
124    ContextActions,
125    Divider,
126    File,
127    Header,
128    Image,
129    Input,
130    Markdown,
131    Plan,
132    RichText,
133    Section,
134    Table,
135    Video,
136}
137
138#[cfg(test)]
139pub mod test_helpers {
140    use super::rich_text::test_helpers as rich_text_helper;
141    use super::rich_text::types::test_helpers::*;
142    use super::*;
143    use crate::composition_objects::test_helpers::*;
144
145    pub fn header(text: impl Into<String>) -> Header {
146        Header {
147            block_id: None,
148            text: Some(plain_text(text)),
149        }
150    }
151
152    pub fn section(text: impl Into<String>) -> Section {
153        Section {
154            block_id: None,
155            text: Some(mrkdwn_text(text).into()),
156            fields: None,
157            accessory: None,
158            expand: None,
159        }
160    }
161
162    pub fn rich_text() -> RichText {
163        RichText {
164            block_id: Some("rich_text_0".into()),
165            elements: Some(vec![rich_text_helper::section(vec![el_text("foo")]).into()]),
166        }
167    }
168
169    pub fn task_card() -> TaskCard {
170        TaskCard {
171            task_id: Some("task_0".into()),
172            title: Some("Fetching weather data".into()),
173            status: Some(TaskStatus::Pending),
174            output: None,
175            details: None,
176            sources: None,
177            block_id: None,
178        }
179    }
180}