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
use serde::Serialize;
use std::fmt::{self, Display};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize)]
pub enum ParseMode {
Markdown,
#[serde(rename = "HTML")]
Html,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Text<'a> {
pub(crate) text: &'a str,
pub(crate) parse_mode: Option<ParseMode>,
}
impl Display for ParseMode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
ParseMode::Markdown => "Markdown",
ParseMode::Html => "HTML",
})
}
}
impl<'a> Text<'a> {
pub const fn plain(text: &'a str) -> Self {
Self {
text,
parse_mode: None,
}
}
pub fn markdown(text: &'a str) -> Self {
Self {
text,
parse_mode: Some(ParseMode::Markdown),
}
}
pub fn html(text: &'a str) -> Self {
Self {
text,
parse_mode: Some(ParseMode::Html),
}
}
pub fn is_plain(self) -> bool {
self.parse_mode == None
}
pub fn is_markdown(self) -> bool {
self.parse_mode == Some(ParseMode::Markdown)
}
pub fn is_html(self) -> bool {
self.parse_mode == Some(ParseMode::Html)
}
}
impl<'a> From<&'a str> for Text<'a> {
fn from(text: &'a str) -> Self {
Text::plain(text)
}
}
impl<'a> From<&'a String> for Text<'a> {
fn from(text: &'a String) -> Self {
Text::plain(text.as_str())
}
}