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
210
211
212
213
214
215
216
217
218
219
220
221
use serde::{Deserialize, Serialize};
use validator::{Validate};

use crate::compose;

type ValidationResult = Result<(), validator::ValidationErrors>;

/// ### Section
///
/// There is a validation requirement from
/// Slack that a Section block **either**:
///
/// - have the `text` field populated, in which case
///   the `fields` field is optional
/// OR
/// - have the `fields` field populated, in which
///   case the `text` field is optional
///
/// Since Section blocks are a very commonly used structure
/// in Block Kit, this "Contents" enum implementation
/// was chosen to enforce that requirement at compile-time.
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum Contents {
    Text(Text),
    Fields(Fields),
}

#[derive(Serialize, Deserialize, Debug, Validate)]
pub struct Fields {
    /// An array of [text objects 🔗][text_objects].
    /// Any text objects included with fields will be
    /// rendered in a compact format that allows for
    /// 2 columns of side-by-side text.
    ///
    /// Maximum number of items is 10.
    /// Maximum length for the text in each item is 2000 characters.
    ///
    /// [text_objects]: https://api.slack.com/reference/messaging/composition-objects#text
    #[validate(length(max = 10))]
    #[validate(custom = "validation::each_text_max_len_2k")]
    fields: Vec<compose::Text>,

    /// The text for the block,
    /// in the form of a [text object 🔗][text_objects].
    ///
    /// Maximum length for the text in this field is 3000 characters.
    ///
    /// [text_objects]: https://api.slack.com/reference/messaging/composition-objects#text
    #[validate(custom = "validation::text_max_len_3k")]
    text: Option<compose::Text>,

    /// A string acting as a unique identifier for a block.
    ///
    /// You can use this block_id when you receive an interaction payload
    /// to [identify the source of the action 🔗][handling_payloads].
    /// If not specified, one will be generated.
    ///
    /// Maximum length for this field is 255 characters.
    ///
    /// block_id should be unique for each message and each iteration of a message.
    /// If a message is updated, use a new block_id.
    ///
    /// [handling_payloads]: https://api.slack.com/interactivity/handling#payloads
    #[validate(length(max = 255))]
    block_id: Option<String>,

    /// One of the available [element objects 🔗][element_objects].
    ///
    /// [element_objects]: https://api.slack.com/reference/messaging/block-elements
    accessory: Option<()>,
}

#[derive(Serialize, Deserialize, Debug, Validate)]
pub struct Text {
    /// The text for the block,
    /// in the form of a [text object 🔗][text_objects].
    ///
    /// Maximum length for the text in this field is 3000 characters.
    ///
    /// [text_objects]: https://api.slack.com/reference/messaging/composition-objects#text
    #[validate(custom = "validation::text_max_len_3k")]
    text: compose::Text,

    /// An array of [text objects 🔗][text_objects].
    /// Any text objects included with fields will be
    /// rendered in a compact format that allows for
    /// 2 columns of side-by-side text.
    ///
    /// Maximum number of items is 10.
    /// Maximum length for the text in each item is 2000 characters.
    ///
    /// [text_objects]: https://api.slack.com/reference/messaging/composition-objects#text
    #[validate(length(max = 10))]
    #[validate(custom = "validation::each_text_max_len_2k")]
    fields: Option<Vec<compose::Text>>,

    /// A string acting as a unique identifier for a block.
    ///
    /// You can use this block_id when you receive an interaction payload
    /// to [identify the source of the action 🔗][handling_payloads].
    /// If not specified, one will be generated.
    ///
    /// Maximum length for this field is 255 characters.
    ///
    /// block_id should be unique for each message and each iteration of a message.
    /// If a message is updated, use a new block_id.
    ///
    /// [handling_payloads]: https://api.slack.com/interactivity/handling#payloads
    block_id: Option<String>,

    /// One of the available [element objects 🔗][element_objects].
    ///
    /// [element_objects]: https://api.slack.com/reference/messaging/block-elements
    accessory: Option<()>,
}

impl Contents {
    pub fn from_fields(fields: Vec<compose::Text>) -> Self {
        Contents::Fields(Fields {
            fields,
            text: None,
            block_id: None,
            accessory: None,
        })
    }

    pub fn from_text(text: compose::Text) -> Self {
        Contents::Text(Text {
            text,
            fields: None,
            block_id: None,
            accessory: None,
        })
    }

    pub fn validate(&self) -> ValidationResult {
        match self {
            Contents::Text(text) => text.validate(),
            Contents::Fields(fields) => fields.validate()
        }
    }
}

pub mod validation {
    use crate::compose;

    pub const FIELDS_MAX_CT: usize = 10;
    pub const FIELD_MAX_LEN: usize = 2000;
    pub const TEXT_MAX_LEN: usize = 3000;
    pub const BLOCK_ID_MAX_LEN: usize = 255;

    type ValidationResult = Result<(), validator::ValidationError>;

    pub fn text_max_len_3k(text: &compose::Text) -> ValidationResult {
        compose::validation::text_max_len(text, TEXT_MAX_LEN)
    }

    pub fn each_text_max_len_2k(texts: &Vec<compose::Text>) -> ValidationResult {
        texts
            .iter()
            .map(|text| compose::validation::text_max_len(text, FIELD_MAX_LEN))
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use std::iter::repeat;
    use test_case::test_case;

    use super::*;
    use crate::compose;

    fn string_of_len(len: usize) -> String {
        repeat(' ').take(len).collect::<String>()
    }

    fn vec_of_len<T: Clone>(item: T, len: usize) -> Vec<T> {
        repeat(item).take(len).collect::<Vec<T>>()
    }

    #[test_case(
        Contents::from_text(compose::Text::markdown(string_of_len(3001)))
        => matches Err(_);
        "fail_when_text_longer_than_3k_chars"
    )]
    #[test_case(
        Contents::from_fields(vec_of_len(compose::Text::plain("".to_string()), 11))
        => matches Err(_);
        "fail_when_more_than_10_fields"
    )]
    #[test_case(
        Contents::from_fields(vec![compose::Text::plain(string_of_len(2001))])
        => matches Err(_);
        "fail_when_field_longer_than_2k_chars"
    )]
    #[test_case(
        Contents::from_fields(vec_of_len(compose::Text::plain(string_of_len(2001)), 2))
        => matches Err(_);
        "fail_when_multiple_fields_longer_than_2k_chars"
    )]
    #[test_case(
        Contents::Fields(Fields {
            text: None,
            fields: vec![],
            block_id: Some(string_of_len(256)),
            accessory: None,
        })
        => matches Err(_);
        "fail_when_block_id_longer_than_255_chars"
    )]
    pub fn section_contents_validation_should(contents: Contents) -> ValidationResult {
        // arrange (test_case input)

        // act
        contents.validate()

        // assert (test_case output)
    }
}