Skip to content

Commit

Permalink
Fix parsing ChatMember
Browse files Browse the repository at this point in the history
  • Loading branch information
Desiders committed Dec 5, 2023
1 parent a20641a commit 8cec923
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
16 changes: 8 additions & 8 deletions src/enums/chat_member_status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum_macros::{AsRefStr, Display, EnumString, IntoStaticStr};

use crate::types::{ChatMember, ChatMemberKind};
use crate::types::ChatMember;

/// This enum represents all possible types of the chat member status
/// # Documentation
Expand Down Expand Up @@ -57,13 +57,13 @@ impl<'a> PartialEq<&'a str> for ChatMemberStatus {

impl<'a> From<&'a ChatMember> for ChatMemberStatus {
fn from(chat_member: &'a ChatMember) -> Self {
match chat_member.kind() {
ChatMemberKind::Owner(_) => Self::Creator,
ChatMemberKind::Administrator(_) => Self::Administrator,
ChatMemberKind::Member(_) => Self::Member,
ChatMemberKind::Restricted(_) => Self::Restricted,
ChatMemberKind::Left(_) => Self::Left,
ChatMemberKind::Banned(_) => Self::Kicked,
match chat_member {
ChatMember::Owner(_) => Self::Creator,
ChatMember::Administrator(_) => Self::Administrator,
ChatMember::Member(_) => Self::Member,
ChatMember::Restricted(_) => Self::Restricted,
ChatMember::Left(_) => Self::Left,
ChatMember::Banned(_) => Self::Kicked,
}
}
}
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub use chat_id_kind::ChatIdKind;
pub use chat_invite_link::ChatInviteLink;
pub use chat_join_request::ChatJoinRequest;
pub use chat_location::ChatLocation;
pub use chat_member::{ChatMember, Kind as ChatMemberKind};
pub use chat_member::ChatMember;
pub use chat_member_administrator::ChatMemberAdministrator;
pub use chat_member_banned::ChatMemberBanned;
pub use chat_member_left::ChatMemberLeft;
Expand Down
32 changes: 8 additions & 24 deletions src/types/chat_member.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
use super::{
ChatMemberAdministrator, ChatMemberBanned, ChatMemberLeft, ChatMemberMember, ChatMemberOwner,
ChatMemberRestricted, User,
ChatMemberRestricted,
};

use serde::Deserialize;
use strum_macros::Display;

#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize)]
pub struct ChatMember {
/// Information about the user.
pub user: User,

#[serde(flatten)]
pub kind: Kind,
}

/// This object contains information about one member of a chat. Currently, the following 6 types of chat members are supported:
/// - [`ChatMemberOwner`]
/// - [`ChatMemberAdministrator`]
Expand All @@ -26,7 +17,7 @@ pub struct ChatMember {
/// <https://core.telegram.org/bots/api#chatmember>
#[derive(Debug, Display, Clone, Hash, PartialEq, Eq, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum Kind {
pub enum ChatMember {
#[serde(rename = "creator")]
Owner(ChatMemberOwner),
Administrator(ChatMemberAdministrator),
Expand All @@ -37,45 +28,38 @@ pub enum Kind {
Banned(ChatMemberBanned),
}

impl From<ChatMemberOwner> for Kind {
impl From<ChatMemberOwner> for ChatMember {
fn from(chat_member: ChatMemberOwner) -> Self {
Self::Owner(chat_member)
}
}

impl From<ChatMemberAdministrator> for Kind {
impl From<ChatMemberAdministrator> for ChatMember {
fn from(chat_member: ChatMemberAdministrator) -> Self {
Self::Administrator(chat_member)
}
}

impl From<ChatMemberMember> for Kind {
impl From<ChatMemberMember> for ChatMember {
fn from(chat_member: ChatMemberMember) -> Self {
Self::Member(chat_member)
}
}

impl From<ChatMemberRestricted> for Kind {
impl From<ChatMemberRestricted> for ChatMember {
fn from(chat_member: ChatMemberRestricted) -> Self {
Self::Restricted(chat_member)
}
}

impl From<ChatMemberLeft> for Kind {
impl From<ChatMemberLeft> for ChatMember {
fn from(chat_member: ChatMemberLeft) -> Self {
Self::Left(chat_member)
}
}

impl From<ChatMemberBanned> for Kind {
impl From<ChatMemberBanned> for ChatMember {
fn from(chat_member: ChatMemberBanned) -> Self {
Self::Banned(chat_member)
}
}

impl ChatMember {
#[must_use]
pub const fn kind(&self) -> &Kind {
&self.kind
}
}

0 comments on commit 8cec923

Please sign in to comment.