Add RequestId type

This commit is contained in:
Сырцев Вадим Игоревич 2024-07-31 15:04:47 +03:00 committed by Andrey Brusnik
parent 3d4e25743e
commit 65435ec430
No known key found for this signature in database
GPG key ID: D33232F28CFF442C
9 changed files with 58 additions and 16 deletions

View file

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

View file

@ -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;

View file

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

View file

@ -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();

View file

@ -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,

View file

@ -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<bool>,
/// 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
}

View file

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

View file

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

View file

@ -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<UserId>,
}