telegram_bot_fork_raw/types/
chat.rs

1use serde::de::{Deserialize, Deserializer, Error};
2
3use types::*;
4
5/// This object represents a Telegram user or bot.
6#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
7pub struct User {
8    /// Unique identifier for this user or bot.
9    pub id: UserId,
10    /// User‘s or bot’s first name.
11    pub first_name: String,
12    /// User‘s or bot’s last name.
13    pub last_name: Option<String>,
14    /// User‘s or bot’s username.
15    pub username: Option<String>,
16    /// IETF language tag of the user's language
17    pub language_code: Option<String>,
18    /// True, if this user is a bot.
19    pub is_bot: bool,
20}
21
22/// This object represents a group.
23#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
24pub struct Group {
25    /// Unique identifier for this chat.
26    pub id: GroupId,
27    /// Title, for supergroups, channels and group chats.
28    pub title: String,
29    /// True if a group has ‘All Members Are Admins’ enabled.
30    pub all_members_are_administrators: bool,
31}
32
33/// This object represents a supergroup.
34#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
35pub struct Supergroup {
36    /// Unique identifier for this chat.
37    pub id: SupergroupId,
38    /// Title, for supergroups, channels and group chats.
39    pub title: String,
40    /// Username for supergroup.
41    pub username: Option<String>,
42}
43
44/// This object represents a channel.
45#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
46pub struct Channel {
47    /// Unique identifier for this chat.
48    pub id: ChannelId,
49    /// Title, for supergroups, channels and group chats.
50    pub title: String,
51    /// Username for channel.
52    pub username: Option<String>,
53}
54
55/// This object represents a private, group or supergroup.
56#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
57pub enum MessageChat {
58    Private(User),
59    Group(Group),
60    Supergroup(Supergroup),
61    #[doc(hidden)]
62    Unknown(RawChat),
63}
64
65impl MessageChat {
66    pub fn id(&self) -> ChatId {
67        match *self {
68            MessageChat::Private(ref x) => x.id.into(),
69            MessageChat::Group(ref x) => x.id.into(),
70            MessageChat::Supergroup(ref x) => x.id.into(),
71            MessageChat::Unknown(ref x) => x.id.into(),
72        }
73    }
74}
75
76/// This object represents a chat.
77#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
78pub enum Chat {
79    Private(User),
80    Group(Group),
81    Supergroup(Supergroup),
82    Channel(Channel),
83    #[doc(hidden)]
84    Unknown(RawChat),
85}
86
87impl Chat {
88    pub fn id(&self) -> ChatId {
89        match *self {
90            Chat::Private(ref x) => x.id.into(),
91            Chat::Group(ref x) => x.id.into(),
92            Chat::Supergroup(ref x) => x.id.into(),
93            Chat::Channel(ref x) => x.id.into(),
94            Chat::Unknown(ref x) => x.id.into(),
95        }
96    }
97}
98
99impl<'de> Deserialize<'de> for Chat {
100    fn deserialize<D>(deserializer: D) -> Result<Chat, D::Error>
101    where
102        D: Deserializer<'de>,
103    {
104        let raw: RawChat = Deserialize::deserialize(deserializer)?;
105
106        macro_rules! required_field {
107            ($name:ident) => {{
108                match raw.$name {
109                    Some(val) => val,
110                    None => return Err(D::Error::missing_field(stringify!($name))),
111                }
112            }};
113        }
114
115        Ok(match raw.type_.as_ref() {
116            "private" => Chat::Private(User {
117                id: raw.id.into(),
118                username: raw.username,
119                first_name: required_field!(first_name),
120                last_name: raw.last_name,
121                language_code: raw.language_code,
122                is_bot: false,
123            }),
124            "group" => Chat::Group(Group {
125                id: raw.id.into(),
126                title: required_field!(title),
127                all_members_are_administrators: required_field!(all_members_are_administrators),
128            }),
129            "supergroup" => Chat::Supergroup(Supergroup {
130                id: raw.id.into(),
131                title: required_field!(title),
132                username: raw.username,
133            }),
134            "channel" => Chat::Channel(Channel {
135                id: raw.id.into(),
136                title: required_field!(title),
137                username: raw.username,
138            }),
139            _ => Chat::Unknown(raw),
140        })
141    }
142}
143
144/// This object represents a chat, directly mapped.
145#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
146pub struct RawChat {
147    /// Unique identifier for this chat.
148    pub id: Integer,
149    /// Type of chat, can be either “private”, “group”, “supergroup” or “channel”
150    #[serde(rename = "type")]
151    pub type_: String,
152    /// Title, for supergroups, channels and group chats
153    pub title: Option<String>,
154    /// Username, for private chats, supergroups and channels if available
155    pub username: Option<String>,
156    /// First name of the other party in a private chat
157    pub first_name: Option<String>,
158    /// Last name of the other party in a private chat
159    pub last_name: Option<String>,
160    /// IETF language tag of the other party in a private chat
161    pub language_code: Option<String>,
162    /// True if a group has ‘All Members Are Admins’ enabled.
163    pub all_members_are_administrators: Option<bool>,
164}