slack_blocks/compose/text/plain.rs
1//! Just plain text
2//!
3//! Only formatting available is emojis.
4
5use serde::{Deserialize, Serialize};
6
7/// Just plain text
8///
9/// Only formatting available is emojis.
10#[derive(Clone, Debug, Default, Deserialize, Hash, PartialEq, Serialize)]
11pub struct Contents {
12 pub(super) text: String,
13
14 #[serde(skip_serializing_if = "Option::is_none")]
15 pub(super) emoji: Option<bool>,
16}
17
18impl Contents {
19 /// Construct some markdown text from a string or string-like
20 /// value
21 ///
22 /// # Arguments
23 /// - `text` - The text contents to render for this `Text` object.
24 /// For some basic formatting examples, see the docs above for
25 /// the Contents struct itself, or [Slack's markdown docs 🔗].
26 /// There are no intrinsic length limits on this, those are usually
27 /// requirements of the context the text will be used in.
28 ///
29 /// [Slack's markdown docs 🔗]: https://api.slack.com/reference/surfaces/formatting
30 ///
31 /// # Example
32 /// ```
33 /// use slack_blocks::compose::text::{mrkdwn, Text};
34 ///
35 /// let text = mrkdwn::Contents::from_text("This link doesn't work! :tada: https://www.cheese.com")
36 /// .with_verbatim(true);
37 /// ```
38 pub fn from_text(text: impl ToString) -> Self {
39 Into::<Self>::into(text.to_string())
40 }
41
42 /// Sets the `emoji` flag
43 ///
44 /// # Arguments
45 /// - `emoji` - Indicates whether emojis in a text field should be
46 /// escaped into the colon emoji format
47 ///
48 /// # Example
49 /// ```
50 /// use slack_blocks::compose::text::{plain, Text};
51 ///
52 /// let text = plain::Contents::from_text("Emojis!! :tada:").with_emoji(true);
53 /// ```
54 pub fn with_emoji(mut self, emoji: bool) -> Self {
55 self.emoji = Some(emoji);
56 self
57 }
58}
59
60impl AsRef<str> for Contents {
61 fn as_ref(&self) -> &str {
62 &self.text
63 }
64}
65
66impl From<String> for Contents {
67 fn from(text: String) -> Self {
68 Self { text, emoji: None }
69 }
70}
71
72impl From<&String> for Contents {
73 fn from(text: &String) -> Self {
74 text.as_str().into()
75 }
76}
77
78impl From<&str> for Contents {
79 fn from(text: &str) -> Self {
80 Self::from_text(text)
81 }
82}