Merge branch 'dev' into generics

This commit is contained in:
Temirkhan Myrzamadi 2019-10-14 16:00:52 +00:00 committed by GitHub
commit 186a3f0f8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 391 additions and 41 deletions

View file

@ -78,6 +78,7 @@ impl<'a> ForwardMessage<'a> {
self self
} }
#[allow(clippy::wrong_self_convention)]
pub fn from_chat_id<C>(mut self, value: C) -> Self pub fn from_chat_id<C>(mut self, value: C) -> Self
where where
C: Into<ChatId>, C: Into<ChatId>,

View file

@ -28,7 +28,7 @@ pub struct KickChatMember<'a> {
} }
#[async_trait] #[async_trait]
impl<'a> Request for KickChatMember<'a> { impl Request for KickChatMember<'_> {
type ReturnValue = True; type ReturnValue = True;
async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> { async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> {

View file

@ -49,7 +49,7 @@ impl<'a> PinChatMessage<'a> {
} }
#[async_trait] #[async_trait]
impl<'a> Request for PinChatMessage<'a> { impl Request for PinChatMessage<'_> {
type ReturnValue = True; type ReturnValue = True;
async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> { async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> {
self.send().await self.send().await

View file

@ -29,7 +29,7 @@ pub struct RestrictChatMember<'a> {
} }
#[async_trait] #[async_trait]
impl<'a> Request for RestrictChatMember<'a> { impl Request for RestrictChatMember<'_> {
type ReturnValue = True; type ReturnValue = True;
async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> { async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> {

View file

@ -65,7 +65,7 @@ pub struct SendAnimation<'a> {
} }
#[async_trait] #[async_trait]
impl<'a> Request for SendAnimation<'a> { impl Request for SendAnimation<'_> {
type ReturnValue = Message; type ReturnValue = Message;
async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> { async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> {

View file

@ -67,7 +67,7 @@ pub struct SendVideo<'a> {
} }
#[async_trait] #[async_trait]
impl<'a> Request for SendVideo<'a> { impl Request for SendVideo<'_> {
type ReturnValue = Message; type ReturnValue = Message;
async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> { async fn send_boxed(self) -> ResponseResult<Self::ReturnValue> {

View file

@ -30,12 +30,11 @@ pub fn file_to_part(path_to_file: &PathBuf) -> Part {
) )
}) })
.flatten_stream(); .flatten_stream();
let part = Part::stream(Body::wrap_stream(file)).file_name( Part::stream(Body::wrap_stream(file)).file_name(
path_to_file path_to_file
.file_name() .file_name()
.unwrap() .unwrap()
.to_string_lossy() .to_string_lossy()
.into_owned(), .into_owned(),
); )
part
} }

View file

@ -44,15 +44,13 @@ pub enum InlineKeyboardButtonKind {
/// Build buttons /// Build buttons
/// ///
/// Example: /// Example:
/// ```edition2018 /// ```
/// use async_telegram_bot::types::InlineKeyboardButton; /// use async_telegram_bot::types::InlineKeyboardButton;
/// ///
/// fn main() { /// let url_button = InlineKeyboardButton::url(
/// let url_button = InlineKeyboardButton::url( /// "Text".to_string(),
/// "Text".to_string(), /// "http://url.com".to_string(),
/// "http://url.com".to_string(), /// );
/// );
/// }
/// ``` /// ```
impl InlineKeyboardButton { impl InlineKeyboardButton {
pub fn url(text: String, url: String) -> InlineKeyboardButton { pub fn url(text: String, url: String) -> InlineKeyboardButton {

View file

@ -5,7 +5,7 @@ use crate::types::InlineKeyboardButton;
/// ///
/// *Note*: This will only work in Telegram versions released after /// *Note*: This will only work in Telegram versions released after
/// 9 April, 2016. Older clients will display unsupported message. /// 9 April, 2016. Older clients will display unsupported message.
#[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone, Default)]
pub struct InlineKeyboardMarkup { pub struct InlineKeyboardMarkup {
/// Array of button rows, each represented by an Array of /// Array of button rows, each represented by an Array of
/// [`InlineKeyboardButton`] objects /// [`InlineKeyboardButton`] objects
@ -28,9 +28,7 @@ pub struct InlineKeyboardMarkup {
/// ``` /// ```
impl InlineKeyboardMarkup { impl InlineKeyboardMarkup {
pub fn new() -> Self { pub fn new() -> Self {
Self { <_>::default()
inline_keyboard: vec![],
}
} }
pub fn append_row(mut self, buttons: Vec<InlineKeyboardButton>) -> Self { pub fn append_row(mut self, buttons: Vec<InlineKeyboardButton>) -> Self {

View file

@ -1,8 +1,4 @@
use crate::types::{ use crate::types::{Animation, Audio, Chat, Contact, Document, Game, InlineKeyboardMarkup, Invoice, Location, MessageEntity, PassportData, PhotoSize, Poll, Sticker, SuccessfulPayment, User, Venue, Video, VideoNote, Voice, True};
Animation, Audio, Chat, Contact, Document, Game, InlineKeyboardMarkup,
Invoice, Location, MessageEntity, PassportData, PhotoSize, Poll, Sticker,
SuccessfulPayment, User, Venue, Video, VideoNote, Voice,
};
#[derive(Debug, Deserialize, PartialEq, Clone)] #[derive(Debug, Deserialize, PartialEq, Clone)]
pub struct Message { pub struct Message {
@ -14,19 +10,6 @@ pub struct Message {
pub kind: MessageKind, pub kind: MessageKind,
} }
impl Message {
pub fn text(&self) -> Option<&str> {
if let MessageKind::Common {
media_kind: MediaKind::Text { ref text, .. },
..
} = self.kind
{
Some(text)
} else {
None
}
}
}
#[derive(Debug, Deserialize, PartialEq, Clone)] #[derive(Debug, Deserialize, PartialEq, Clone)]
#[serde(untagged)] #[serde(untagged)]
@ -54,16 +37,16 @@ pub enum MessageKind {
new_chat_photo: Vec<PhotoSize>, new_chat_photo: Vec<PhotoSize>,
}, },
DeleteChatPhoto { DeleteChatPhoto {
delete_chat_photo: bool, delete_chat_photo: True,
}, },
GroupChatCreated { GroupChatCreated {
group_chat_created: bool, group_chat_created: True,
}, },
SupergroupChatCreated { SupergroupChatCreated {
supergroup_chat_created: bool, supergroup_chat_created: True,
}, },
ChannelChatCreated { ChannelChatCreated {
channel_chat_created: bool, channel_chat_created: True,
}, },
Migrate { Migrate {
migrate_to_chat_id: i64, migrate_to_chat_id: i64,
@ -201,6 +184,375 @@ pub enum MediaKind {
}, },
} }
mod getters {
use std::ops::Deref;
use crate::types::{
self, Message, Sender, User, ForwardedFrom, Chat, MessageEntity,
PhotoSize, True,
message::{
MessageKind::{
Common, NewChatMembers, LeftChatMember, NewChatTitle,
NewChatPhoto, DeleteChatPhoto, GroupChatCreated,
ChannelChatCreated, Migrate, Invoice, SuccessfulPayment,
ConnectedWebsite, PassportData
},
MediaKind::{
Text, Video, Photo, Animation, Audio, Document, Voice, Game,
Sticker, VideoNote, Contact, Location, Poll, Venue
},
ForwardKind::{NonChannelForward, ChannelForward, Origin}
},
};
use crate::types::message::MessageKind::{SupergroupChatCreated, Pinned};
/// Getters for [Message] fields from [telegram docs].
///
/// [Message]: crate::types::Message
/// [telegram docs]: https://core.telegram.org/bots/api#message
impl Message {
/// NOTE: this is getter for both `from` and `author_signature`
pub fn from(&self) -> Option<&Sender> {
match &self.kind {
Common { from, .. } => Some(from),
_ => None,
}
}
/// NOTE: this is getter for both `forward_from` and
/// `forward_sender_name`
pub fn forward_from(&self) -> Option<&ForwardedFrom> {
match &self.kind {
Common { forward_kind: NonChannelForward { from, .. }, .. } =>
Some(from),
_ => None,
}
}
pub fn forward_from_chat(&self) -> Option<&Chat> {
match &self.kind {
Common { forward_kind: ChannelForward { chat, .. }, .. } =>
Some(chat),
_ => None,
}
}
pub fn forward_from_message_id(&self) -> Option<&i32> {
match &self.kind {
Common { forward_kind: ChannelForward { message_id, .. }, .. } =>
Some(message_id),
_ => None,
}
}
pub fn forward_signature(&self) -> Option<&str> {
match &self.kind {
Common { forward_kind: ChannelForward { signature, .. }, .. } =>
signature.as_ref().map(Deref::deref),
_ => None,
}
}
pub fn forward_date(&self) -> Option<&i32> {
match &self.kind {
Common { forward_kind: ChannelForward { date, .. }, .. } |
Common { forward_kind: NonChannelForward { date, .. }, .. } =>
Some(date),
_ => None,
}
}
pub fn reply_to_message(&self) -> Option<&Message> {
match &self.kind {
Common { forward_kind: Origin { reply_to_message, .. }, .. } =>
reply_to_message.as_ref().map(Deref::deref),
_ => None,
}
}
pub fn edit_date(&self) -> Option<&i32> {
match &self.kind {
Common { edit_date, .. } => edit_date.as_ref(),
_ => None,
}
}
pub fn media_group_id(&self) -> Option<&str> {
match &self.kind {
Common { media_kind: Video { media_group_id, .. }, .. } |
Common { media_kind: Photo { media_group_id, .. }, .. } =>
media_group_id.as_ref().map(Deref::deref),
_ => None,
}
}
pub fn text(&self) -> Option<&str> {
match &self.kind {
Common { media_kind: Text { text, .. }, .. } => Some(text),
_ => None,
}
}
pub fn entities(&self) -> Option<&[MessageEntity]> {
match &self.kind {
Common { media_kind: Text { entities, .. }, .. } =>
Some(entities),
_ => None,
}
}
pub fn caption_entities(&self) -> Option<&[MessageEntity]> {
match &self.kind {
Common { media_kind: Animation { caption_entities, .. }, .. } |
Common { media_kind: Audio { caption_entities, .. }, .. } |
Common { media_kind: Document { caption_entities, .. }, .. } |
Common { media_kind: Photo { caption_entities, .. }, .. } |
Common { media_kind: Video { caption_entities, .. }, .. } |
Common { media_kind: Voice { caption_entities, .. }, .. } =>
Some(caption_entities),
_ => None,
}
}
pub fn audio(&self) -> Option<&types::Audio> {
match &self.kind {
Common { media_kind: Audio { audio, .. }, .. } => Some(audio),
_ => None,
}
}
pub fn document(&self) -> Option<&types::Document> {
match &self.kind {
Common { media_kind: Document { document, .. }, .. } =>
Some(document),
_ => None,
}
}
pub fn animation(&self) -> Option<&types::Animation> {
match &self.kind {
Common { media_kind: Animation { animation, .. }, .. } =>
Some(animation),
_ => None,
}
}
pub fn game(&self) -> Option<&types::Game> {
match &self.kind {
Common { media_kind: Game { game, .. }, .. } => Some(game),
_ => None,
}
}
pub fn photo(&self) -> Option<&[PhotoSize]> {
match &self.kind {
Common { media_kind: Photo { photo, .. }, .. } => Some(photo),
_ => None,
}
}
pub fn sticker(&self) -> Option<&types::Sticker> {
match &self.kind {
Common { media_kind: Sticker { sticker, .. }, .. } =>
Some(sticker),
_ => None,
}
}
pub fn video(&self) -> Option<&types::Video> {
match &self.kind {
Common { media_kind: Video { video, .. }, .. } => Some(video),
_ => None,
}
}
pub fn voice(&self) -> Option<&types::Voice> {
match &self.kind {
Common { media_kind: Voice { voice, .. }, .. } => Some(voice),
_ => None,
}
}
pub fn video_note(&self) -> Option<&types::VideoNote> {
match &self.kind {
Common { media_kind: VideoNote { video_note, .. }, .. } =>
Some(video_note),
_ => None,
}
}
pub fn caption(&self) -> Option<&str> {
match &self.kind {
Common { media_kind, .. } => match media_kind {
Animation { caption, ..} |
Audio { caption, ..} |
Document { caption, ..} |
Photo { caption, ..} |
Video { caption, ..} |
Voice { caption, ..} => caption.as_ref().map(Deref::deref),
_ => None,
},
_ => None,
}
}
pub fn contact(&self) -> Option<&types::Contact> {
match &self.kind {
Common { media_kind: Contact { contact }, .. } => Some(contact),
_ => None,
}
}
pub fn location(&self) -> Option<&types::Location> {
match &self.kind {
Common { media_kind: Location { location, .. }, .. } =>
Some(location),
_ => None,
}
}
pub fn venue(&self) -> Option<&types::Venue> {
match &self.kind {
Common { media_kind: Venue { venue, .. }, .. } => Some(venue),
_ => None,
}
}
pub fn poll(&self) -> Option<&types::Poll> {
match &self.kind {
Common { media_kind: Poll { poll, .. }, .. } => Some(poll),
_ => None,
}
}
pub fn new_chat_members(&self) -> Option<&[User]> {
match &self.kind {
NewChatMembers { new_chat_members } => Some(new_chat_members),
_ => None,
}
}
pub fn left_chat_member(&self) -> Option<&User> {
match &self.kind {
LeftChatMember { left_chat_member } => Some(left_chat_member),
_ => None,
}
}
pub fn new_chat_title(&self) -> Option<&str> {
match &self.kind {
NewChatTitle { new_chat_title } => Some(new_chat_title),
_ => None,
}
}
pub fn new_chat_photo(&self) -> Option<&[PhotoSize]> {
match &self.kind {
NewChatPhoto { new_chat_photo } => Some(new_chat_photo),
_ => None,
}
}
// TODO: OK, `Option<True>` is weird, can we do something with it?
// mb smt like `is_delete_chat_photo(&self) -> bool`?
pub fn delete_chat_photo(&self) -> Option<True> {
match &self.kind {
DeleteChatPhoto { delete_chat_photo } =>
Some(*delete_chat_photo),
_ => None,
}
}
pub fn group_chat_created(&self) -> Option<True> {
match &self.kind {
GroupChatCreated { group_chat_created } =>
Some(*group_chat_created),
_ => None,
}
}
pub fn super_group_chat_created(&self) -> Option<True> {
match &self.kind {
SupergroupChatCreated { supergroup_chat_created } =>
Some(*supergroup_chat_created),
_ => None,
}
}
pub fn channel_chat_created(&self) -> Option<True> {
match &self.kind {
ChannelChatCreated { channel_chat_created } =>
Some(*channel_chat_created),
_ => None,
}
}
pub fn migrate_to_chat_id(&self) -> Option<&i64> {
match &self.kind {
Migrate { migrate_to_chat_id, .. } => Some(migrate_to_chat_id),
_ => None,
}
}
pub fn migrate_from_chat_id(&self) -> Option<&i64> {
match &self.kind {
Migrate { migrate_from_chat_id, .. } =>
Some(migrate_from_chat_id),
_ => None,
}
}
pub fn pinned_message(&self) -> Option<&Message> {
match &self.kind {
Pinned { pinned } => Some(pinned),
_ => None,
}
}
pub fn invoice(&self) -> Option<&types::Invoice> {
match &self.kind {
Invoice { invoice } => Some(invoice),
_ => None,
}
}
pub fn successful_payment(&self) -> Option<&types::SuccessfulPayment> {
match &self.kind {
SuccessfulPayment { successful_payment } =>
Some(successful_payment),
_ => None,
}
}
pub fn connected_website(&self) -> Option<&str> {
match &self.kind {
ConnectedWebsite { connected_website } =>
Some(connected_website),
_ => None,
}
}
pub fn passport_data(&self) -> Option<&types::PassportData> {
match &self.kind {
PassportData { passport_data } => Some(passport_data),
_ => None,
}
}
pub fn reply_markup(&self) -> Option<&types::InlineKeyboardMarkup> {
match &self.kind {
Common { reply_markup, .. } => reply_markup.as_ref(),
_ => None,
}
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::from_str; use serde_json::from_str;

View file

@ -8,6 +8,7 @@ impl std::convert::TryFrom<bool> for True {
type Error = (); type Error = ();
fn try_from(value: bool) -> Result<Self, Self::Error> { fn try_from(value: bool) -> Result<Self, Self::Error> {
#[allow(clippy::match_bool)]
match value { match value {
true => Ok(True), true => Ok(True),
false => Err(()), false => Err(()),
@ -37,6 +38,7 @@ impl<'de> Visitor<'de> for TrueVisitor {
where where
E: de::Error, E: de::Error,
{ {
#[allow(clippy::match_bool)]
match value { match value {
true => Ok(True), true => Ok(True),
false => Err(E::custom("expected `true`, found `false`")), false => Err(E::custom("expected `true`, found `false`")),