1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
use crate::block::ACTIONS_TYPE; use crate::block_element::BlockElement; use serde::Serialize; #[derive(Debug, Serialize)] pub struct ActionsBlock { #[serde(rename = "type")] type_name: &'static str, elements: Vec<BlockElement>, #[serde(skip_serializing_if = "Option::is_none")] block_id: Option<String>, } impl ActionsBlock { pub fn new(elements: Vec<BlockElement>) -> Self { ActionsBlock { type_name: ACTIONS_TYPE, elements, block_id: Option::default(), } } pub fn block_id(mut self, block_id: impl Into<String>) -> Self { self.block_id = Some(block_id.into()); self } } #[cfg(test)] mod test { use super::*; use crate::block_element::button::ButtonElement; use crate::block_element::BlockElement::Button; use crate::composition::text::PlainText; #[test] fn test_ser_new() { let actions = ActionsBlock::new(vec![Button(ButtonElement::new( PlainText::new("text"), "action_id", ))]); let json = serde_json::to_string_pretty(&actions).unwrap_or("".to_string()); let expected = r#"{ "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "text" }, "action_id": "action_id" } ] }"#; assert_eq!(json, expected); } #[test] fn test_ser_block_id() { let actions = ActionsBlock::new(vec![Button(ButtonElement::new( PlainText::new("text"), "action_id", ))]) .block_id("block"); let json = serde_json::to_string_pretty(&actions).unwrap_or("".to_string()); let expected = r#"{ "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "text" }, "action_id": "action_id" } ], "block_id": "block" }"#; assert_eq!(json, expected); } }