From 3a2c8a8ffe86cbf4512d022b44934a9142d64c75 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:04:23 +0400 Subject: [PATCH 1/7] Make `InlineKeyboardButtonKind::Pay.0` have type `True` --- src/types/inline_keyboard_button.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/types/inline_keyboard_button.rs b/src/types/inline_keyboard_button.rs index f776d54f..add4988a 100644 --- a/src/types/inline_keyboard_button.rs +++ b/src/types/inline_keyboard_button.rs @@ -1,4 +1,4 @@ -use crate::types::{CallbackGame, LoginUrl, WebAppInfo}; +use crate::types::{CallbackGame, LoginUrl, True, WebAppInfo}; use serde::{Deserialize, Serialize}; /// This object represents one button of an inline keyboard. @@ -110,8 +110,7 @@ pub enum InlineKeyboardButtonKind { /// row. /// /// [Pay button]: https://core.telegram.org/bots/api#payments - // FIXME(waffle): This should be using `True` - Pay(bool), + Pay(True), } /// Build buttons. From 71555ba43e9d22e1aee6c7726c896967ea2920c7 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:05:16 +0400 Subject: [PATCH 2/7] Add more constructors to `InlineKeyboardButton` --- src/types/inline_keyboard_button.rs | 97 +++++++++++++++++++---------- 1 file changed, 63 insertions(+), 34 deletions(-) diff --git a/src/types/inline_keyboard_button.rs b/src/types/inline_keyboard_button.rs index add4988a..7c7cd018 100644 --- a/src/types/inline_keyboard_button.rs +++ b/src/types/inline_keyboard_button.rs @@ -13,31 +13,6 @@ pub struct InlineKeyboardButton { pub kind: InlineKeyboardButtonKind, } -impl InlineKeyboardButton { - pub fn new(text: S, kind: InlineKeyboardButtonKind) -> Self - where - S: Into, - { - Self { - text: text.into(), - kind, - } - } - - pub fn text(mut self, val: S) -> Self - where - S: Into, - { - 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)] #[serde(rename_all = "snake_case")] pub enum InlineKeyboardButtonKind { @@ -113,16 +88,17 @@ pub enum InlineKeyboardButtonKind { Pay(True), } -/// 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 { + pub fn new(text: S, kind: InlineKeyboardButtonKind) -> Self + where + S: Into, + { + Self { + text: text.into(), + kind, + } + } + pub fn url(text: T, url: reqwest::Url) -> InlineKeyboardButton where T: Into, @@ -133,6 +109,16 @@ impl InlineKeyboardButton { } } + pub fn login(text: T, url: LoginUrl) -> InlineKeyboardButton + where + T: Into, + { + InlineKeyboardButton { + text: text.into(), + kind: InlineKeyboardButtonKind::LoginUrl(url), + } + } + pub fn callback(text: T, callback_data: C) -> InlineKeyboardButton where T: Into, @@ -144,6 +130,16 @@ impl InlineKeyboardButton { } } + pub fn web_app(text: T, info: WebAppInfo) -> InlineKeyboardButton + where + T: Into, + { + InlineKeyboardButton { + text: text.into(), + kind: InlineKeyboardButtonKind::WebApp(info), + } + } + pub fn switch_inline_query(text: T, switch_inline_query: Q) -> InlineKeyboardButton where T: Into, @@ -170,4 +166,37 @@ impl InlineKeyboardButton { ), } } + + pub fn callback_game(text: T, game: CallbackGame) -> InlineKeyboardButton + where + T: Into, + { + InlineKeyboardButton { + text: text.into(), + kind: InlineKeyboardButtonKind::CallbackGame(game), + } + } + + pub fn pay(text: T) -> InlineKeyboardButton + where + T: Into, + { + InlineKeyboardButton { + text: text.into(), + kind: InlineKeyboardButtonKind::Pay(True), + } + } + + pub fn text(mut self, val: S) -> Self + where + S: Into, + { + self.text = val.into(); + self + } + + pub fn kind(mut self, val: InlineKeyboardButtonKind) -> Self { + self.kind = val; + self + } } From adacbe90c27980aba023ea1a8ba6a1967a750ea7 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:10:37 +0400 Subject: [PATCH 3/7] Make code in `inline_keyboard_button.rs` nicer --- src/types/inline_keyboard_button.rs | 65 +++++++++++------------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/src/types/inline_keyboard_button.rs b/src/types/inline_keyboard_button.rs index 7c7cd018..fb36bbd2 100644 --- a/src/types/inline_keyboard_button.rs +++ b/src/types/inline_keyboard_button.rs @@ -99,92 +99,77 @@ impl InlineKeyboardButton { } } - pub fn url(text: T, url: reqwest::Url) -> InlineKeyboardButton + pub fn url(text: T, url: reqwest::Url) -> Self where T: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::Url(url), - } + Self::new(text, InlineKeyboardButtonKind::Url(url)) } - pub fn login(text: T, url: LoginUrl) -> InlineKeyboardButton + pub fn login(text: T, url: LoginUrl) -> Self where T: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::LoginUrl(url), - } + Self::new(text, InlineKeyboardButtonKind::LoginUrl(url)) } - pub fn callback(text: T, callback_data: C) -> InlineKeyboardButton + pub fn callback(text: T, callback_data: C) -> Self where T: Into, C: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::CallbackData(callback_data.into()), - } + Self::new( + text, + InlineKeyboardButtonKind::CallbackData(callback_data.into()), + ) } - pub fn web_app(text: T, info: WebAppInfo) -> InlineKeyboardButton + pub fn web_app(text: T, info: WebAppInfo) -> Self where T: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::WebApp(info), - } + Self::new(text, InlineKeyboardButtonKind::WebApp(info)) } - pub fn switch_inline_query(text: T, switch_inline_query: Q) -> InlineKeyboardButton + pub fn switch_inline_query(text: T, switch_inline_query: Q) -> Self where T: Into, Q: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query.into()), - } + Self::new( + text, + InlineKeyboardButtonKind::SwitchInlineQuery(switch_inline_query.into()), + ) } pub fn switch_inline_query_current_chat( text: T, switch_inline_query_current_chat: Q, - ) -> InlineKeyboardButton + ) -> Self where T: Into, Q: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat( + Self::new( + text, + InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat( switch_inline_query_current_chat.into(), ), - } + ) } - pub fn callback_game(text: T, game: CallbackGame) -> InlineKeyboardButton + pub fn callback_game(text: T, game: CallbackGame) -> Self where T: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::CallbackGame(game), - } + Self::new(text, InlineKeyboardButtonKind::CallbackGame(game)) } - pub fn pay(text: T) -> InlineKeyboardButton + pub fn pay(text: T) -> Self where T: Into, { - InlineKeyboardButton { - text: text.into(), - kind: InlineKeyboardButtonKind::Pay(True), - } + Self::new(text, InlineKeyboardButtonKind::Pay(True)) } pub fn text(mut self, val: S) -> Self From 0a8434ed066fbabef92be43899906b163f80175a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:11:17 +0400 Subject: [PATCH 4/7] Deprecate useless `InlineKeyboardButton::{text, kind}` --- src/types/inline_keyboard_button.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/types/inline_keyboard_button.rs b/src/types/inline_keyboard_button.rs index fb36bbd2..01f949d2 100644 --- a/src/types/inline_keyboard_button.rs +++ b/src/types/inline_keyboard_button.rs @@ -171,7 +171,13 @@ impl InlineKeyboardButton { { 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(mut self, val: S) -> Self where S: Into, @@ -180,6 +186,10 @@ impl InlineKeyboardButton { 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 From 9e2f5dc6ccf6aa5ffe6c50d7ed96a31f6408278d Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:15:12 +0400 Subject: [PATCH 5/7] Document `InlineKeyboardButton` constructors --- src/types/inline_keyboard_button.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/types/inline_keyboard_button.rs b/src/types/inline_keyboard_button.rs index 01f949d2..334f4545 100644 --- a/src/types/inline_keyboard_button.rs +++ b/src/types/inline_keyboard_button.rs @@ -74,6 +74,7 @@ pub enum InlineKeyboardButtonKind { /// button. /// /// ## Note + /// /// This type of button **must** always be the first button in the first /// row. CallbackGame(CallbackGame), @@ -81,6 +82,7 @@ pub enum InlineKeyboardButtonKind { /// Specify True, to send a [Pay button]. /// /// ## Note + /// /// This type of button **must** always be the first button in the first /// row. /// @@ -89,6 +91,7 @@ pub enum InlineKeyboardButtonKind { } impl InlineKeyboardButton { + /// Creates a new `InlineKeyboardButton`. pub fn new(text: S, kind: InlineKeyboardButtonKind) -> Self where S: Into, @@ -99,6 +102,9 @@ impl InlineKeyboardButton { } } + /// Constructor for `InlineKeyboardButton` with [`Url`] kind. + /// + /// [`Url`]: InlineKeyboardButtonKind::Url pub fn url(text: T, url: reqwest::Url) -> Self where T: Into, @@ -106,6 +112,9 @@ impl InlineKeyboardButton { Self::new(text, InlineKeyboardButtonKind::Url(url)) } + /// Constructor for `InlineKeyboardButton` with [`LoginUrl`] kind. + /// + /// [`LoginUrl`]: InlineKeyboardButtonKind::LoginUrl pub fn login(text: T, url: LoginUrl) -> Self where T: Into, @@ -113,6 +122,9 @@ impl InlineKeyboardButton { Self::new(text, InlineKeyboardButtonKind::LoginUrl(url)) } + /// Constructor for `InlineKeyboardButton` with [`CallbackData`] kind. + /// + /// [`CallbackData`]: InlineKeyboardButtonKind::CallbackData pub fn callback(text: T, callback_data: C) -> Self where T: Into, @@ -124,6 +136,9 @@ impl InlineKeyboardButton { ) } + /// Constructor for `InlineKeyboardButton` with [`WebApp`] kind. + /// + /// [`WebApp`]: InlineKeyboardButtonKind::WebApp pub fn web_app(text: T, info: WebAppInfo) -> Self where T: Into, @@ -131,6 +146,9 @@ impl InlineKeyboardButton { Self::new(text, InlineKeyboardButtonKind::WebApp(info)) } + /// Constructor for `InlineKeyboardButton` with [`SwitchInlineQuery`] kind. + /// + /// [`SwitchInlineQuery`]: InlineKeyboardButtonKind::SwitchInlineQuery pub fn switch_inline_query(text: T, switch_inline_query: Q) -> Self where T: Into, @@ -142,6 +160,10 @@ impl InlineKeyboardButton { ) } + /// Constructor for `InlineKeyboardButton` with + /// [`SwitchInlineQueryCurrentChat`] kind. + /// + /// [`SwitchInlineQueryCurrentChat`]: InlineKeyboardButtonKind::SwitchInlineQueryCurrentChat pub fn switch_inline_query_current_chat( text: T, switch_inline_query_current_chat: Q, @@ -158,6 +180,9 @@ impl InlineKeyboardButton { ) } + /// Constructor for `InlineKeyboardButton` with [`CallbackGame`] kind. + /// + /// [`CallbackGame`]: InlineKeyboardButtonKind::CallbackGame pub fn callback_game(text: T, game: CallbackGame) -> Self where T: Into, @@ -165,6 +190,9 @@ impl InlineKeyboardButton { Self::new(text, InlineKeyboardButtonKind::CallbackGame(game)) } + /// Constructor for `InlineKeyboardButton` with [`Pay`] kind. + /// + /// [`Pay`]: InlineKeyboardButtonKind::Pay pub fn pay(text: T) -> Self where T: Into, From 108caf80511f37895d33297c41cd1582a102a099 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:19:30 +0400 Subject: [PATCH 6/7] Fix link --- src/types/inline_keyboard_button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/inline_keyboard_button.rs b/src/types/inline_keyboard_button.rs index 334f4545..464f10d8 100644 --- a/src/types/inline_keyboard_button.rs +++ b/src/types/inline_keyboard_button.rs @@ -58,7 +58,7 @@ pub enum InlineKeyboardButtonKind { /// switched from, skipping the chat selection screen. /// /// [inline mode]: https://core.telegram.org/bots/inline - /// [switch_pm…]: https://core.telegram.org/bots/api#answerinlinequery + /// [switch_pm…]: crate::payloads::AnswerInlineQuery SwitchInlineQuery(String), /// If set, pressing the button will insert the bot‘s username and the From 0d787ce92a467b8ce8112472c722218fe44a244e Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 26 Jun 2022 20:24:13 +0400 Subject: [PATCH 7/7] Update changelog --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca38d080..75e0c53f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## 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 - `ChatPrivate::type_` field ([#232][pr232])