mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Merge pull request #110 from teloxide/conversion_traits_for_parse_mode
Impl conversion traits for ParseMode
This commit is contained in:
commit
26fa7ffd06
1 changed files with 35 additions and 0 deletions
|
@ -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)]
|
||||
|
|
Loading…
Reference in a new issue