Replace useless structs ChatBoostSource and ReactionType with enums

This commit is contained in:
Andrey Brusnik 2024-07-30 17:07:21 +04:00
parent ab069009b5
commit 62000c8204
No known key found for this signature in database
GPG key ID: D33232F28CFF442C
5 changed files with 50 additions and 78 deletions

View file

@ -90,7 +90,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `from_request` and `from_attachment_menu` fields to `WriteAccessAllowed`
- Support for TBA 7.0 ([#1101](pr1101))
- Reactions:
- Add `ReactionType`, `MessageReactionUpdated` and `MessageReactionCountUpdated` structs
- Add `ReactionType` enum
- Add `MessageReactionUpdated` and `MessageReactionCountUpdated` structs
- Add `MessageReaction` and `MessageReactionCount` variants to `UpdateKind` enum
- Add `filter_message_reaction_updated` and `filter_message_reaction_count_updated` filters to `UpdateFilterExt` trait
- Add `set_message_reaction` TBA method to `Requester` trait
@ -106,7 +107,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Multiple Message Actions
- Add TBA methods `delete_messages`, `forward_messages` and `copy_messages` to `Requester` trait
- Chat Boost
- Add `ChatBoost`, `ChatBoostSource`, `ChatBoostUpdated`, `ChatBoostRemoved` and `UserChatBoosts` structs
- Add `ChatBoostSource` enum
- Add `ChatBoost`, `ChatBoostUpdated`, `ChatBoostRemoved` and `UserChatBoosts` structs
- Add `ChatBoost` and `RemovedChatBoost` variants to `UpdateKind` enum
- Add `filter_chat_boost` and `filter_removed_chat_boost` filters to `UpdateFilterExt` trait
- Add `get_user_chat_boosts` TBA method to `Requester` trait

View file

@ -609,9 +609,7 @@ mod tests {
has_protected_content: None,
}),
photo: None,
available_reactions: Some(vec![ReactionType {
kind: ReactionTypeKind::Emoji { emoji: "🌭".to_owned() },
}]),
available_reactions: Some(vec![ReactionType::Emoji { emoji: "🌭".to_owned() }]),
pinned_message: None,
message_auto_delete_time: None,
has_hidden_members: false,
@ -649,9 +647,7 @@ mod tests {
has_restricted_voice_and_video_messages: None,
}),
photo: None,
available_reactions: Some(vec![ReactionType {
kind: ReactionTypeKind::Emoji { emoji: "🌭".to_owned() },
}]),
available_reactions: Some(vec![ReactionType::Emoji { emoji: "🌭".to_owned() }]),
pinned_message: None,
message_auto_delete_time: None,
has_hidden_members: false,

View file

