api_checkpoint/
api_checkpoint.rs1use serde_json::{Value, json};
3use slackblocks::{
4 Accordion, AccordionSection, Block, ButtonElement, DataTableBlock, ErrorCategory, JsonNumber,
5 MarkdownText, MessagePayload, PlainText, PlanBlock, RawNumber, RawText, RichTextBlock,
6 RichTextList, RichTextListStyle, RichTextSection, RichTextStyle, RichTextText, SectionBlock,
7 SelectOption, TableBlock, TableCell, TaskCardBlock, TaskStatus,
8};
9
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11 let accordion = Accordion::new([AccordionSection::builder("Details")
12 .block(
13 SectionBlock::builder()
14 .text("Iterator composition")
15 .build()?,
16 )
17 .build()?])?;
18 let expanded = MessagePayload::builder("C0123456")
19 .blocks(accordion)
20 .build()?;
21 assert_eq!(expanded.blocks().map(<[Block]>::len), Some(1));
22 let option = SelectOption::builder()
23 .text("First option")
24 .value("first")
25 .build()?;
26 assert_eq!(option.value(), "first");
27 let section = SectionBlock::builder()
28 .text("Hello, Rust!")
29 .accessory(
30 ButtonElement::builder()
31 .text("Read more")
32 .action_id("read")
33 .build()?,
34 )
35 .block_id("hello")
36 .build()?;
37 let edited = section
38 .clone()
39 .into_builder()
40 .text(PlainText::new("Updated")?)
41 .build()?;
42 assert_ne!(edited, section);
43 let message = MessagePayload::builder("C0123456").block(section).build()?;
44 assert_eq!(message.blocks().map(<[Block]>::len), Some(1));
45 let encoded = serde_json::to_string(&message)?;
46 assert_eq!(serde_json::from_str::<MessagePayload>(&encoded)?, message);
47 let omitted = MessagePayload::try_from(json!({"channel":"C0123456"}))?;
48 assert_eq!(
49 serde_json::to_value(omitted.into_builder().build()?)?,
50 json!({"channel":"C0123456"})
51 );
52
53 let rich = RichTextBlock::builder()
54 .element(
55 RichTextList::builder()
56 .style(RichTextListStyle::Bullet)
57 .element(
58 RichTextSection::builder()
59 .element(
60 RichTextText::new("Native ownership")?
61 .into_builder()
62 .style(RichTextStyle::new().bold(true))
63 .build()?,
64 )
65 .build()?,
66 )
67 .build()?,
68 )
69 .build()?;
70 let table = TableBlock::builder()
71 .row([
72 TableCell::from(RawText::new("Heading")?),
73 rich.clone().into(),
74 ])
75 .row([TableCell::from(RawText::new("Ragged rows are valid")?)])
76 .build()?;
77 assert_eq!(table.rows()[1].len(), 1);
78 let pending = TaskCardBlock::builder()
79 .task_id("review")
80 .title("Review")
81 .status(TaskStatus::Pending)
82 .details(rich)
83 .build()?;
84 let error = MessagePayload::builder("C0123456")
85 .block(pending.clone())
86 .build()
87 .unwrap_err();
88 assert_eq!(error.category(), ErrorCategory::TypeMismatch);
89 assert_eq!(error.path(), "MessagePayload.blocks[0].status");
90 let plan = PlanBlock::builder()
91 .title("Release")
92 .task(pending)
93 .build()?;
94 let wire = serde_json::to_value(&plan)?;
95 assert!(wire["tasks"][0].get("type").is_none());
96 assert_eq!(PlanBlock::try_from(wire)?, plan);
97 let count = JsonNumber::from(9_007_199_254_740_993_u64);
98 assert_eq!(serde_json::to_string(&count)?, "9007199254740993");
99 let data = DataTableBlock::builder()
100 .caption("Exact counts")
101 .row([RawNumber::builder().value(0_i64).text("Count").build()?])
102 .row([RawNumber::builder()
103 .value(count)
104 .text("9007199254740993")
105 .build()?])
106 .build()?;
107 assert_eq!(data.rows().len(), 2);
108 assert!(PlainText::new("").is_err());
109 assert_eq!(
110 serde_json::to_value(MarkdownText::new("*native*")?)?["text"],
111 Value::String("*native*".into())
112 );
113 assert!(std::mem::size_of::<Block>() <= 32);
114 fn send_sync<T: Send + Sync>() {}
115 send_sync::<MessagePayload>();
116 println!("{encoded}");
117 Ok(())
118}