From 24d76b59b37661757526c56d3bde3a6280fdb8b8 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Sun, 5 Jan 2020 02:35:25 +0600 Subject: [PATCH] Update the dependencies --- Cargo.toml | 29 ++-- src/dispatching/dispatchers/filter.rs | 162 +++++++++++---------- src/dispatching/updaters.rs | 30 ++-- src/network/download.rs | 17 +-- src/requests/all/add_sticker_to_set.rs | 5 + src/requests/all/create_new_sticker_set.rs | 7 + src/requests/all/edit_message_media.rs | 9 +- src/requests/all/send_animation.rs | 11 ++ src/requests/all/send_audio.rs | 11 ++ src/requests/all/send_document.rs | 8 + src/requests/all/send_media_group.rs | 4 + src/requests/all/send_photo.rs | 7 + src/requests/all/send_sticker.rs | 5 + src/requests/all/send_video.rs | 12 ++ src/requests/all/send_video_note.rs | 8 + src/requests/all/send_voice.rs | 8 + src/requests/form_builder.rs | 13 +- src/requests/utils.rs | 23 ++- src/types/parse_mode.rs | 4 +- 19 files changed, 235 insertions(+), 138 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index abc07eaa..c4ac9602 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,21 +6,20 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -reqwest = { version = "0.10.0-alpha.1", features = ["json", "unstable-stream"] } -serde_json = "1.0.41" +serde_json = "1.0.44" serde = { version = "1.0.101", features = ["derive"] } -derive_more = "0.15.0" -tokio = "0.2.0-alpha.6" -bytes = "0.4.12" + +tokio = { version = "0.2.6", features = ["full"] } +tokio-util = { version = "0.2.0", features = ["full"] } + +reqwest = { version = "0.10", features = ["json", "stream"] } log = "0.4.8" -pin-project = "0.4.0-alpha.7" -futures-preview = "0.3.0-alpha.19" -async-trait = "0.1.13" -thiserror = "1.0.2" +bytes = "0.5.3" + +derive_more = "0.99.2" +thiserror = "1.0.9" +async-trait = "0.1.22" +futures = "0.3.1" +pin-project = "0.4.6" serde_with_macros = "1.0.1" -either = "1.5.3" - -[features] -default = [] - -unstable-stream = [] # add streams to public API +either = "1.5.3" \ No newline at end of file diff --git a/src/dispatching/dispatchers/filter.rs b/src/dispatching/dispatchers/filter.rs index 9983f488..cf88cafa 100644 --- a/src/dispatching/dispatchers/filter.rs +++ b/src/dispatching/dispatchers/filter.rs @@ -186,79 +186,81 @@ impl<'a, HandlerE, Eh> FilterDispatcher<'a, HandlerE, Eh> { Eh: ErrorHandler>, { updater - .for_each_concurrent(None, |res| async { - let Update { kind, id } = match res { - Ok(upd) => upd, - Err(err) => { - self.error_handler - .handle_error(Either::Left(err)) + .for_each_concurrent(None, |res| { + async { + let Update { kind, id } = match res { + Ok(upd) => upd, + Err(err) => { + self.error_handler + .handle_error(Either::Left(err)) + .await; + return; + } + }; + + log::debug!( + "Handled update#{id:?}: {kind:?}", + id = id, + kind = kind + ); + + match kind { + UpdateKind::Message(mes) => { + Self::handle( + mes, + &self.message_handlers, + &self.error_handler, + ) + .await + } + UpdateKind::EditedMessage(mes) => { + Self::handle( + mes, + &self.edited_message_handlers, + &self.error_handler, + ) .await; - return; - } - }; - - log::debug!( - "Handled update#{id:?}: {kind:?}", - id = id, - kind = kind - ); - - match kind { - UpdateKind::Message(mes) => { - Self::handle( - mes, - &self.message_handlers, - &self.error_handler, - ) - .await - } - UpdateKind::EditedMessage(mes) => { - Self::handle( - mes, - &self.edited_message_handlers, - &self.error_handler, - ) - .await; - } - UpdateKind::ChannelPost(post) => { - Self::handle( - post, - &self.channel_post_handlers, - &self.error_handler, - ) - .await; - } - UpdateKind::EditedChannelPost(post) => { - Self::handle( - post, - &self.edited_channel_post_handlers, - &self.error_handler, - ) - .await; - } - UpdateKind::InlineQuery(query) => { - Self::handle( - query, - &self.inline_query_handlers, - &self.error_handler, - ) - .await; - } - UpdateKind::ChosenInlineResult(result) => { - Self::handle( - result, - &self.chosen_inline_result_handlers, - &self.error_handler, - ) - .await; - } - UpdateKind::CallbackQuery(callback) => { - Self::handle( - callback, - &self.callback_query_handlers, - &self.error_handler, - ) - .await; + } + UpdateKind::ChannelPost(post) => { + Self::handle( + post, + &self.channel_post_handlers, + &self.error_handler, + ) + .await; + } + UpdateKind::EditedChannelPost(post) => { + Self::handle( + post, + &self.edited_channel_post_handlers, + &self.error_handler, + ) + .await; + } + UpdateKind::InlineQuery(query) => { + Self::handle( + query, + &self.inline_query_handlers, + &self.error_handler, + ) + .await; + } + UpdateKind::ChosenInlineResult(result) => { + Self::handle( + result, + &self.chosen_inline_result_handlers, + &self.error_handler, + ) + .await; + } + UpdateKind::CallbackQuery(callback) => { + Self::handle( + callback, + &self.callback_query_handlers, + &self.error_handler, + ) + .await; + } } } }) @@ -308,13 +310,17 @@ mod tests { let counter2 = &AtomicI32::new(0); let mut dp = FilterDispatcher::::new(|_| async {}) - .message_handler(true, |_mes: Message| async move { - counter.fetch_add(1, Ordering::SeqCst); - Ok::<_, Infallible>(()) + .message_handler(true, |_mes: Message| { + async move { + counter.fetch_add(1, Ordering::SeqCst); + Ok::<_, Infallible>(()) + } }) - .message_handler(true, |_mes: Message| async move { - counter2.fetch_add(1, Ordering::SeqCst); - Ok::<_, Infallible>(()) + .message_handler(true, |_mes: Message| { + async move { + counter2.fetch_add(1, Ordering::SeqCst); + Ok::<_, Infallible>(()) + } }); dp.dispatch(one_message_updater()).await; diff --git a/src/dispatching/updaters.rs b/src/dispatching/updaters.rs index 56dbe2e9..28a65901 100644 --- a/src/dispatching/updaters.rs +++ b/src/dispatching/updaters.rs @@ -146,23 +146,25 @@ pub fn polling( stream::unfold( (allowed_updates, bot, 0), - move |(mut allowed_updates, bot, mut offset)| async move { - let mut req = bot.get_updates().offset(offset); - req.timeout = timeout; - req.limit = limit; - req.allowed_updates = allowed_updates.take(); + move |(mut allowed_updates, bot, mut offset)| { + async move { + let mut req = bot.get_updates().offset(offset); + req.timeout = timeout; + req.limit = limit; + req.allowed_updates = allowed_updates.take(); - let updates = match req.send().await { - Err(err) => vec![Err(err)], - Ok(updates) => { - if let Some(upd) = updates.last() { - offset = upd.id + 1; + let updates = match req.send().await { + Err(err) => vec![Err(err)], + Ok(updates) => { + if let Some(upd) = updates.last() { + offset = upd.id + 1; + } + updates.into_iter().map(Ok).collect::>() } - updates.into_iter().map(Ok).collect::>() - } - }; + }; - Some((stream::iter(updates), (allowed_updates, bot, offset))) + Some((stream::iter(updates), (allowed_updates, bot, offset))) + } }, ) .flatten() diff --git a/src/network/download.rs b/src/network/download.rs index 4ff6c794..0e68414f 100644 --- a/src/network/download.rs +++ b/src/network/download.rs @@ -1,10 +1,7 @@ use reqwest::Client; use tokio::io::{AsyncWrite, AsyncWriteExt}; -#[cfg(feature = "unstable-stream")] -use ::{bytes::Bytes, tokio::stream::Stream}; - -use crate::DownloadError; +use crate::errors::DownloadError; use super::TELEGRAM_API_URL; @@ -42,11 +39,13 @@ pub async fn download_file_stream( .await? .error_for_status()?; - Ok(futures::stream::unfold(res, |mut res| async { - match res.chunk().await { - Err(err) => Some((Err(err), res)), - Ok(Some(c)) => Some((Ok(c), res)), - Ok(None) => None, + Ok(futures::stream::unfold(res, |mut res| { + async { + match res.chunk().await { + Err(err) => Some((Err(err), res)), + Ok(Some(c)) => Some((Ok(c), res)), + Ok(None) => None, + } } })) } diff --git a/src/requests/all/add_sticker_to_set.rs b/src/requests/all/add_sticker_to_set.rs index fad08872..4e0ced7a 100644 --- a/src/requests/all/add_sticker_to_set.rs +++ b/src/requests/all/add_sticker_to_set.rs @@ -46,10 +46,15 @@ impl Request for AddStickerToSet<'_> { "addStickerToSet", FormBuilder::new() .add("user_id", &self.user_id) + .await .add("name", &self.name) + .await .add("png_sticker", &self.png_sticker) + .await .add("emojis", &self.emojis) + .await .add("mask_position", &self.mask_position) + .await .build(), ) .await diff --git a/src/requests/all/create_new_sticker_set.rs b/src/requests/all/create_new_sticker_set.rs index aba8219f..34c5005d 100644 --- a/src/requests/all/create_new_sticker_set.rs +++ b/src/requests/all/create_new_sticker_set.rs @@ -52,12 +52,19 @@ impl Request for CreateNewStickerSet<'_> { "createNewStickerSet", FormBuilder::new() .add("user_id", &self.user_id) + .await .add("name", &self.name) + .await .add("title", &self.title) + .await .add("png_sticker", &self.png_sticker) + .await .add("emojis", &self.emojis) + .await .add("contains_masks", &self.contains_masks) + .await .add("mask_position", &self.mask_position) + .await .build(), ) .await diff --git a/src/requests/all/edit_message_media.rs b/src/requests/all/edit_message_media.rs index 7941b1d9..a52d1b6f 100644 --- a/src/requests/all/edit_message_media.rs +++ b/src/requests/all/edit_message_media.rs @@ -43,10 +43,13 @@ impl Request for EditMessageMedia<'_> { } => { params = params .add("chat_id", chat_id) - .add("message_id", message_id); + .await + .add("message_id", message_id) + .await; } ChatOrInlineMessage::Inline { inline_message_id } => { - params = params.add("inline_message_id", inline_message_id); + params = + params.add("inline_message_id", inline_message_id).await; } } @@ -56,7 +59,9 @@ impl Request for EditMessageMedia<'_> { "editMessageMedia", params .add("media", &self.media) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_animation.rs b/src/requests/all/send_animation.rs index a416b02c..df8087be 100644 --- a/src/requests/all/send_animation.rs +++ b/src/requests/all/send_animation.rs @@ -72,16 +72,27 @@ impl Request for SendAnimation<'_> { "sendAnimation", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("animation", &self.animation) + .await .add("duration", &self.duration) + .await .add("width", &self.width) + .await .add("height", &self.height) + .await .add("thumb", &self.thumb) + .await .add("caption", &self.caption) + .await .add("parse_mode", &self.parse_mode) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_audio.rs b/src/requests/all/send_audio.rs index dc87956d..0fbcab1c 100644 --- a/src/requests/all/send_audio.rs +++ b/src/requests/all/send_audio.rs @@ -68,16 +68,27 @@ impl Request for SendAudio<'_> { "sendAudio", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("audio", &self.audio) + .await .add("caption", &self.caption) + .await .add("parse_mode", &self.parse_mode) + .await .add("duration", &self.duration) + .await .add("performer", &self.performer) + .await .add("title", &self.title) + .await .add("thumb", &self.thumb) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_document.rs b/src/requests/all/send_document.rs index 1817167f..ee0dea8d 100644 --- a/src/requests/all/send_document.rs +++ b/src/requests/all/send_document.rs @@ -61,13 +61,21 @@ impl Request for SendDocument<'_> { "sendDocument", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("document", &self.document) + .await .add("thumb", &self.thumb) + .await .add("caption", &self.caption) + .await .add("parse_mode", &self.parse_mode) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_media_group.rs b/src/requests/all/send_media_group.rs index 1b6fd151..6fd739a6 100644 --- a/src/requests/all/send_media_group.rs +++ b/src/requests/all/send_media_group.rs @@ -39,9 +39,13 @@ impl Request for SendMediaGroup<'_> { "sendMediaGroup", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("media", &self.media) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .build(), ) .await diff --git a/src/requests/all/send_photo.rs b/src/requests/all/send_photo.rs index 1ce44e87..4bd4f83e 100644 --- a/src/requests/all/send_photo.rs +++ b/src/requests/all/send_photo.rs @@ -50,12 +50,19 @@ impl Request for SendPhoto<'_> { "sendPhoto", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("photo", &self.photo) + .await .add("caption", &self.caption) + .await .add("parse_mode", &self.parse_mode) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_sticker.rs b/src/requests/all/send_sticker.rs index bbf41ffb..c88228e6 100644 --- a/src/requests/all/send_sticker.rs +++ b/src/requests/all/send_sticker.rs @@ -45,10 +45,15 @@ impl Request for SendSticker<'_> { "sendSticker", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("sticker", &self.sticker) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_video.rs b/src/requests/all/send_video.rs index 1e549fb0..83ac7f27 100644 --- a/src/requests/all/send_video.rs +++ b/src/requests/all/send_video.rs @@ -70,17 +70,29 @@ impl Request for SendVideo<'_> { "sendVideo", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("video", &self.video) + .await .add("duration", &self.duration) + .await .add("width", &self.width) + .await .add("height", &self.height) + .await .add("thumb", &self.thumb) + .await .add("caption", &self.caption) + .await .add("parse_mode", &self.parse_mode) + .await .add("supports_streaming", &self.supports_streaming) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_video_note.rs b/src/requests/all/send_video_note.rs index 9f1b3d94..659ff06d 100644 --- a/src/requests/all/send_video_note.rs +++ b/src/requests/all/send_video_note.rs @@ -59,13 +59,21 @@ impl Request for SendVideoNote<'_> { "sendVideoNote", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("video_note", &self.video_note) + .await .add("duration", &self.duration) + .await .add("length", &self.length) + .await .add("thumb", &self.thumb) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/all/send_voice.rs b/src/requests/all/send_voice.rs index 7bfd0163..51204b4e 100644 --- a/src/requests/all/send_voice.rs +++ b/src/requests/all/send_voice.rs @@ -56,13 +56,21 @@ impl Request for SendVoice<'_> { "sendVoice", FormBuilder::new() .add("chat_id", &self.chat_id) + .await .add("voice", &self.voice) + .await .add("caption", &self.caption) + .await .add("parse_mode", &self.parse_mode) + .await .add("duration", &self.duration) + .await .add("disable_notification", &self.disable_notification) + .await .add("reply_to_message_id", &self.reply_to_message_id) + .await .add("reply_markup", &self.reply_markup) + .await .build(), ) .await diff --git a/src/requests/form_builder.rs b/src/requests/form_builder.rs index e8aceed1..543932b0 100644 --- a/src/requests/form_builder.rs +++ b/src/requests/form_builder.rs @@ -22,7 +22,7 @@ impl FormBuilder { } /// Add the supplied key-value pair to this `FormBuilder`. - pub fn add<'a, T, N>(self, name: N, value: &T) -> Self + pub async fn add<'a, T, N>(self, name: N, value: &T) -> Self where N: Into>, T: IntoFormValue, @@ -32,20 +32,21 @@ impl FormBuilder { Some(FormValue::Str(string)) => Self { form: self.form.text(name, string), }, - Some(FormValue::File(path)) => self.add_file(name, path), + Some(FormValue::File(path)) => self.add_file(name, path).await, None => self, } } // used in SendMediaGroup - pub fn add_file<'a, N>(self, name: N, path_to_file: PathBuf) -> Self + pub async fn add_file<'a, N>(self, name: N, path_to_file: PathBuf) -> Self where N: Into>, { Self { - form: self - .form - .part(name.into().into_owned(), file_to_part(path_to_file)), + form: self.form.part( + name.into().into_owned(), + file_to_part(path_to_file).await, + ), } } diff --git a/src/requests/utils.rs b/src/requests/utils.rs index a53b91c0..d501c2c6 100644 --- a/src/requests/utils.rs +++ b/src/requests/utils.rs @@ -2,11 +2,11 @@ use std::path::PathBuf; use bytes::{Bytes, BytesMut}; use reqwest::{multipart::Part, Body}; -use tokio::{codec::FramedRead, prelude::*}; +use tokio_util::codec::{Decoder, FramedRead}; struct FileDecoder; -impl tokio::codec::Decoder for FileDecoder { +impl Decoder for FileDecoder { type Item = Bytes; type Error = std::io::Error; @@ -17,24 +17,23 @@ impl tokio::codec::Decoder for FileDecoder { if src.is_empty() { return Ok(None); } - Ok(Some(src.take().freeze())) + Ok(Some(src.split().freeze())) } } -pub fn file_to_part(path_to_file: PathBuf) -> Part { +pub async fn file_to_part(path_to_file: PathBuf) -> Part { let file_name = path_to_file .file_name() .unwrap() .to_string_lossy() .into_owned(); - let file = tokio::fs::File::open(path_to_file) - .map(|file| { - FramedRead::new( - file.unwrap(), /* TODO: this can cause panics */ - FileDecoder, - ) - }) - .flatten_stream(); + let file = FramedRead::new( + tokio::fs::File::open(path_to_file).await.unwrap(), /* TODO: this + * can + * cause panics */ + FileDecoder, + ); + Part::stream(Body::wrap_stream(file)).file_name(file_name) } diff --git a/src/types/parse_mode.rs b/src/types/parse_mode.rs index 871a4af8..34a0904c 100644 --- a/src/types/parse_mode.rs +++ b/src/types/parse_mode.rs @@ -49,7 +49,7 @@ use serde::{Deserialize, Serialize}; /// pre-formatted fixed-width code block written in the Rust programming /// language ``` /// ```` -/// +/// /// Please note: /// - Any character between 1 and 126 inclusively can be escaped anywhere with a /// preceding '\' character, in which case it is treated as an ordinary @@ -110,7 +110,7 @@ use serde::{Deserialize, Serialize}; /// pre-formatted fixed-width code block written in the Rust programming /// language ``` /// ```` -/// +/// /// Please note: /// - Entities must not be nested, use parse mode [`MarkdownV2`] instead. /// - There is no way to specify underline and strikethrough entities, use parse