slack_messaging/blocks/table/
cell.rs1use crate::blocks::RichText;
2use serde::Serialize;
3
4#[derive(Debug, Clone, Serialize, PartialEq)]
6#[serde(rename_all = "snake_case", tag = "type", content = "text")]
7pub enum TableCell {
8 RawText(String),
10
11 #[serde(untagged)]
13 RichText(RichText),
14}
15
16impl<T: Into<String>> From<T> for TableCell {
17 fn from(value: T) -> Self {
18 Self::RawText(value.into())
19 }
20}
21
22impl From<RichText> for TableCell {
23 fn from(value: RichText) -> Self {
24 Self::RichText(value)
25 }
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31 use crate::blocks::rich_text::{RichTextSection, types::RichTextElementLink};
32
33 #[test]
34 fn it_serializes_into_raw_text_cell() {
35 let cell = TableCell::RawText("Data 1A".into());
36
37 let expected = serde_json::json!({
38 "type": "raw_text",
39 "text": "Data 1A",
40 });
41
42 let json = serde_json::to_value(cell).unwrap();
43 assert_eq!(json, expected);
44 }
45
46 #[test]
47 fn it_serializes_into_rich_text_cell() {
48 let cell = TableCell::RichText(rich_text());
49
50 let expected = serde_json::json!({
51 "type": "rich_text",
52 "elements": [
53 {
54 "type": "rich_text_section",
55 "elements": [
56 {
57 "text": "Data 1B",
58 "type": "link",
59 "url": "https://slack.com"
60 }
61 ]
62 }
63 ]
64 });
65
66 let json = serde_json::to_value(cell).unwrap();
67 assert_eq!(json, expected);
68 }
69
70 fn rich_text() -> RichText {
71 RichText::builder()
72 .element(
73 RichTextSection::builder()
74 .element(
75 RichTextElementLink::builder()
76 .text("Data 1B")
77 .url("https://slack.com")
78 .build()
79 .unwrap(),
80 )
81 .build()
82 .unwrap(),
83 )
84 .build()
85 .unwrap()
86 }
87}