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
use crate::composition::text::Text::Plain;
use serde::Serialize;

/// An object containing some text, formatted either as `plain_text` or using `mrkdwn`,
/// our proprietary textual markup that's just different enough from Markdown to frustrate you.

const PLAIN_TEXT: &str = "plain_text";
const MARKDOWN: &str = "mrkdwn";

#[derive(Debug, Serialize, Clone)]
#[serde(untagged)]
pub enum Text {
    Plain(PlainText),
    Markdown(MarkdownText),
}

#[derive(Debug, Serialize, Clone)]
pub struct PlainText {
    #[serde(rename = "type")]
    type_name: &'static str,
    text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    emoji: Option<bool>,
}

#[derive(Debug, Serialize, Clone)]
pub struct MarkdownText {
    #[serde(rename = "type")]
    type_name: &'static str,
    text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    verbatim: Option<bool>,
}

impl From<&str> for PlainText {
    fn from(s: &str) -> Self {
        PlainText::new(s)
    }
}

impl From<String> for PlainText {
    fn from(s: String) -> Self {
        PlainText::new(s)
    }
}

impl From<&str> for MarkdownText {
    fn from(s: &str) -> Self {
        MarkdownText::new(s)
    }
}

impl From<String> for MarkdownText {
    fn from(s: String) -> Self {
        MarkdownText::new(s)
    }
}

impl Default for Text {
    fn default() -> Self {
        Plain(PlainText::default())
    }
}

impl Default for PlainText {
    fn default() -> Self {
        PlainText {
            type_name: PLAIN_TEXT,
            text: String::default(),
            emoji: Option::default(),
        }
    }
}

impl Default for MarkdownText {
    fn default() -> Self {
        MarkdownText {
            type_name: MARKDOWN,
            text: String::default(),
            verbatim: Option::default(),
        }
    }
}

impl PlainText {
    pub fn new(text: impl Into<String>) -> Self {
        PlainText {
            text: text.into(),
            ..PlainText::default()
        }
    }

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

impl MarkdownText {
    pub fn new(text: impl Into<String>) -> Self {
        MarkdownText {
            text: text.into(),
            ..MarkdownText::default()
        }
    }

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

#[cfg(test)]
mod test {
    use crate::composition::text::{MarkdownText, PlainText, Text};

    #[test]
    fn test_ser_new() {
        let text = Text::Plain(PlainText::new("plain"));
        let json = serde_json::to_string_pretty(&text).unwrap();
        let expected = r#"{
  "type": "plain_text",
  "text": "plain"
}"#;
        assert_eq!(json, expected);

        let text = Text::Markdown(MarkdownText::new("*markdown*"));
        let json = serde_json::to_string_pretty(&text).unwrap();
        let expected = r#"{
  "type": "mrkdwn",
  "text": "*markdown*"
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_plain() {
        let plain: PlainText = "plain".into();
        let json = serde_json::to_string_pretty(&plain).unwrap();
        let expected = r#"{
  "type": "plain_text",
  "text": "plain"
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_plain_emoji() {
        let plain: PlainText = "plain".into();
        let plain = plain.emoji(false);
        let json = serde_json::to_string_pretty(&plain).unwrap();
        let expected = r#"{
  "type": "plain_text",
  "text": "plain",
  "emoji": false
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_markdown() {
        let markdown: MarkdownText = "*markdown*".into();
        let json = serde_json::to_string_pretty(&markdown).unwrap();
        let expected = r#"{
  "type": "mrkdwn",
  "text": "*markdown*"
}"#;
        assert_eq!(json, expected);
    }

    #[test]
    fn test_ser_markdown_verbatim() {
        let markdown: MarkdownText = "*markdown*".into();
        let markdown = markdown.verbatim(false);
        let json = serde_json::to_string_pretty(&markdown).unwrap();
        let expected = r#"{
  "type": "mrkdwn",
  "text": "*markdown*",
  "verbatim": false
}"#;
        assert_eq!(json, expected);
    }
}