slack_messaging/composition_objects/
types.rs

1use crate::validators::required;
2
3use serde::Serialize;
4use serde_json::Value;
5use slack_messaging_derive::Builder;
6
7/// Type of conversation to set into [Conversation filter object](https://docs.slack.dev/reference/block-kit/composition-objects/conversation-filter-object)
8#[derive(Debug, Clone, Serialize, PartialEq)]
9#[serde(rename_all = "snake_case")]
10pub enum Conversation {
11    Im,
12    Mpim,
13    Private,
14    Public,
15}
16
17/// Interaction type to set into [Dispatch action configuration](https://docs.slack.dev/reference/block-kit/composition-objects/dispatch-action-configuration-object)
18#[derive(Debug, Clone, Serialize, PartialEq)]
19#[serde(rename_all = "snake_case")]
20pub enum TriggerAction {
21    /// Represents `on_enter_pressed`.
22    OnEnterPressed,
23
24    /// Represents `on_character_entered`.
25    OnCharacterEntered,
26}
27
28/// Phantom type to control url field of [`Opt`](crate::composition_objects::Opt). By default, this type is used,
29/// and the url field is unavailable.
30#[derive(Debug, Clone, PartialEq)]
31pub struct UrlUnavailable;
32
33/// Phantom type to control url field of [`Opt`](crate::composition_objects::Opt). Using this type, the url field
34/// is available.
35#[derive(Debug, Clone, PartialEq)]
36pub struct UrlAvailable;
37
38/// Input parameter for [Trigger object](https://docs.slack.dev/reference/block-kit/composition-objects/trigger-object).
39///
40/// # Fields and Validations
41///
42/// For more details, see the [official
43/// documentation](https://docs.slack.dev/reference/block-kit/composition-objects/trigger-object).
44///
45/// | Field | Type | Required | Validation |
46/// |-------|------|----------|------------|
47/// | name | String | Yes | N/A |
48/// | value | [Value] | Yes | N/A |
49///
50/// # Example
51///
52/// ```
53/// use slack_messaging::composition_objects::types::InputParameter;
54/// # use std::error::Error;
55///
56/// # fn try_main() -> Result<(), Box<dyn Error>> {
57/// let param = InputParameter::builder()
58///     .name("input_parameter_a")
59///     .value("Value for input param A")
60///     .build()?;
61///
62/// let expected = serde_json::json!({
63///     "name": "input_parameter_a",
64///     "value": "Value for input param A"
65/// });
66///
67/// let json = serde_json::to_value(param).unwrap();
68///
69/// assert_eq!(json, expected);
70///
71/// // If your object has any validation errors, the build method returns Result::Err
72/// let param = InputParameter::builder()
73///     .name("input_parameter_a")
74///     .build();
75///
76/// assert!(param.is_err());
77/// #     Ok(())
78/// # }
79/// # fn main() {
80/// #     try_main().unwrap()
81/// # }
82/// ```
83#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
84pub struct InputParameter {
85    #[builder(validate("required"))]
86    pub(crate) name: Option<String>,
87
88    #[builder(validate("required"))]
89    pub(crate) value: Option<Value>,
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use crate::errors::*;
96
97    #[test]
98    fn it_implements_builder() {
99        let expected = InputParameter {
100            name: Some("input_parameter_a".into()),
101            value: Some(Value::String("Value for input param A".into())),
102        };
103
104        let val = InputParameter::builder()
105            .set_name(Some("input_parameter_a"))
106            .set_value(Some(Value::String("Value for input param A".into())))
107            .build()
108            .unwrap();
109
110        assert_eq!(val, expected);
111
112        let val = InputParameter::builder()
113            .name("input_parameter_a")
114            .value("Value for input param A")
115            .build()
116            .unwrap();
117
118        assert_eq!(val, expected);
119    }
120
121    #[test]
122    fn it_requires_name_field() {
123        let err = InputParameter::builder()
124            .value("Value for input param A")
125            .build()
126            .unwrap_err();
127        assert_eq!(err.object(), "InputParameter");
128
129        let errors = err.field("name");
130        assert!(errors.includes(ValidationErrorKind::Required));
131    }
132
133    #[test]
134    fn it_requires_value_field() {
135        let err = InputParameter::builder()
136            .name("input_parameter_a")
137            .build()
138            .unwrap_err();
139        assert_eq!(err.object(), "InputParameter");
140
141        let errors = err.field("value");
142        assert!(errors.includes(ValidationErrorKind::Required));
143    }
144}