diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index cef3b02f..871a4af8 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -2,6 +2,11 @@ // (for built ins there no warnings, but for (De)Serialize, there are) #![allow(deprecated)] +use std::{ + convert::{TryFrom, TryInto}, + str::FromStr, +}; + use serde::{Deserialize, Serialize}; /// ## Formatting options @@ -128,6 +133,36 @@ pub enum ParseMode { Markdown, } +impl TryFrom<&str> for ParseMode { + type Error = (); + + fn try_from(value: &str) -> Result { + let normalized = value.to_lowercase(); + match normalized.as_ref() { + "html" => Ok(ParseMode::HTML), + "markdown" => Ok(ParseMode::Markdown), + "markdownv2" => Ok(ParseMode::MarkdownV2), + _ => Err(()), + } + } +} + +impl TryFrom for ParseMode { + type Error = (); + + fn try_from(value: String) -> Result { + value.as_str().try_into() + } +} + +impl FromStr for ParseMode { + type Err = (); + + fn from_str(s: &str) -> Result { + s.try_into() + } +} + #[cfg(test)] mod tests { #![allow(deprecated)]