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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::block::SECTION_TYPE;
use crate::block_element::BlockElement;
use crate::composition::text::Text;
use serde::Serialize;

/// A section is one of the most flexible blocks available.
/// - simple text
/// - multiple text fields
/// - `Element` accessory
#[derive(Debug, Serialize)]
pub struct SectionBlock {
    #[serde(rename = "type")]
    type_name: &'static str,
    text: Text,
    #[serde(skip_serializing_if = "Option::is_none")]
    block_id: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    fields: Vec<Text>,
    #[serde(skip_serializing_if = "Option::is_none")]
    accessory: Option<BlockElement>,
}

impl SectionBlock {
    pub fn new(text: Text) -> Self {
        SectionBlock {
            type_name: SECTION_TYPE,
            text,
            block_id: Option::default(),
            fields: Vec::default(),
            accessory: Option::default(),
        }
    }

    pub fn block_id(mut self, block_id: impl Into<String>) -> Self {
        self.block_id = Some(block_id.into());
        self
    }

    pub fn fields(mut self, fields: Vec<Text>) -> Self {
        self.fields = fields;
        self
    }

    pub fn accessory(mut self, accessory: BlockElement) -> Self {
        self.accessory = Some(accessory);
        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 section = SectionBlock::new(Text::default());
        let json = serde_json::to_string_pretty(&section).unwrap_or("".to_string());
        let expected = r#"{
  "type": "section",
  "text": {
    "type": "plain_text",
    "text": ""
  }
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_block_id() {
        let section = SectionBlock::new(Text::default()).block_id("block");
        let json = serde_json::to_string_pretty(&section).unwrap_or("".to_string());
        let expected = r#"{
  "type": "section",
  "text": {
    "type": "plain_text",
    "text": ""
  },
  "block_id": "block"
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_fields() {
        let section = SectionBlock::new(Text::default()).fields(vec![Text::default()]);
        let json = serde_json::to_string_pretty(&section).unwrap_or("".to_string());
        let expected = r#"{
  "type": "section",
  "text": {
    "type": "plain_text",
    "text": ""
  },
  "fields": [
    {
      "type": "plain_text",
      "text": ""
    }
  ]
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_accessory() {
        let button = ButtonElement::new(PlainText::new("text"), "action_id");
        let element = Button(button);
        let section = SectionBlock::new(Text::default()).accessory(element);
        let json = serde_json::to_string_pretty(&section).unwrap_or("".to_string());
        let expected = r#"{
  "type": "section",
  "text": {
    "type": "plain_text",
    "text": ""
  },
  "accessory": {
    "type": "button",
    "text": {
      "type": "plain_text",
      "text": "text"
    },
    "action_id": "action_id"
  }
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_all() {
        let button = ButtonElement::new(PlainText::new("text"), "action_id");
        let element = Button(button);
        let section = SectionBlock::new(Text::default())
            .block_id("block")
            .fields(vec![Text::default()])
            .accessory(element);

        let json = serde_json::to_string_pretty(&section).unwrap_or("".to_string());
        let expected = r#"{
  "type": "section",
  "text": {
    "type": "plain_text",
    "text": ""
  },
  "block_id": "block",
  "fields": [
    {
      "type": "plain_text",
      "text": ""
    }
  ],
  "accessory": {
    "type": "button",
    "text": {
      "type": "plain_text",
      "text": "text"
    },
    "action_id": "action_id"
  }
}"#;
        assert_eq!(json, expected);
    }
}