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
use serde::Serialize;

/// Interaction type to set into [Dispatch action configuration](https://api.slack.com/reference/block-kit/composition-objects#dispatch_action_config)
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TriggerAction {
    /// Represents `on_enter_pressed`.
    OnEnterPressed,

    /// Represents `on_character_entered`.
    OnCharacterEntered,
}

/// [Dispatch action configuration](https://api.slack.com/reference/block-kit/composition-objects#dispatch_action_config)
/// representation.
///
/// # Example
///
/// ```
/// use slack_messaging::blocks::elements::{DispatchActionConfiguration, TriggerAction};
/// use serde_json::json;
///
/// let config = DispatchActionConfiguration::new()
///     .push_trigger_action(TriggerAction::OnEnterPressed)
///     .push_trigger_action(TriggerAction::OnCharacterEntered);
///
/// let expected = json!({
///     "trigger_actions_on": [
///         "on_enter_pressed",
///         "on_character_entered"
///     ]
/// });
///
/// let config_json = serde_json::to_value(config).unwrap();
///
/// assert_eq!(config_json, expected);
/// ```
#[derive(Debug, Default, Clone, Serialize)]
pub struct DispatchActionConfiguration {
    trigger_actions_on: Vec<TriggerAction>,
}

impl DispatchActionConfiguration {
    /// Constructs a Dispatch action configuration.
    ///
    /// ```
    /// use slack_messaging::blocks::elements::DispatchActionConfiguration;
    /// use serde_json::json;
    ///
    /// let config = DispatchActionConfiguration::new();
    ///
    /// let expected = json!({
    ///     "trigger_actions_on": []
    /// });
    ///
    /// let config_json = serde_json::to_value(config).unwrap();
    ///
    /// assert_eq!(config_json, expected);
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets trigger_actions_on field directly.
    ///
    /// ```
    /// use slack_messaging::blocks::elements::{DispatchActionConfiguration, TriggerAction};
    /// use serde_json::json;
    ///
    /// let config = DispatchActionConfiguration::new()
    ///     .set_trigger_actions(
    ///         vec![
    ///             TriggerAction::OnEnterPressed,
    ///             TriggerAction::OnCharacterEntered,
    ///         ]
    ///     );
    ///
    /// let expected = json!({
    ///     "trigger_actions_on": [
    ///         "on_enter_pressed",
    ///         "on_character_entered"
    ///     ]
    /// });
    ///
    /// let config_json = serde_json::to_value(config).unwrap();
    ///
    /// assert_eq!(config_json, expected);
    /// ```
    pub fn set_trigger_actions(self, actions: Vec<TriggerAction>) -> Self {
        Self {
            trigger_actions_on: actions,
        }
    }

    /// Adds trigger_action to trigger_actions_on field.
    ///
    /// ```
    /// use slack_messaging::blocks::elements::{DispatchActionConfiguration, TriggerAction};
    /// use serde_json::json;
    ///
    /// let config = DispatchActionConfiguration::new()
    ///     .push_trigger_action(TriggerAction::OnEnterPressed);
    ///
    /// let expected = json!({
    ///     "trigger_actions_on": [
    ///         "on_enter_pressed"
    ///     ]
    /// });
    ///
    /// let config_json = serde_json::to_value(config).unwrap();
    ///
    /// assert_eq!(config_json, expected);
    /// ```
    pub fn push_trigger_action(self, action: TriggerAction) -> Self {
        let Self {
            mut trigger_actions_on,
        } = self;
        trigger_actions_on.push(action);
        Self { trigger_actions_on }
    }
}