telegram_bot_fork_raw/types/
chat.rs1use serde::de::{Deserialize, Deserializer, Error};
2
3use types::*;
4
5#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
7pub struct User {
8 pub id: UserId,
10 pub first_name: String,
12 pub last_name: Option<String>,
14 pub username: Option<String>,
16 pub language_code: Option<String>,
18 pub is_bot: bool,
20}
21
22#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
24pub struct Group {
25 pub id: GroupId,
27 pub title: String,
29 pub all_members_are_administrators: bool,
31}
32
33#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
35pub struct Supergroup {
36 pub id: SupergroupId,
38 pub title: String,
40 pub username: Option<String>,
42}
43
44#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
46pub struct Channel {
47 pub id: ChannelId,
49 pub title: String,
51 pub username: Option<String>,
53}
54
55#[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#[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#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
146pub struct RawChat {
147 pub id: Integer,
149 #[serde(rename = "type")]
151 pub type_: String,
152 pub title: Option<String>,
154 pub username: Option<String>,
156 pub first_name: Option<String>,
158 pub last_name: Option<String>,
160 pub language_code: Option<String>,
162 pub all_members_are_administrators: Option<bool>,
164}