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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::block_element::STATIC_SELECT_MENU_TYPE;
use crate::composition::confirmation_dialog::ConfirmationDialog;
use crate::composition::option::OptionObject;
use crate::composition::option_group::OptionGroup;
use crate::composition::text::Text::Plain;
use crate::composition::text::{PlainText, Text};
use serde::Serialize;

/// requires set options or option_groups to display.
#[derive(Debug, Serialize)]
pub struct StaticSelectMenuElement {
    #[serde(rename = "type")]
    type_name: &'static str,
    placeholder: Text,
    action_id: String,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    options: Vec<OptionObject>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    option_groups: Vec<OptionGroup>,
    #[serde(skip_serializing_if = "Option::is_none")]
    initial_option: Option<OptionObject>,
    #[serde(skip_serializing_if = "Option::is_none")]
    confirm: Option<ConfirmationDialog>,
}

impl StaticSelectMenuElement {
    pub fn new(placeholder: impl Into<PlainText>, action_id: impl Into<String>) -> Self {
        StaticSelectMenuElement {
            type_name: STATIC_SELECT_MENU_TYPE,
            placeholder: Plain(placeholder.into()),
            action_id: action_id.into(),
            options: Vec::default(),
            option_groups: Vec::default(),
            initial_option: Option::default(),
            confirm: Option::default(),
        }
    }

    /// option_groups should not be if options is specified.
    pub fn options(mut self, options: Vec<OptionObject>) -> Self {
        self.options = options;
        self.option_groups = vec![];
        self
    }

    /// options should not be if option_groups is specified.
    pub fn option_groups(mut self, option_groups: Vec<OptionGroup>) -> Self {
        self.option_groups = option_groups;
        self.options = vec![];
        self
    }

    pub fn initial_option(mut self, initial_option: OptionObject) -> Self {
        self.initial_option = Some(initial_option);
        self
    }

    pub fn confirm(mut self, confirm: ConfirmationDialog) -> Self {
        self.confirm = Some(confirm);
        self
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_ser_new() {
        let menu = StaticSelectMenuElement::new("placeholder", "action_id");
        let json = serde_json::to_string_pretty(&menu).unwrap();

        let expected = r#"{
  "type": "static_select",
  "placeholder": {
    "type": "plain_text",
    "text": "placeholder"
  },
  "action_id": "action_id"
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_option() {
        let option = OptionObject::new(PlainText::new("text"), "value");
        let menu = StaticSelectMenuElement::new("placeholder", "action_id").options(vec![option]);
        let json = serde_json::to_string_pretty(&menu).unwrap();

        let expected = r#"{
  "type": "static_select",
  "placeholder": {
    "type": "plain_text",
    "text": "placeholder"
  },
  "action_id": "action_id",
  "options": [
    {
      "text": {
        "type": "plain_text",
        "text": "text"
      },
      "value": "value"
    }
  ]
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_option_group() {
        let option = OptionObject::new(PlainText::new("text"), "value");
        let group = OptionGroup::new(PlainText::new("label"), vec![option]);
        let menu =
            StaticSelectMenuElement::new("placeholder", "action_id").option_groups(vec![group]);
        let json = serde_json::to_string_pretty(&menu).unwrap();

        let expected = r#"{
  "type": "static_select",
  "placeholder": {
    "type": "plain_text",
    "text": "placeholder"
  },
  "action_id": "action_id",
  "option_groups": [
    {
      "label": {
        "type": "plain_text",
        "text": "label"
      },
      "options": [
        {
          "text": {
            "type": "plain_text",
            "text": "text"
          },
          "value": "value"
        }
      ]
    }
  ]
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_initial_option() {
        let option = OptionObject::new(PlainText::new("text"), "value");
        let menu = StaticSelectMenuElement::new("placeholder", "action_id").initial_option(option);
        let json = serde_json::to_string_pretty(&menu).unwrap();

        let expected = r#"{
  "type": "static_select",
  "placeholder": {
    "type": "plain_text",
    "text": "placeholder"
  },
  "action_id": "action_id",
  "initial_option": {
    "text": {
      "type": "plain_text",
      "text": "text"
    },
    "value": "value"
  }
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_initial_confirm() {
        let confirm = ConfirmationDialog::new(
            PlainText::new("title"),
            Plain(PlainText::new("text")),
            PlainText::new("confirm"),
            PlainText::new("deny"),
        );
        let menu = StaticSelectMenuElement::new("placeholder", "action_id").confirm(confirm);
        let json = serde_json::to_string_pretty(&menu).unwrap();

        let expected = r#"{
  "type": "static_select",
  "placeholder": {
    "type": "plain_text",
    "text": "placeholder"
  },
  "action_id": "action_id",
  "confirm": {
    "title": {
      "type": "plain_text",
      "text": "title"
    },
    "text": {
      "type": "plain_text",
      "text": "text"
    },
    "confirm": {
      "type": "plain_text",
      "text": "confirm"
    },
    "deny": {
      "type": "plain_text",
      "text": "deny"
    }
  }
}"#;
        assert_eq!(json, expected);
    }
}