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
use serde::{Deserialize, Serialize};

use crate::impl_from_contents;

pub mod actions;
pub mod context;
pub mod file;
pub mod image;
pub mod input;
pub mod section;

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

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Block {
    #[serde(rename = "section")]
    Section(section::Contents),

    /// # Divider Block
    ///
    /// _[slack api docs 🔗][divider_docs]_
    ///
    /// A content divider, like an `<hr>`,
    /// to split up different blocks inside of a message.
    ///
    /// The divider block is nice and neat, requiring no fields.
    ///
    /// [divider_docs]: https://api.slack.com/reference/block-kit/blocks#divider
    #[serde(rename = "divider")]
    Divider,

    #[serde(rename = "image")]
    Image(image::Contents),

    #[serde(rename = "actions")]
    Actions(actions::Contents),

    #[serde(rename = "context")]
    Context(context::Contents),

    #[serde(rename = "input")]
    Input(input::Contents),

    #[serde(rename = "file")]
    File(file::Contents),
}

use std::fmt;

impl fmt::Display for Block {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let kind = match self {
            Block::Section { .. } => "Section",
            Block::Divider => "Divider",
            Block::Image { .. } => "Image",
            Block::Actions { .. } => "Actions",
            Block::Context { .. } => "Context",
            Block::Input { .. } => "Input",
            Block::File { .. } => "File",
        };

        write!(f, "{}", kind)
    }
}

impl Block {
    pub fn validate(&self) -> ValidationResult {
        use Block::*;

        match self {
            Section(contents) => contents.validate(),
            Image(contents) => contents.validate(),
            Actions(contents) => contents.validate(),
            Context(contents) => contents.validate(),
            Input(contents) => contents.validate(),
            File(contents) => contents.validate(),
            other => todo!("validation not implemented for {}", other),
        }
    }
}

impl_from_contents!(Block, Section, section::Contents);
impl_from_contents!(Block, Actions, actions::Contents);
impl_from_contents!(Block, Context, context::Contents);

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

    use super::*;

    #[test_case(
        r#"{
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "my message"
            }
        }"#
        => matches Block::Section(section::Contents::Text(_));
        "section_text"
    )]
    #[test_case(
        r#"{
            "type": "section",
            "fields": [{
                "type": "mrkdwn",
                "text": "my message"
            }]
        }"#
        => matches Block::Section (section::Contents::Fields(_));
        "section_fields"
    )]
    pub fn block_should_deserialize(json: &str) -> Block {
        // arrange

        // act
        serde_json::from_str::<Block>(&json).expect("Failed to serialize")

        // assert
    }
}