From 73fd76997d9a4de0cdd62b25e34768d2820c4bc6 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Thu, 12 Sep 2019 21:32:54 +0300 Subject: [PATCH] add builders --- src/core/types/mod.rs | 2 +- src/keyboards/inline_keyboard_button.rs | 55 +++++++++++++++++++ src/keyboards/inline_keyboard_markup.rs | 70 +++++++++++++++++++++++++ src/keyboards/mod.rs | 7 +++ src/lib.rs | 1 + 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 src/keyboards/inline_keyboard_button.rs create mode 100644 src/keyboards/inline_keyboard_markup.rs create mode 100644 src/keyboards/mod.rs diff --git a/src/core/types/mod.rs b/src/core/types/mod.rs index bbbd4237..d602573a 100644 --- a/src/core/types/mod.rs +++ b/src/core/types/mod.rs @@ -9,7 +9,7 @@ pub use self::{ chat_photo::ChatPhoto, document::Document, force_reply::ForceReply, - inline_keyboard_button::InlineKeyboardButton, + inline_keyboard_button::{InlineKeyboardButton, InlineKeyboardButtonKind}, inline_keyboard_markup::InlineKeyboardMarkup, input_file::InputFile, input_media::InputMedia, diff --git a/src/keyboards/inline_keyboard_button.rs b/src/keyboards/inline_keyboard_button.rs new file mode 100644 index 00000000..9cc43188 --- /dev/null +++ b/src/keyboards/inline_keyboard_button.rs @@ -0,0 +1,55 @@ +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 new file mode 100644 index 00000000..3fd49ee7 --- /dev/null +++ b/src/keyboards/inline_keyboard_markup.rs @@ -0,0 +1,70 @@ +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(&mut mut 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 new file mode 100644 index 00000000..63fac65a --- /dev/null +++ b/src/keyboards/mod.rs @@ -0,0 +1,7 @@ +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 7425f248..0d2c5221 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,4 @@ extern crate serde; pub mod bot; pub mod core; +pub mod keyboards;