moved implementations to core::types

This commit is contained in:
P0lunin 2019-09-15 13:45:21 +03:00
parent 0ae31c6454
commit 8ffb4db4cc
6 changed files with 159 additions and 133 deletions

View file

@ -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
)
}
}
}

View file

@ -11,3 +11,111 @@ pub struct InlineKeyboardMarkup {
/// [`InlineKeyboardButton`] objects
pub inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
}
/// 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<InlineKeyboardButton>) -> 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);
}
}

View file

@ -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
)
}
}
}

View file

@ -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<InlineKeyboardButton>) -> 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);
}
}

View file

@ -1,7 +0,0 @@
mod inline_keyboard_button;
mod inline_keyboard_markup;
pub use self::{
inline_keyboard_button::InlineKeyboardButtonBuilder,
inline_keyboard_markup::InlineKeyboardMarkupBuilder,
};

View file

@ -5,4 +5,3 @@ extern crate serde;
pub mod bot;
pub mod core;
pub mod keyboards;