@ -3,18 +3,11 @@ use serde::{Deserialize, Serialize};
use crate::types::{MessageId, User};
/// This object describes the source of a chat boost.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChatBoostSource {
#[serde(flatten)]
pub kind: ChatBoostSourceKind,
}
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "source")]
pub enum ChatBoostSourceKind {
pub enum ChatBoostSource {
Premium(ChatBoostSourcePremium),
GiftCode(ChatBoostSourceGiftCode),
Giveaway(ChatBoostSourceGiveaway),
@ -62,10 +55,10 @@ pub struct ChatBoostSourceGiveaway {
impl ChatBoostSource {
#[must_use]
pub fn user(&self) -> Option<&User> {
Some(match &self.kind {
ChatBoostSourceKind::Premium(premium) => &premium.user,
ChatBoostSourceKind::GiftCode(gift_code) => &gift_code.user,
ChatBoostSourceKind::Giveaway(giveaway) => return giveaway.user.as_ref(),
Some(match &self {
Self::Premium(premium) => &premium.user,
Self::GiftCode(gift_code) => &gift_code.user,
Self::Giveaway(giveaway) => return giveaway.user.as_ref(),
})
}
}

View file

@ -1,19 +1,10 @@
use serde::{Deserialize, Serialize};
/// The reaction type is based on an emoji.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct ReactionType {
/// Kind of this reaction type - emoji or custom emoji.
#[serde(flatten)]
pub kind: ReactionTypeKind,
}
/// Kind of a [`ReactionType`] - emoji or custom emoji.
/// The reaction type is based on an emoji or custom emoji.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum ReactionTypeKind {
pub enum ReactionType {
/// "emoji" or "custom_emoji" reaction
Emoji {
/// Reaction emoji. Currently, it can be one of "👍", "👎", "❤", "🔥",
@ -36,16 +27,16 @@ pub enum ReactionTypeKind {
impl ReactionType {
#[must_use]
pub fn emoji(&self) -> Option<&String> {
match &self.kind {
ReactionTypeKind::Emoji { emoji } => Some(emoji),
match &self {
Self::Emoji { emoji } => Some(emoji),
_ => None,
}
}
#[must_use]
pub fn custom_emoji_id(&self) -> Option<&String> {
match &self.kind {
ReactionTypeKind::CustomEmoji { custom_emoji_id } => Some(custom_emoji_id),
match &self {
Self::CustomEmoji { custom_emoji_id } => Some(custom_emoji_id),
_ => None,
}
}

View file

@ -467,12 +467,12 @@ fn empty_error() -> UpdateKind {
#[cfg(test)]
mod test {
use crate::types::{
Chat, ChatBoost, ChatBoostRemoved, ChatBoostSource, ChatBoostSourceKind,
ChatBoostSourcePremium, ChatBoostUpdated, ChatFullInfo, ChatId, ChatKind, ChatPrivate,
ChatPublic, LinkPreviewOptions, MediaKind, MediaText, Message, MessageCommon, MessageId,
MessageKind, MessageReactionCountUpdated, MessageReactionUpdated, PublicChatChannel,
PublicChatKind, PublicChatSupergroup, ReactionCount, ReactionType, ReactionTypeKind,
Update, UpdateId, UpdateKind, User, UserId,
Chat, ChatBoost, ChatBoostRemoved, ChatBoostSource, ChatBoostSourcePremium,
ChatBoostUpdated, ChatFullInfo, ChatId, ChatKind, ChatPrivate, ChatPublic,
LinkPreviewOptions, MediaKind, MediaText, Message, MessageCommon, MessageId, MessageKind,
MessageReactionCountUpdated, MessageReactionUpdated, PublicChatChannel, PublicChatKind,
PublicChatSupergroup, ReactionCount, ReactionType, Update, UpdateId, UpdateKind, User,
UserId,
};
use chrono::DateTime;
@ -901,9 +901,7 @@ mod test {
actor_chat: None,
date: DateTime::from_timestamp(1721306082, 0).unwrap(),
old_reaction: vec![],
new_reaction: vec![ReactionType {
kind: ReactionTypeKind::Emoji { emoji: "🌭".to_owned() },
}],
new_reaction: vec![ReactionType::Emoji { emoji: "🌭".to_owned() }],
}),
};
@ -971,15 +969,11 @@ mod test {
date: DateTime::from_timestamp(1721306391, 0).unwrap(),
reactions: vec![
ReactionCount {
r#type: ReactionType {
kind: ReactionTypeKind::Emoji { emoji: "🗿".to_owned() },
},
r#type: ReactionType::Emoji { emoji: "🗿".to_owned() },
total_count: 2,
},
ReactionCount {
r#type: ReactionType {
kind: ReactionTypeKind::Emoji { emoji: "🌭".to_owned() },
},
r#type: ReactionType::Emoji { emoji: "🌭".to_owned() },
total_count: 1,
},
],
@ -1048,8 +1042,7 @@ mod test {
boost_id: "4506e1b7e866e33fcbde78fe1746ec3a".to_owned(),
add_date: DateTime::from_timestamp(1721399621, 0).unwrap(),
expiration_date: DateTime::from_timestamp(1745088963, 0).unwrap(),
source: ChatBoostSource {
kind: ChatBoostSourceKind::Premium(ChatBoostSourcePremium {
source: ChatBoostSource::Premium(ChatBoostSourcePremium {
user: User {
id: UserId(1459074222),
is_bot: false,
@ -1062,7 +1055,6 @@ mod test {
},
}),
},
},
}),
};
@ -1123,8 +1115,7 @@ mod test {
},
boost_id: "4506e1b7e866e33fcbde78fe1746ec3a".to_owned(),
remove_date: DateTime::from_timestamp(1721999621, 0).unwrap(),
source: ChatBoostSource {
kind: ChatBoostSourceKind::Premium(ChatBoostSourcePremium {
source: ChatBoostSource::Premium(ChatBoostSourcePremium {
user: User {
id: UserId(1459074222),
is_bot: false,
@ -1136,7 +1127,6 @@ mod test {
added_to_attachment_menu: false,
},
}),
},
}),
};