From 65435ec430bf8ad6de51a1764fa439d16a2eb2fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=8B=D1=80=D1=86=D0=B5=D0=B2=20=D0=92=D0=B0=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=20=D0=98=D0=B3=D0=BE=D1=80=D0=B5=D0=B2=D0=B8=D1=87?= Date: Wed, 31 Jul 2024 15:04:47 +0300 Subject: [PATCH] Add `RequestId` type --- crates/teloxide-core/CHANGELOG.md | 4 +++- crates/teloxide-core/src/types.rs | 6 +++-- crates/teloxide-core/src/types/chat_shared.rs | 4 ++-- .../src/types/keyboard_button.rs | 7 +++++- .../src/types/keyboard_button_request_chat.rs | 6 ++--- ...er.rs => keyboard_button_request_users.rs} | 14 +++++++---- crates/teloxide-core/src/types/message.rs | 5 +++- crates/teloxide-core/src/types/request_id.rs | 24 +++++++++++++++++++ .../teloxide-core/src/types/users_shared.rs | 4 ++-- 9 files changed, 58 insertions(+), 16 deletions(-) rename crates/teloxide-core/src/types/{keyboard_button_request_user.rs => keyboard_button_request_users.rs} (89%) create mode 100644 crates/teloxide-core/src/types/request_id.rs diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index 44a0fe54..4ee5206a 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -43,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `shared_chat` method to `Message` - Add `KeyboardButtonRequestUser` and `UserShared` types - Add `RequestUser` variant to `ButtonRequest` - - Add `UserShared` variant to `MessageKind` + - Add `UserShared` variant to `MessageKind` - Add `shared_user` method to `Message` - Support for TBA 6.6 ([#1040](pr1040)) - Add methods for working with bot's description: @@ -118,6 +118,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `giveaway`, `giveaway_created`, `giveaway_winners` and `giveaway_completed` getters to `Message` - Other Changes - Add fields `ChafFullInfo::{has_visible_history, accent_color_id, background_custom_emoji_id, profile_accent_color_id, profile_background_custom_emoji_id}` + - Add `RequestId` type [pr851]: https://github.com/teloxide/teloxide/pull/851 [pr887]: https://github.com/teloxide/teloxide/pull/887 @@ -232,6 +233,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Message::pinned_message` and `CallbackQuery::message` now have `MaybeInaccessibleMessage` type - Field `emoji_status_custom_emoji_id` is allowed in non-private chats (moved to the `ChatFullInfo`) - Struct `Forward` was replaced by `MessageOrigin` in `MessageCommon` + - `RequestId` replaces `i32` in `ChatShared` and `KeyboardButtonRequestChat` structs [pr852]: https://github.com/teloxide/teloxide/pull/853 diff --git a/crates/teloxide-core/src/types.rs b/crates/teloxide-core/src/types.rs index 2d0af202..3d222acf 100644 --- a/crates/teloxide-core/src/types.rs +++ b/crates/teloxide-core/src/types.rs @@ -84,7 +84,7 @@ pub use invoice::*; pub use keyboard_button::*; pub use keyboard_button_poll_type::*; pub use keyboard_button_request_chat::*; -pub use keyboard_button_request_user::*; +pub use keyboard_button_request_users::*; pub use label_price::*; pub use link_preview_options::*; pub use location::*; @@ -116,6 +116,7 @@ pub use reply_keyboard_markup::*; pub use reply_keyboard_remove::*; pub use reply_markup::*; pub use reply_parameters::*; +pub use request_id::*; pub use response_parameters::*; pub use sent_web_app_message::*; pub use shipping_address::*; @@ -209,7 +210,7 @@ mod invoice; mod keyboard_button; mod keyboard_button_poll_type; mod keyboard_button_request_chat; -mod keyboard_button_request_user; +mod keyboard_button_request_users; mod label_price; mod link_preview_options; mod location; @@ -238,6 +239,7 @@ mod reply_keyboard_markup; mod reply_keyboard_remove; mod reply_markup; mod reply_parameters; +mod request_id; mod response_parameters; mod sent_web_app_message; mod shipping_address; diff --git a/crates/teloxide-core/src/types/chat_shared.rs b/crates/teloxide-core/src/types/chat_shared.rs index 4c75b94e..f23c94bb 100644 --- a/crates/teloxide-core/src/types/chat_shared.rs +++ b/crates/teloxide-core/src/types/chat_shared.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::ChatId; +use crate::types::{ChatId, RequestId}; /// Information about the chat whose identifier was shared with the bot using a /// [`KeyboardButtonRequestChat`] button. @@ -9,7 +9,7 @@ use crate::types::ChatId; #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct ChatShared { /// Identifier of the request. - pub request_id: i32, + pub request_id: RequestId, /// Identifier of the shared chat. pub chat_id: ChatId, } diff --git a/crates/teloxide-core/src/types/keyboard_button.rs b/crates/teloxide-core/src/types/keyboard_button.rs index 60470977..56893d12 100644 --- a/crates/teloxide-core/src/types/keyboard_button.rs +++ b/crates/teloxide-core/src/types/keyboard_button.rs @@ -198,6 +198,8 @@ impl Serialize for ButtonRequest { #[cfg(test)] mod tests { + use crate::types::RequestId; + use super::*; #[test] @@ -221,7 +223,10 @@ mod tests { fn serialize_chat_request() { let button = KeyboardButton { text: String::from(""), - request: Some(ButtonRequest::RequestChat(KeyboardButtonRequestChat::new(0, false))), + request: Some(ButtonRequest::RequestChat(KeyboardButtonRequestChat::new( + RequestId(0), + false, + ))), }; let expected = r#"{"text":"","request_chat":{"request_id":0,"chat_is_channel":false}}"#; let actual = serde_json::to_string(&button).unwrap(); diff --git a/crates/teloxide-core/src/types/keyboard_button_request_chat.rs b/crates/teloxide-core/src/types/keyboard_button_request_chat.rs index fef4222b..2765aa69 100644 --- a/crates/teloxide-core/src/types/keyboard_button_request_chat.rs +++ b/crates/teloxide-core/src/types/keyboard_button_request_chat.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::ChatAdministratorRights; +use crate::types::{ChatAdministratorRights, RequestId}; /// This object defines the criteria used to request a suitable chat. The /// identifier of the selected chat will be shared with the bot when the @@ -14,7 +14,7 @@ pub struct KeyboardButtonRequestChat { /// [`ChatShared`] object. Must be unique within the message. /// /// [`ChatShared`]: crate::types::ChatShared - pub request_id: i32, + pub request_id: RequestId, /// Pass `true` to request a channel chat, pass `false` to request a group /// or a supergroup chat. @@ -57,7 +57,7 @@ pub struct KeyboardButtonRequestChat { impl KeyboardButtonRequestChat { /// Creates a new [`KeyboardButtonRequestChat`]. - pub fn new(request_id: i32, chat_is_channel: bool) -> Self { + pub fn new(request_id: RequestId, chat_is_channel: bool) -> Self { Self { request_id, chat_is_channel, diff --git a/crates/teloxide-core/src/types/keyboard_button_request_user.rs b/crates/teloxide-core/src/types/keyboard_button_request_users.rs similarity index 89% rename from crates/teloxide-core/src/types/keyboard_button_request_user.rs rename to crates/teloxide-core/src/types/keyboard_button_request_users.rs index 9636cf16..6f126622 100644 --- a/crates/teloxide-core/src/types/keyboard_button_request_user.rs +++ b/crates/teloxide-core/src/types/keyboard_button_request_users.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; +use crate::types::RequestId; + /// This object defines the criteria used to request a suitable users. The /// identifiers of the selected users will be shared with the bot when the /// corresponding button is pressed. More about requesting users ยป @@ -12,7 +14,7 @@ pub struct KeyboardButtonRequestUsers { /// [`UsersShared`] object. Must be unique within the message. /// /// [`UsersShared`]: crate::types::UsersShared - pub request_id: i32, + pub request_id: RequestId, /// Pass `true` to request a bot, pass `false` to request a regular user. If /// not specified, no additional restrictions are applied. @@ -26,13 +28,13 @@ pub struct KeyboardButtonRequestUsers { pub user_is_premium: Option, /// The maximum number of users to be selected; 1-10. Defaults to 1. - #[serde(default = "de_max_quantity_default")] + #[serde(default = "one", skip_serializing_if = "is_one")] pub max_quantity: u8, } impl KeyboardButtonRequestUsers { /// Creates a new [`KeyboardButtonRequestUsers`]. - pub fn new(request_id: i32) -> Self { + pub fn new(request_id: RequestId) -> Self { Self { request_id, user_is_bot: None, user_is_premium: None, max_quantity: 1 } } @@ -57,6 +59,10 @@ impl KeyboardButtonRequestUsers { } } -fn de_max_quantity_default() -> u8 { +fn one() -> u8 { 1 } + +fn is_one(value: &u8) -> bool { + *value == 1 +} diff --git a/crates/teloxide-core/src/types/message.rs b/crates/teloxide-core/src/types/message.rs index 93440682..750700e8 100644 --- a/crates/teloxide-core/src/types/message.rs +++ b/crates/teloxide-core/src/types/message.rs @@ -1865,7 +1865,10 @@ mod tests { chat_full_info: ChatFullInfo::default() }, kind: MessageKind::ChatShared(MessageChatShared { - chat_shared: ChatShared { request_id: 348349, chat_id: ChatId(384939) } + chat_shared: ChatShared { + request_id: RequestId(348349), + chat_id: ChatId(384939) + } }), via_bot: None } diff --git a/crates/teloxide-core/src/types/request_id.rs b/crates/teloxide-core/src/types/request_id.rs new file mode 100644 index 00000000..98cd87f3 --- /dev/null +++ b/crates/teloxide-core/src/types/request_id.rs @@ -0,0 +1,24 @@ +use derive_more::Display; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Copy, Display, PartialEq, Eq, Hash, Deserialize, Serialize)] +#[serde(transparent)] +pub struct RequestId(pub i32); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_request_id_de() { + #[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)] + struct Dummy { + request_id: RequestId, + } + let json = r#"{"request_id":42}"#; + let dummy = Dummy { request_id: RequestId(42) }; + + assert_eq!(serde_json::to_string(&dummy).unwrap(), json); + assert_eq!(dummy, serde_json::from_str(json).unwrap()); + } +} diff --git a/crates/teloxide-core/src/types/users_shared.rs b/crates/teloxide-core/src/types/users_shared.rs index 46b20679..d522fa54 100644 --- a/crates/teloxide-core/src/types/users_shared.rs +++ b/crates/teloxide-core/src/types/users_shared.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::types::UserId; +use crate::types::{RequestId, UserId}; /// This object contains information about the users whose identifiers were /// shared with the bot using a [KeyboardButtonRequestUsers] button. @@ -9,7 +9,7 @@ use crate::types::UserId; #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct UsersShared { /// Identifier of the request - pub request_id: i32, + pub request_id: RequestId, /// Identifiers of the shared users pub user_ids: Vec, }