add Update::user(), Update::chat() functions

This commit is contained in:
p0lunin 2020-01-29 19:25:56 +02:00
parent 2028ce70c7
commit 1cec80bf9d

View file

@ -2,10 +2,7 @@
use serde::{Deserialize, Serialize};
use crate::types::{
CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll,
PreCheckoutQuery, ShippingQuery,
};
use crate::types::{CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll, PreCheckoutQuery, ShippingQuery, User, Sender, Chat};
/// This [object] represents an incoming update.
///
@ -73,6 +70,62 @@ pub enum UpdateKind {
Poll(Poll),
}
impl Update {
pub fn user(&self) -> Option<&User> {
match &self.kind {
UpdateKind::Message(m) => {
match m.from() {
Some(Sender::User(user)) => Some(user),
_ => None,
}
}
UpdateKind::EditedMessage(m) => {
match m.from() {
Some(Sender::User(user)) => Some(user),
_ => None,
}
}
UpdateKind::CallbackQuery(query) => {
Some(&query.from)
}
UpdateKind::ChosenInlineResult(chosen) => {
Some(&chosen.from)
}
UpdateKind::InlineQuery(query) => {
Some(&query.from)
}
UpdateKind::ShippingQuery(query) => {
Some(&query.from)
}
UpdateKind::PreCheckoutQuery(query) => {
Some(&query.from)
}
_ => None
}
}
pub fn chat(&self) -> Option<&Chat> {
match &self.kind {
UpdateKind::Message(m) => {
Some(&m.chat)
}
UpdateKind::EditedMessage(m) => {
Some(&m.chat)
}
UpdateKind::ChannelPost(p) => {
Some(&p.chat)
}
UpdateKind::EditedChannelPost(p) => {
Some(&p.chat)
}
UpdateKind::CallbackQuery(q) => {
Some(&q.message?.chat)
}
_ => None
}
}
}
#[cfg(test)]
mod test {
use crate::types::{