slack_messaging/composition_objects/builders/
plain_text.rs

1use super::PlainText;
2
3impl PlainText {
4    /// Construct a [`PlainTextBuilder`].
5    pub fn builder() -> PlainTextBuilder {
6        PlainTextBuilder::default()
7    }
8}
9
10/// Builder for [`PlainText`] object.
11#[derive(Debug, Default)]
12pub struct PlainTextBuilder {
13    text: Option<String>,
14    emoji: Option<bool>,
15}
16
17impl PlainTextBuilder {
18    /// Set text.
19    ///
20    /// ```
21    /// # use slack_messaging::composition_objects::PlainText;
22    /// let text = PlainText::builder()
23    ///     .text("hello world")
24    ///     .build();
25    ///
26    /// let expected = serde_json::json!({
27    ///     "type": "plain_text",
28    ///     "text": "hello world",
29    /// });
30    ///
31    /// let json = serde_json::to_value(text).unwrap();
32    /// assert_eq!(json, expected);
33    /// ```
34    pub fn text(self, text: impl Into<String>) -> Self {
35        self.set_text(Some(text.into()))
36    }
37
38    /// Set text.
39    ///
40    /// ```
41    /// # use slack_messaging::composition_objects::PlainText;
42    /// let text = PlainText::builder()
43    ///     .set_text(Some("hello world".into()))
44    ///     .build();
45    ///
46    /// let expected = serde_json::json!({
47    ///     "type": "plain_text",
48    ///     "text": "hello world",
49    /// });
50    ///
51    /// let json = serde_json::to_value(text).unwrap();
52    /// assert_eq!(json, expected);
53    /// ```
54    pub fn set_text(self, text: Option<String>) -> Self {
55        Self { text, ..self }
56    }
57
58    /// Set emoji field.
59    ///
60    /// ```
61    /// # use slack_messaging::composition_objects::PlainText;
62    /// let text = PlainText::builder()
63    ///     .text("😊")
64    ///     .emoji(true)
65    ///     .build();
66    ///
67    /// let expected = serde_json::json!({
68    ///    "type": "plain_text",
69    ///    "text": "😊",
70    ///    "emoji": true
71    /// });
72    ///
73    /// let json = serde_json::to_value(text).unwrap();
74    /// assert_eq!(json, expected);
75    /// ```
76    pub fn emoji(self, emoji: bool) -> Self {
77        self.set_emoji(Some(emoji))
78    }
79
80    /// Set emoji field.
81    ///
82    /// ```
83    /// # use slack_messaging::composition_objects::PlainText;
84    /// let text = PlainText::builder()
85    ///     .text("😊")
86    ///     .set_emoji(Some(true))
87    ///     .build();
88    ///
89    /// let expected = serde_json::json!({
90    ///    "type": "plain_text",
91    ///    "text": "😊",
92    ///    "emoji": true
93    /// });
94    ///
95    /// let json = serde_json::to_value(text).unwrap();
96    /// assert_eq!(json, expected);
97    /// ```
98    pub fn set_emoji(self, emoji: Option<bool>) -> Self {
99        Self { emoji, ..self }
100    }
101
102    /// Build a [`PlainText`] object. This method will panic if `text` field is not set.
103    pub fn build(self) -> PlainText {
104        PlainText {
105            text: self.text.expect("text must be set to TextBuilder"),
106            emoji: self.emoji,
107        }
108    }
109
110    /// Get text value.
111    pub fn get_text(&self) -> &Option<String> {
112        &self.text
113    }
114
115    /// Get emoji value.
116    pub fn get_emoji(&self) -> &Option<bool> {
117        &self.emoji
118    }
119}