From 8ffb4db4cc35bbbbcd4f5b8f5fa88f09d9f15e20 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Sun, 15 Sep 2019 13:45:21 +0300 Subject: [PATCH] moved implementations to core::types --- src/core/types/inline_keyboard_button.rs | 51 +++++++++++ src/core/types/inline_keyboard_markup.rs | 108 +++++++++++++++++++++++ src/keyboards/inline_keyboard_button.rs | 55 ------------ src/keyboards/inline_keyboard_markup.rs | 70 --------------- src/keyboards/mod.rs | 7 -- src/lib.rs | 1 - 6 files changed, 159 insertions(+), 133 deletions(-) delete mode 100644 src/keyboards/inline_keyboard_button.rs delete mode 100644 src/keyboards/inline_keyboard_markup.rs delete mode 100644 src/keyboards/mod.rs diff --git a/src/core/types/inline_keyboard_button.rs b/src/core/types/inline_keyboard_button.rs index 73389ae6..ba2bcc4f 100644 --- a/src/core/types/inline_keyboard_button.rs +++ b/src/core/types/inline_keyboard_button.rs @@ -40,3 +40,54 @@ pub enum InlineKeyboardButtonKind { /* CallbackGame(CallbackGame), TODO: разобраться, что с этим делать * TODO: add LoginUrl, pay */ } + +/// Build buttons +/// +/// Example: +/// ```edition2018 +/// use async_telegram_bot::core::types::InlineKeyboardButton; +/// +/// fn main() { +/// let url_button = InlineKeyboardButton::url( +/// "Text".to_string(), +/// "http://url.com".to_string(), +/// ); +/// } +/// ``` +impl InlineKeyboardButton { + pub fn url(text: String, url: String) -> InlineKeyboardButton { + InlineKeyboardButton { + text, + kind: InlineKeyboardButtonKind::Url(url), + } + } + + pub fn callback(text: String, callback_data: String) + -> InlineKeyboardButton { + InlineKeyboardButton { + text, + kind: InlineKeyboardButtonKind::CallbackData(callback_data), + } + } + + pub fn switch_inline_query(text: String, switch_inline_query: String) + -> InlineKeyboardButton { + InlineKeyboardButton { + text, + kind: InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query) + } + } + + pub fn switch_inline_query_current_chat( + text: String, + switch_inline_query_current_chat: String + ) -> InlineKeyboardButton { + + InlineKeyboardButton { + text, + kind: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat( + switch_inline_query_current_chat + ) + } + } +} diff --git a/src/core/types/inline_keyboard_markup.rs b/src/core/types/inline_keyboard_markup.rs index 77f8eee7..5dd68af3 100644 --- a/src/core/types/inline_keyboard_markup.rs +++ b/src/core/types/inline_keyboard_markup.rs @@ -11,3 +11,111 @@ pub struct InlineKeyboardMarkup { /// [`InlineKeyboardButton`] objects pub inline_keyboard: Vec>, } + +/// Build Markup +/// +/// Example: +/// ```edition2018 +/// use async_telegram_bot::core::types::{ +/// InlineKeyboardMarkup, +/// InlineKeyboardButton +/// }; +/// +/// fn main() { +/// let url_button = InlineKeyboardButton::url( +/// "text".to_string(), +/// "http://url.com".to_string() +/// ); +/// let keyboard = InlineKeyboardMarkup::new() +/// .row(vec![url_button]); +/// } +/// ``` +impl InlineKeyboardMarkup { + pub fn new() -> Self { + Self { + inline_keyboard: vec![] + } + } + + pub fn append_row(mut self, buttons: Vec) -> Self { + self.inline_keyboard.push(buttons); + self + } + + pub fn append_to_row(mut self, button: InlineKeyboardButton, index: usize) + -> Self { + match self.inline_keyboard.get_mut(index) { + Some(buttons) => buttons.push(button), + None => self.inline_keyboard.push(vec![button]) + }; + self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn append_row() { + let button1 = InlineKeyboardButton::url( + "text 1".to_string(), + "url 1".to_string(), + ); + let button2 = InlineKeyboardButton::url( + "text 2".to_string(), + "url 2".to_string(), + ); + let markup = InlineKeyboardMarkup::new() + .append_row(vec![button1.clone(), button2.clone()]); + let expected = InlineKeyboardMarkup { + inline_keyboard: vec![ + vec![button1.clone(), button2.clone()] + ] + }; + assert_eq!(markup, expected); + } + + #[test] + fn append_to_row__existent_row() { + let button1 = InlineKeyboardButton::url( + "text 1".to_string(), + "url 1".to_string(), + ); + let button2 = InlineKeyboardButton::url( + "text 2".to_string(), + "url 2".to_string(), + ); + let markup = InlineKeyboardMarkup::new() + .append_row(vec![button1.clone()]) + .append_to_row(button2.clone(), 0); + let expected = InlineKeyboardMarkup { + inline_keyboard: vec![ + vec![button1.clone(), button2.clone()] + ] + }; + assert_eq!(markup, expected); + } + + #[test] + fn append_to_row__nonexistent_row() { + let button1 = InlineKeyboardButton::url( + "text 1".to_string(), + "url 1".to_string(), + ); + let button2 = InlineKeyboardButton::url( + "text 2".to_string(), + "url 2".to_string(), + ); + let markup = InlineKeyboardMarkup::new() + .append_row(vec![button1.clone()]) + .append_to_row(button2.clone(), 1); + let expected = InlineKeyboardMarkup { + inline_keyboard: vec![ + vec![button1.clone()], + vec![button2.clone()] + ] + }; + assert_eq!(markup, expected); + } +} diff --git a/src/keyboards/inline_keyboard_button.rs b/src/keyboards/inline_keyboard_button.rs deleted file mode 100644 index 9cc43188..00000000 --- a/src/keyboards/inline_keyboard_button.rs +++ /dev/null @@ -1,55 +0,0 @@ -use crate::core::types::{InlineKeyboardButton, InlineKeyboardButtonKind}; - -pub struct InlineKeyboardButtonBuilder; - -/// Build buttons -/// -/// Example: -/// ```edition2018 -/// use async_telegram_bot::keyboards::InlineKeyboardButtonBuilder; -/// -/// fn main() { -/// let url_button = InlineKeyboardButtonBuilder::url( -/// "Text".to_string(), -/// "http://url.com".to_string(), -/// ); -/// } -/// ``` -impl InlineKeyboardButtonBuilder { - pub fn url(text: String, url: String) -> InlineKeyboardButton { - InlineKeyboardButton { - text, - kind: InlineKeyboardButtonKind::Url(url), - } - } - - pub fn callback(text: String, callback_data: String) - -> InlineKeyboardButton { - InlineKeyboardButton { - text, - kind: InlineKeyboardButtonKind::CallbackData(callback_data), - } - } - - pub fn switch_inline_query(text: String, switch_inline_query: String) - -> InlineKeyboardButton { - InlineKeyboardButton { - text, - kind: InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query) - } - } - - pub fn switch_inline_query_current_chat( - text: String, - switch_inline_query_current_chat: String - ) -> InlineKeyboardButton { - - InlineKeyboardButton { - text, - kind: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat( - switch_inline_query_current_chat - ) - } - } -} - diff --git a/src/keyboards/inline_keyboard_markup.rs b/src/keyboards/inline_keyboard_markup.rs deleted file mode 100644 index e0ff0663..00000000 --- a/src/keyboards/inline_keyboard_markup.rs +++ /dev/null @@ -1,70 +0,0 @@ -use crate::core::types::{InlineKeyboardMarkup, InlineKeyboardButton}; - -pub struct InlineKeyboardMarkupBuilder { - keyboard: InlineKeyboardMarkup, -} - -/// Builder for [`InlineKeyboardMarkup`] -/// -/// Example: -/// ```edition2018 -/// use async_telegram_bot::keyboards; -/// -/// fn main() { -/// let url_button = keyboards::InlineKeyboardButtonBuilder::url( -/// "text".to_string(), -/// "http://url.com".to_string() -/// ); -/// let keyboard = keyboards::InlineKeyboardMarkupBuilder::new() -/// .row(vec![url_button]) -/// .build(); -/// } -/// ``` -impl InlineKeyboardMarkupBuilder { - pub fn new() -> Self { - Self { - keyboard: InlineKeyboardMarkup { - inline_keyboard: vec![] - } - } - } - - pub fn row(mut self, buttons: Vec) -> Self { - self.keyboard.inline_keyboard.push(buttons); - self - } - - pub fn append_to_row(mut self, button: InlineKeyboardButton, index: usize) - -> Self { - match self.keyboard.inline_keyboard.get_mut(index) { - Some(buttons) => buttons.push(button), - None => self.keyboard.inline_keyboard.push(vec![button]) - }; - self - } - - pub fn build(self) -> InlineKeyboardMarkup { - self.keyboard - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::keyboards::InlineKeyboardButtonBuilder; - - #[test] - fn test_row() { - let btn = InlineKeyboardButtonBuilder::url( - "text".to_string(), - "http://url".to_string(), - ); - let kb = InlineKeyboardMarkupBuilder::new() - .row(vec![btn.clone()]) - .build(); - let expected = InlineKeyboardMarkup { - inline_keyboard: vec![vec![btn.clone()]], - }; - assert_eq!(kb, expected); - } -} diff --git a/src/keyboards/mod.rs b/src/keyboards/mod.rs deleted file mode 100644 index 63fac65a..00000000 --- a/src/keyboards/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod inline_keyboard_button; -mod inline_keyboard_markup; - -pub use self::{ - inline_keyboard_button::InlineKeyboardButtonBuilder, - inline_keyboard_markup::InlineKeyboardMarkupBuilder, -}; diff --git a/src/lib.rs b/src/lib.rs index 0d2c5221..7425f248 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,3 @@ extern crate serde; pub mod bot; pub mod core; -pub mod keyboards;