mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-18 15:20:15 +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)
|
// (for built ins there no warnings, but for (De)Serialize, there are)
|
||||||
#![allow(deprecated)]
|
#![allow(deprecated)]
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
convert::{TryFrom, TryInto},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
/// ## Formatting options
|
/// ## Formatting options
|
||||||
|
@ -128,6 +133,36 @@ pub enum ParseMode {
|
||||||
Markdown,
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#![allow(deprecated)]
|
#![allow(deprecated)]
|
||||||
|
|
Loading…
Reference in a new issue