From 9b21b6089b0ef1e9ccff107873c527e2099b9c36 Mon Sep 17 00:00:00 2001 From: p0lunin Date: Tue, 31 Dec 2019 19:59:37 +0200 Subject: [PATCH 1/6] 5 --- src/types/parse_mode.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index 32a9eace..d48661dd 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -1,4 +1,6 @@ +use serde::export::TryFrom; use serde::{Deserialize, Serialize}; +use std::str::FromStr; /// ## Formatting options /// The Bot API supports basic formatting for messages. @@ -74,6 +76,45 @@ 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), + _ => Err(()), + } + } +} + +impl TryFrom for ParseMode { + type Error = (); + + fn try_from(value: String) -> Result { + let normalized = value.to_lowercase(); + match normalized.as_ref() { + "html" => Ok(ParseMode::HTML), + "markdown" => Ok(ParseMode::Markdown), + _ => Err(()), + } + } +} + +impl FromStr for ParseMode { + type Err = (); + + fn from_str(s: &str) -> Result { + let normalized = s.to_lowercase(); + match normalized.as_ref() { + "html" => Ok(ParseMode::HTML), + "markdown" => Ok(ParseMode::Markdown), + _ => Err(()), + } + } +} + #[cfg(test)] mod tests { use super::*; From 60532be947e0c5533fa35fae6a5f6851f11149a7 Mon Sep 17 00:00:00 2001 From: p0lunin Date: Wed, 1 Jan 2020 20:01:26 +0200 Subject: [PATCH 2/6] refactoring --- src/types/parse_mode.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index a7cbe566..aac3baca 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -2,9 +2,10 @@ // (for built ins there no warnings, but for (De)Serialize, there are) #![allow(deprecated)] -use std::{str::FromStr, convert::TryFrom} +use std::{str::FromStr, convert::TryFrom}; use serde::{Deserialize, Serialize}; +use std::convert::TryInto; /// ## Formatting options /// The Bot API supports basic formatting for messages. You can use bold, @@ -137,6 +138,7 @@ impl TryFrom<&str> for ParseMode { let normalized = value.to_lowercase(); match normalized.as_ref() { "html" => Ok(ParseMode::HTML), + #[allow(depredecated)] "markdown" => Ok(ParseMode::Markdown), _ => Err(()), } @@ -147,12 +149,7 @@ impl TryFrom for ParseMode { type Error = (); fn try_from(value: String) -> Result { - let normalized = value.to_lowercase(); - match normalized.as_ref() { - "html" => Ok(ParseMode::HTML), - "markdown" => Ok(ParseMode::Markdown), - _ => Err(()), - } + value.as_str().try_into() } } @@ -160,12 +157,7 @@ impl FromStr for ParseMode { type Err = (); fn from_str(s: &str) -> Result { - let normalized = s.to_lowercase(); - match normalized.as_ref() { - "html" => Ok(ParseMode::HTML), - "markdown" => Ok(ParseMode::Markdown), - _ => Err(()), - } + s.try_into() } } From 9b2694657eeff3da2fe81790c7fef28543cf8476 Mon Sep 17 00:00:00 2001 From: p0lunin Date: Wed, 1 Jan 2020 20:01:40 +0200 Subject: [PATCH 3/6] updated to bot api v4.5 --- src/types/parse_mode.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index aac3baca..7ef28e65 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -140,6 +140,7 @@ impl TryFrom<&str> for ParseMode { "html" => Ok(ParseMode::HTML), #[allow(depredecated)] "markdown" => Ok(ParseMode::Markdown), + "markdownv2" => Ok(ParseMode::MarkdownV2), _ => Err(()), } } From 31326b20676d4a815779673837ce2edfd5fffec6 Mon Sep 17 00:00:00 2001 From: p0lunin Date: Wed, 1 Jan 2020 20:08:10 +0200 Subject: [PATCH 4/6] refactoring --- src/types/parse_mode.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index 7ef28e65..40a21e73 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -2,10 +2,9 @@ // (for built ins there no warnings, but for (De)Serialize, there are) #![allow(deprecated)] -use std::{str::FromStr, convert::TryFrom}; +use std::{str::FromStr, convert::{TryFrom, TryInto}}; use serde::{Deserialize, Serialize}; -use std::convert::TryInto; /// ## Formatting options /// The Bot API supports basic formatting for messages. You can use bold, From 2a4b46299643a06e47ecf0da91ea9733ad6e190b Mon Sep 17 00:00:00 2001 From: p0lunin Date: Thu, 2 Jan 2020 12:04:47 +0200 Subject: [PATCH 5/6] rustfmt --- src/types/parse_mode.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index 40a21e73..4557dfe5 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -2,7 +2,10 @@ // (for built ins there no warnings, but for (De)Serialize, there are) #![allow(deprecated)] -use std::{str::FromStr, convert::{TryFrom, TryInto}}; +use std::{ + convert::{TryFrom, TryInto}, + str::FromStr, +}; use serde::{Deserialize, Serialize}; From 4d3d96be7ccc6a875538a81cb8edfcc61855a9dc Mon Sep 17 00:00:00 2001 From: p0lunin Date: Thu, 2 Jan 2020 12:17:56 +0200 Subject: [PATCH 6/6] remove #[allow(depredecated)] in match --- src/types/parse_mode.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index 4557dfe5..871a4af8 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -140,7 +140,6 @@ impl TryFrom<&str> for ParseMode { let normalized = value.to_lowercase(); match normalized.as_ref() { "html" => Ok(ParseMode::HTML), - #[allow(depredecated)] "markdown" => Ok(ParseMode::Markdown), "markdownv2" => Ok(ParseMode::MarkdownV2), _ => Err(()),