Merge pull request #110 from teloxide/conversion_traits_for_parse_mode

Impl conversion traits for ParseMode
This commit is contained in:
Temirkhan Myrzamadi 2020-01-02 16:29:17 +06:00 committed by GitHub
commit 26fa7ffd06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<Self, Self::Error> {
let normalized = value.to_lowercase();
match normalized.as_ref() {
"html" => Ok(ParseMode::HTML),
"markdown" => Ok(ParseMode::Markdown),
"markdownv2" => Ok(ParseMode::MarkdownV2),
_ => Err(()),
}
}
}
impl TryFrom<String> for ParseMode {
type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> {
value.as_str().try_into()
}
}
impl FromStr for ParseMode {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.try_into()
}
}
#[cfg(test)]
mod tests {
#![allow(deprecated)]