frankenstein/
parse_mode.rs

1#![allow(deprecated)]
2
3use std::fmt::Display;
4use std::str::FromStr;
5
6use serde::{Deserialize, Serialize};
7
8/// Text Formatting Options
9///
10/// See <https://core.telegram.org/bots/api#formatting-options>
11#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
12pub enum ParseMode {
13    #[serde(rename = "HTML")]
14    Html,
15
16    MarkdownV2,
17
18    #[deprecated = "This is a legacy mode, retained for backward compatibility. Use `MarkdownV2` instead."]
19    Markdown,
20}
21
22impl FromStr for ParseMode {
23    type Err = ();
24
25    fn from_str(s: &str) -> Result<Self, Self::Err> {
26        match s {
27            "HTML" | "Html" | "html" => Ok(Self::Html),
28            "Markdown" | "markdown" => Ok(Self::Markdown),
29            "MarkdownV2" | "markdownv2" => Ok(Self::MarkdownV2),
30            _ => Err(()),
31        }
32    }
33}
34
35impl ParseMode {
36    #[must_use]
37    pub const fn to_str(self) -> &'static str {
38        match self {
39            Self::Html => "HTML",
40            Self::MarkdownV2 => "MarkdownV2",
41            Self::Markdown => "Markdown",
42        }
43    }
44}
45
46impl Display for ParseMode {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}", self.to_str())
49    }
50}
51
52#[test]
53fn serde_markdown_works() {
54    crate::test_json::assert_json_str(&ParseMode::MarkdownV2, r#""MarkdownV2""#);
55}
56
57#[test]
58fn serde_html_works() {
59    crate::test_json::assert_json_str(&ParseMode::Html, r#""HTML""#);
60}