1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use crate::types::{keyboard::inline, Chat, User};
use serde::de::IgnoredAny;
pub mod forward;
mod from;
mod id;
mod kind;
pub mod text;
mod timer;
pub use {
forward::Forward, from::From, id::Id, kind::Kind, text::Text,
timer::AutoDeleteTimerChanged,
};
#[derive(Debug, PartialEq, Clone)]
#[non_exhaustive]
pub struct Message {
pub id: Id,
pub from: Option<From>,
pub date: i64,
pub chat: Chat,
pub forward: Option<Forward>,
pub reply_to: Option<Box<Message>>,
pub edit_date: Option<i64>,
pub author_signature: Option<String>,
pub reply_markup: Option<inline::Keyboard>,
pub kind: Kind,
pub via_bot: Option<User>,
}
pub(crate) struct Data {
pub id: Id,
pub from: Option<From>,
pub date: i64,
pub chat: Chat,
pub forward: Option<Forward>,
pub reply_to: Option<Message>,
pub edit_date: Option<i64>,
pub author_signature: Option<String>,
pub reply_markup: Option<inline::Keyboard>,
pub via_bot: Option<User>,
}
impl Message {
pub(crate) fn new(data: Data, kind: Kind) -> Self {
Self {
id: data.id,
from: data.from,
date: data.date,
chat: data.chat,
forward: data.forward,
reply_to: data.reply_to.map(Box::new),
edit_date: data.edit_date,
author_signature: data.author_signature,
reply_markup: data.reply_markup,
via_bot: data.via_bot,
kind,
}
}
pub(crate) fn split(self) -> (Data, Kind) {
let data = Data {
id: self.id,
from: self.from,
date: self.date,
chat: self.chat,
forward: self.forward,
reply_to: self.reply_to.map(|message| *message),
edit_date: self.edit_date,
author_signature: self.author_signature,
reply_markup: self.reply_markup,
via_bot: self.via_bot,
};
(data, self.kind)
}
}
const MESSAGE_ID: &str = "message_id";
const FROM: &str = "from";
const SENDER_CHAT: &str = "sender_chat";
const DATE: &str = "date";
const CHAT: &str = "chat";
const FORWARD_FROM: &str = "forward_from";
const FORWARD_FROM_CHAT: &str = "forward_from_chat";
const FORWARD_FROM_MESSAGE_ID: &str = "forward_from_message_id";
const FORWARD_SIGNATURE: &str = "forward_signature";
const FORWARD_SENDER_NAME: &str = "forward_sender_name";
const FORWARD_DATE: &str = "forward_date";
const REPLY_TO_MESSAGE: &str = "reply_to_message";
const EDIT_DATE: &str = "edit_date";
const MEDIA_GROUP_ID: &str = "media_group_id";
const AUTHOR_SIGNATURE: &str = "author_signature";
const TEXT: &str = "text";
const ENTITIES: &str = "entities";
const CAPTION_ENTITIES: &str = "caption_entities";
const AUDIO: &str = "audio";
const DICE: &str = "dice";
const DOCUMENT: &str = "document";
const ANIMATION: &str = "animation";
const GAME: &str = "game";
const PHOTO: &str = "photo";
const STICKER: &str = "sticker";
const VIDEO: &str = "video";
const VOICE: &str = "voice";
const VIDEO_NOTE: &str = "video_note";
const CAPTION: &str = "caption";
const CONTACT: &str = "contact";
const LOCATION: &str = "location";
const VENUE: &str = "venue";
const POLL: &str = "poll";
const NEW_CHAT_MEMBERS: &str = "new_chat_members";
const LEFT_CHAT_MEMBER: &str = "left_chat_member";
const NEW_CHAT_TITLE: &str = "new_chat_title";
const NEW_CHAT_PHOTO: &str = "new_chat_photo";
const DELETE_CHAT_PHOTO: &str = "delete_chat_photo";
const GROUP_CHAT_CREATED: &str = "group_chat_created";
const SUPERGROUP_CHAT_CREATED: &str = "supergroup_chat_created";
const CHANNEL_CHAT_CREATED: &str = "channel_chat_created";
const AUTO_DELETE_TIMER_CHANGED: &str = "message_auto_delete_timer_changed";
const MIGRATE_TO_CHAT_ID: &str = "migrate_to_chat_id";
const MIGRATE_FROM_CHAT_ID: &str = "migrate_from_chat_id";
const PINNED_MESSAGE: &str = "pinned_message";
const INVOICE: &str = "invoice";
const SUCCESSFUL_PAYMENT: &str = "successful_payment";
const CONNECTED_WEBSITE: &str = "connected_website";
const PASSPORT_DATA: &str = "passport_data";
const PROXIMITY_ALERT_TRIGGERED: &str = "proximity_alert_triggered";
const VOICE_CHAT_STARTED: &str = "voice_chat_started";
const VOICE_CHAT_PARTICIPANTS_INVITED: &str = "voice_chat_participants_invited";
const VOICE_CHAT_ENDED: &str = "voice_chat_ended";
const VOICE_CHAT_SCHEDULED: &str = "voice_chat_scheduled";
const REPLY_MARKUP: &str = "reply_markup";
const VIA_BOT: &str = "via_bot";
struct MessageVisitor;
impl<'v> serde::de::Visitor<'v> for MessageVisitor {
type Value = Message;
fn expecting(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "struct Message")
}
#[allow(clippy::cognitive_complexity)] #[allow(clippy::too_many_lines)]
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
where
V: serde::de::MapAccess<'v>,
{
let mut message_id = None;
let mut from = None;
let mut sender_chat = None;
let mut date = None;
let mut chat = None;
let mut forward_from = None;
let mut forward_from_chat = None;
let mut forward_from_message_id = None;
let mut forward_signature = None;
let mut forward_sender_name = None;
let mut forward_date = None;
let mut reply_to_message = None;
let mut edit_date = None;
let mut media_group_id = None;
let mut author_signature = None;
let mut text = None;
let mut entities = None;
let mut caption_entities = None;
let mut audio = None;
let mut dice = None;
let mut document = None;
let mut animation = None;
let mut game = None;
let mut photo = None;
let mut sticker = None;
let mut video = None;
let mut voice = None;
let mut video_note = None;
let mut caption = None;
let mut contact = None;
let mut location = None;
let mut venue = None;
let mut poll = None;
let mut new_chat_members = None;
let mut left_chat_member = None;
let mut new_chat_title = None;
let mut new_chat_photo = None;
let mut delete_chat_photo = false;
let mut group_chat_created = false;
let mut supergroup_chat_created = false;
let mut channel_chat_created = false;
let mut auto_delete_timer_changed = None;
let mut migrate_to_chat_id = None;
let mut migrate_from_chat_id = None;
let mut pinned_message = None;
let mut invoice = None;
let mut successful_payment = None;
let mut connected_website = None;
let mut passport_data = None;
let mut proximity_alert = None;
let mut voice_chat_started = false;
let mut voice_chat_participants_invited = None;
let mut voice_chat_ended = None;
let mut voice_chat_scheduled = None;
let mut reply_markup = None;
let mut via_bot = None;
while let Some(key) = map.next_key()? {
match key {
MESSAGE_ID => message_id = Some(map.next_value()?),
FROM => from = Some(map.next_value()?),
SENDER_CHAT => sender_chat = Some(map.next_value()?),
DATE => date = Some(map.next_value()?),
CHAT => chat = Some(map.next_value()?),
FORWARD_FROM => forward_from = Some(map.next_value()?),
FORWARD_FROM_CHAT => {
forward_from_chat = Some(map.next_value()?);
}
FORWARD_FROM_MESSAGE_ID => {
forward_from_message_id = Some(map.next_value()?);
}
FORWARD_SIGNATURE => {
forward_signature = Some(map.next_value()?);
}
FORWARD_SENDER_NAME => {
forward_sender_name = Some(map.next_value()?);
}
FORWARD_DATE => forward_date = Some(map.next_value()?),
REPLY_TO_MESSAGE => reply_to_message = Some(map.next_value()?),
EDIT_DATE => edit_date = Some(map.next_value()?),
MEDIA_GROUP_ID => media_group_id = Some(map.next_value()?),
AUTHOR_SIGNATURE => author_signature = Some(map.next_value()?),
TEXT => text = Some(map.next_value()?),
ENTITIES => entities = Some(map.next_value()?),
CAPTION_ENTITIES => caption_entities = Some(map.next_value()?),
AUDIO => audio = Some(map.next_value()?),
DICE => dice = Some(map.next_value()?),
DOCUMENT => document = Some(map.next_value()?),
ANIMATION => animation = Some(map.next_value()?),
GAME => game = Some(map.next_value()?),
PHOTO => photo = Some(map.next_value()?),
STICKER => sticker = Some(map.next_value()?),
VIDEO => video = Some(map.next_value()?),
VOICE => voice = Some(map.next_value()?),
VIDEO_NOTE => video_note = Some(map.next_value()?),
CAPTION => caption = Some(map.next_value()?),
CONTACT => contact = Some(map.next_value()?),
LOCATION => location = Some(map.next_value()?),
VENUE => venue = Some(map.next_value()?),
POLL => poll = Some(map.next_value()?),
NEW_CHAT_MEMBERS => new_chat_members = Some(map.next_value()?),
LEFT_CHAT_MEMBER => left_chat_member = Some(map.next_value()?),
NEW_CHAT_TITLE => new_chat_title = Some(map.next_value()?),
NEW_CHAT_PHOTO => new_chat_photo = Some(map.next_value()?),
DELETE_CHAT_PHOTO => delete_chat_photo = map.next_value()?,
GROUP_CHAT_CREATED => group_chat_created = map.next_value()?,
SUPERGROUP_CHAT_CREATED => {
supergroup_chat_created = map.next_value()?;
}
CHANNEL_CHAT_CREATED => {
channel_chat_created = map.next_value()?;
}
AUTO_DELETE_TIMER_CHANGED => {
auto_delete_timer_changed = Some(map.next_value()?);
}
MIGRATE_TO_CHAT_ID => {
migrate_to_chat_id = Some(map.next_value()?);
}
MIGRATE_FROM_CHAT_ID => {
migrate_from_chat_id = Some(map.next_value()?);
}
PINNED_MESSAGE => pinned_message = Some(map.next_value()?),
INVOICE => invoice = Some(map.next_value()?),
SUCCESSFUL_PAYMENT => {
successful_payment = Some(map.next_value()?);
}
CONNECTED_WEBSITE => {
connected_website = Some(map.next_value()?);
}
PASSPORT_DATA => passport_data = Some(map.next_value()?),
PROXIMITY_ALERT_TRIGGERED => {
proximity_alert = Some(map.next_value()?);
}
REPLY_MARKUP => reply_markup = Some(map.next_value()?),
VIA_BOT => via_bot = Some(map.next_value()?),
VOICE_CHAT_STARTED => {
let _ = map.next_value::<IgnoredAny>();
voice_chat_started = true;
}
VOICE_CHAT_PARTICIPANTS_INVITED => {
voice_chat_participants_invited = Some(map.next_value()?);
}
VOICE_CHAT_ENDED => voice_chat_ended = Some(map.next_value()?),
VOICE_CHAT_SCHEDULED => {
voice_chat_scheduled = Some(map.next_value()?);
}
_ => {
let _ = map.next_value::<IgnoredAny>();
}
}
}
#[allow(clippy::option_if_let_else, clippy::manual_map)]
let forward_source = if let Some(message_id) = forward_from_message_id {
Some(forward::From::Channel {
chat: forward_from_chat.ok_or_else(|| {
serde::de::Error::missing_field(FORWARD_FROM_CHAT)
})?,
message_id,
signature: forward_signature,
})
} else if let Some(chat) = forward_from_chat {
Some(forward::From::AnonymousAdmin {
chat,
signature: forward_signature,
})
} else if let Some(user) = forward_from {
Some(forward::From::User(user))
} else if let Some(hidden_user) = forward_sender_name {
Some(forward::From::HiddenUser(hidden_user))
} else {
None
};
let forward = if let Some(from) = forward_source {
Some(Forward {
from,
date: forward_date.ok_or_else(|| {
serde::de::Error::missing_field(FORWARD_DATE)
})?,
})
} else {
None
};
let caption = Text {
value: caption.unwrap_or_default(),
entities: caption_entities.unwrap_or_default(),
};
#[allow(clippy::option_if_let_else)]
let kind = if let Some(value) = text {
let text = Text {
value,
entities: entities.unwrap_or_default(),
};
Kind::Text(text)
} else if let Some(audio) = audio {
Kind::Audio {
audio,
caption,
media_group_id,
}
} else if let Some(game) = game {
Kind::Game(game)
} else if let Some(photo) = photo {
Kind::Photo {
photo,
caption,
media_group_id,
}
} else if let Some(sticker) = sticker {
Kind::Sticker(sticker)
} else if let Some(video) = video {
Kind::Video {
video,
caption,
media_group_id,
}
} else if let Some(voice) = voice {
Kind::Voice { voice, caption }
} else if let Some(video_note) = video_note {
Kind::VideoNote(video_note)
} else if let Some(contact) = contact {
Kind::Contact(contact)
} else if let Some(venue) = venue {
Kind::Venue(venue)
} else if let Some(location) = location {
Kind::Location(location)
} else if let Some(poll) = poll {
Kind::Poll(poll)
} else if let Some(animation) = animation {
Kind::Animation { animation, caption }
} else if let Some(document) = document {
Kind::Document {
document,
caption,
media_group_id,
}
} else if let Some(new_chat_members) = new_chat_members {
Kind::NewChatMembers(new_chat_members)
} else if let Some(left_chat_member) = left_chat_member {
Kind::LeftChatMember(left_chat_member)
} else if let Some(new_chat_title) = new_chat_title {
Kind::NewChatTitle(new_chat_title)
} else if let Some(new_chat_photo) = new_chat_photo {
Kind::NewChatPhoto(new_chat_photo)
} else if delete_chat_photo {
Kind::ChatPhotoDeleted
} else if group_chat_created {
Kind::GroupCreated
} else if supergroup_chat_created {
Kind::SupergroupCreated
} else if channel_chat_created {
Kind::ChannelCreated
} else if let Some(migrate_to) = migrate_to_chat_id {
Kind::MigrateTo(migrate_to)
} else if let Some(migrate_from) = migrate_from_chat_id {
Kind::MigrateFrom(migrate_from)
} else if let Some(pinned) = pinned_message {
Kind::Pinned(pinned)
} else if let Some(invoice) = invoice {
Kind::Invoice(invoice)
} else if let Some(successful_payment) = successful_payment {
Kind::SuccessfulPayment(successful_payment)
} else if let Some(connected_website) = connected_website {
Kind::ConnectedWebsite(connected_website)
} else if let Some(passport_data) = passport_data {
Kind::PassportData(passport_data)
} else if let Some(alert) = proximity_alert {
Kind::ProximityAlert(alert)
} else if voice_chat_started {
Kind::VoiceChatStarted
} else if let Some(invited) = voice_chat_participants_invited {
Kind::VoiceChatParticipantsInvited(invited)
} else if let Some(ended) = voice_chat_ended {
Kind::VoiceChatEnded(ended)
} else if let Some(scheduled) = voice_chat_scheduled {
Kind::VoiceChatScheduled(scheduled)
} else if let Some(dice) = dice {
Kind::Dice(dice)
} else if let Some(change) = auto_delete_timer_changed {
Kind::AutoDeleteTimerChanged(change)
} else {
Kind::Unknown
};
#[allow(clippy::option_if_let_else, clippy::manual_map)]
let from = if let Some(chat) = sender_chat {
Some(From::Chat(chat))
} else if let Some(user) = from {
Some(From::User(user))
} else {
None
};
Ok(Message {
id: message_id
.ok_or_else(|| serde::de::Error::missing_field(MESSAGE_ID))?,
from,
date: date.ok_or_else(|| serde::de::Error::missing_field(DATE))?,
chat: chat.ok_or_else(|| serde::de::Error::missing_field(CHAT))?,
forward,
reply_to: reply_to_message,
edit_date,
author_signature,
reply_markup,
via_bot,
kind,
})
}
}
impl<'de> serde::Deserialize<'de> for Message {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
d.deserialize_struct(
"Message",
&[
MESSAGE_ID,
FROM,
SENDER_CHAT,
DATE,
CHAT,
FORWARD_FROM,
FORWARD_FROM_CHAT,
FORWARD_FROM_MESSAGE_ID,
FORWARD_SIGNATURE,
FORWARD_SENDER_NAME,
FORWARD_SENDER_NAME,
FORWARD_DATE,
REPLY_TO_MESSAGE,
EDIT_DATE,
MEDIA_GROUP_ID,
AUTHOR_SIGNATURE,
TEXT,
ENTITIES,
CAPTION_ENTITIES,
AUDIO,
DICE,
DOCUMENT,
ANIMATION,
GAME,
PHOTO,
STICKER,
VIDEO,
VOICE,
VIDEO_NOTE,
CAPTION,
CONTACT,
LOCATION,
VENUE,
POLL,
NEW_CHAT_MEMBERS,
LEFT_CHAT_MEMBER,
NEW_CHAT_TITLE,
NEW_CHAT_PHOTO,
DELETE_CHAT_PHOTO,
GROUP_CHAT_CREATED,
SUPERGROUP_CHAT_CREATED,
CHANNEL_CHAT_CREATED,
MIGRATE_TO_CHAT_ID,
MIGRATE_FROM_CHAT_ID,
PINNED_MESSAGE,
INVOICE,
SUCCESSFUL_PAYMENT,
CONNECTED_WEBSITE,
PASSPORT_DATA,
REPLY_MARKUP,
VIA_BOT,
],
MessageVisitor,
)
}
}