Merge pull request #231 from teloxide/inline_kb_button_cleanup

`InlineKeyboardButton` cleanup
This commit is contained in:
Waffle Maybe 2022-06-29 08:56:30 +04:00 committed by GitHub
commit 850e3c08ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 57 deletions

View file

@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased ## unreleased
### Added
- `InlineKeyboardButton::{pay, login, web_app, callback_game, pay}` constructors ([#231][pr231])
### Changed
- `InlineKeyboardButtonKind::Pay`'s only field now has type `True` ([#231][pr231])
### Deprecated
- `InlineKeyboardButton::{text, kind}` functions ([#231][pr231])
[pr231]: https://github.com/teloxide/teloxide-core/pull/231
### Removed ### Removed
- `ChatPrivate::type_` field ([#232][pr232]) - `ChatPrivate::type_` field ([#232][pr232])

View file

@ -1,4 +1,4 @@
use crate::types::{CallbackGame, LoginUrl, WebAppInfo}; use crate::types::{CallbackGame, LoginUrl, True, WebAppInfo};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// This object represents one button of an inline keyboard. /// This object represents one button of an inline keyboard.
@ -13,31 +13,6 @@ pub struct InlineKeyboardButton {
pub kind: InlineKeyboardButtonKind, pub kind: InlineKeyboardButtonKind,
} }
impl InlineKeyboardButton {
pub fn new<S>(text: S, kind: InlineKeyboardButtonKind) -> Self
where
S: Into<String>,
{
Self {
text: text.into(),
kind,
}
}
pub fn text<S>(mut self, val: S) -> Self
where
S: Into<String>,
{
self.text = val.into();
self
}
pub fn kind(mut self, val: InlineKeyboardButtonKind) -> Self {
self.kind = val;
self
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum InlineKeyboardButtonKind { pub enum InlineKeyboardButtonKind {
@ -83,7 +58,7 @@ pub enum InlineKeyboardButtonKind {
/// switched from, skipping the chat selection screen. /// switched from, skipping the chat selection screen.
/// ///
/// [inline mode]: https://core.telegram.org/bots/inline /// [inline mode]: https://core.telegram.org/bots/inline
/// [switch_pm…]: https://core.telegram.org/bots/api#answerinlinequery /// [switch_pm…]: crate::payloads::AnswerInlineQuery
SwitchInlineQuery(String), SwitchInlineQuery(String),
/// If set, pressing the button will insert the bots username and the /// If set, pressing the button will insert the bots username and the
@ -99,6 +74,7 @@ pub enum InlineKeyboardButtonKind {
/// button. /// button.
/// ///
/// ## Note /// ## Note
///
/// This type of button **must** always be the first button in the first /// This type of button **must** always be the first button in the first
/// row. /// row.
CallbackGame(CallbackGame), CallbackGame(CallbackGame),
@ -106,69 +82,144 @@ pub enum InlineKeyboardButtonKind {
/// Specify True, to send a [Pay button]. /// Specify True, to send a [Pay button].
/// ///
/// ## Note /// ## Note
///
/// This type of button **must** always be the first button in the first /// This type of button **must** always be the first button in the first
/// row. /// row.
/// ///
/// [Pay button]: https://core.telegram.org/bots/api#payments /// [Pay button]: https://core.telegram.org/bots/api#payments
// FIXME(waffle): This should be using `True` Pay(True),
Pay(bool),
} }
/// Build buttons.
///
/// # Examples
/// ```
/// use teloxide_core::types::InlineKeyboardButton;
///
/// let url = url::Url::parse("https://example.com").unwrap();
/// let url_button = InlineKeyboardButton::url("Text".to_string(), url);
/// ```
impl InlineKeyboardButton { impl InlineKeyboardButton {
pub fn url<T>(text: T, url: reqwest::Url) -> InlineKeyboardButton /// Creates a new `InlineKeyboardButton`.
pub fn new<S>(text: S, kind: InlineKeyboardButtonKind) -> Self
where where
T: Into<String>, S: Into<String>,
{ {
InlineKeyboardButton { Self {
text: text.into(), text: text.into(),
kind: InlineKeyboardButtonKind::Url(url), kind,
} }
} }
pub fn callback<T, C>(text: T, callback_data: C) -> InlineKeyboardButton /// Constructor for `InlineKeyboardButton` with [`Url`] kind.
///
/// [`Url`]: InlineKeyboardButtonKind::Url
pub fn url<T>(text: T, url: reqwest::Url) -> Self
where
T: Into<String>,
{
Self::new(text, InlineKeyboardButtonKind::Url(url))
}
/// Constructor for `InlineKeyboardButton` with [`LoginUrl`] kind.
///
/// [`LoginUrl`]: InlineKeyboardButtonKind::LoginUrl
pub fn login<T>(text: T, url: LoginUrl) -> Self
where
T: Into<String>,
{
Self::new(text, InlineKeyboardButtonKind::LoginUrl(url))
}
/// Constructor for `InlineKeyboardButton` with [`CallbackData`] kind.
///
/// [`CallbackData`]: InlineKeyboardButtonKind::CallbackData
pub fn callback<T, C>(text: T, callback_data: C) -> Self
where where
T: Into<String>, T: Into<String>,
C: Into<String>, C: Into<String>,
{ {
InlineKeyboardButton { Self::new(
text: text.into(), text,
kind: InlineKeyboardButtonKind::CallbackData(callback_data.into()), InlineKeyboardButtonKind::CallbackData(callback_data.into()),
} )
} }
pub fn switch_inline_query<T, Q>(text: T, switch_inline_query: Q) -> InlineKeyboardButton /// Constructor for `InlineKeyboardButton` with [`WebApp`] kind.
///
/// [`WebApp`]: InlineKeyboardButtonKind::WebApp
pub fn web_app<T>(text: T, info: WebAppInfo) -> Self
where
T: Into<String>,
{
Self::new(text, InlineKeyboardButtonKind::WebApp(info))
}
/// Constructor for `InlineKeyboardButton` with [`SwitchInlineQuery`] kind.
///
/// [`SwitchInlineQuery`]: InlineKeyboardButtonKind::SwitchInlineQuery
pub fn switch_inline_query<T, Q>(text: T, switch_inline_query: Q) -> Self
where where
T: Into<String>, T: Into<String>,
Q: Into<String>, Q: Into<String>,
{ {
InlineKeyboardButton { Self::new(
text: text.into(), text,
kind: InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query.into()), InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query.into()),
} )
} }
/// Constructor for `InlineKeyboardButton` with
/// [`SwitchInlineQueryCurrentChat`] kind.
///
/// [`SwitchInlineQueryCurrentChat`]: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat
pub fn switch_inline_query_current_chat<T, Q>( pub fn switch_inline_query_current_chat<T, Q>(
text: T, text: T,
switch_inline_query_current_chat: Q, switch_inline_query_current_chat: Q,
) -> InlineKeyboardButton ) -> Self
where where
T: Into<String>, T: Into<String>,
Q: Into<String>, Q: Into<String>,
{ {
InlineKeyboardButton { Self::new(
text: text.into(), text,
kind: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat( InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat(
switch_inline_query_current_chat.into(), switch_inline_query_current_chat.into(),
), ),
} )
}
/// Constructor for `InlineKeyboardButton` with [`CallbackGame`] kind.
///
/// [`CallbackGame`]: InlineKeyboardButtonKind::CallbackGame
pub fn callback_game<T>(text: T, game: CallbackGame) -> Self
where
T: Into<String>,
{
Self::new(text, InlineKeyboardButtonKind::CallbackGame(game))
}
/// Constructor for `InlineKeyboardButton` with [`Pay`] kind.
///
/// [`Pay`]: InlineKeyboardButtonKind::Pay
pub fn pay<T>(text: T) -> Self
where
T: Into<String>,
{
Self::new(text, InlineKeyboardButtonKind::Pay(True))
}
}
impl InlineKeyboardButton {
#[deprecated(
since = "0.7.0",
note = "set correct text in the constructor or access the field directly"
)]
pub fn text<S>(mut self, val: S) -> Self
where
S: Into<String>,
{
self.text = val.into();
self
}
#[deprecated(
since = "0.7.0",
note = "set correct kind in the constructor or access the field directly"
)]
pub fn kind(mut self, val: InlineKeyboardButtonKind) -> Self {
self.kind = val;
self
} }
} }