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