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
//! Just plain text
//!
//! Only formatting available is emojis.

use serde::{Deserialize, Serialize};

/// Just plain text
///
/// Only formatting available is emojis.
#[derive(Clone, Debug, Default, Deserialize, Hash, PartialEq, Serialize)]
pub struct Contents {
  text: String,

  #[serde(skip_serializing_if = "Option::is_none")]
  emoji: Option<bool>,
}

impl Contents {
  /// Construct some markdown text from a string or string-like
  /// value
  ///
  /// # Arguments
  /// - `text` - The text contents to render for this `Text` object.
  ///     For some basic formatting examples, see the docs above for
  ///     the Contents struct itself, or [Slack's markdown docs 🔗].
  ///     There are no intrinsic length limits on this, those are usually
  ///     requirements of the context the text will be used in.
  ///
  /// [Slack's markdown docs 🔗]: https://api.slack.com/reference/surfaces/formatting
  ///
  /// # Example
  /// ```
  /// use slack_blocks::compose::text::{mrkdwn, Text};
  ///
  /// let text = mrkdwn::Contents::from_text("This link doesn't work! :tada: https://www.cheese.com")
  ///     .with_verbatim(true);
  /// ```
  pub fn from_text(text: impl ToString) -> Self {
    Into::<Self>::into(text.to_string())
  }

  /// Sets the `emoji` flag
  ///
  /// # Arguments
  /// - `emoji` - Indicates whether emojis in a text field should be
  ///     escaped into the colon emoji format
  ///
  /// # Example
  /// ```
  /// use slack_blocks::compose::text::{plain, Text};
  ///
  /// let text = plain::Contents::from_text("Emojis!! :tada:").with_emoji(true);
  /// ```
  pub fn with_emoji(mut self, emoji: bool) -> Self {
    self.emoji = Some(emoji);
    self
  }
}

impl AsRef<str> for Contents {
  fn as_ref(&self) -> &str {
    &self.text
  }
}

impl From<String> for Contents {
  fn from(text: String) -> Self {
    Self { text, emoji: None }
  }
}

impl From<&String> for Contents {
  fn from(text: &String) -> Self {
    text.as_str().into()
  }
}

impl From<&str> for Contents {
  fn from(text: &str) -> Self {
    Self::from_text(text)
  }
}