mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
moved implementations to core::types
This commit is contained in:
parent
0ae31c6454
commit
8ffb4db4cc
6 changed files with 159 additions and 133 deletions
|
@ -40,3 +40,54 @@ pub enum InlineKeyboardButtonKind {
|
||||||
/* CallbackGame(CallbackGame), TODO: разобраться, что с этим делать
|
/* CallbackGame(CallbackGame), TODO: разобраться, что с этим делать
|
||||||
* TODO: add LoginUrl, pay */
|
* 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -11,3 +11,111 @@ pub struct InlineKeyboardMarkup {
|
||||||
/// [`InlineKeyboardButton`] objects
|
/// [`InlineKeyboardButton`] objects
|
||||||
pub inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
mod inline_keyboard_button;
|
|
||||||
mod inline_keyboard_markup;
|
|
||||||
|
|
||||||
pub use self::{
|
|
||||||
inline_keyboard_button::InlineKeyboardButtonBuilder,
|
|
||||||
inline_keyboard_markup::InlineKeyboardMarkupBuilder,
|
|
||||||
};
|
|
|
@ -5,4 +5,3 @@ extern crate serde;
|
||||||
|
|
||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod keyboards;
|
|
||||||
|
|
Loading…
Reference in a new issue