Update the dependencies

This commit is contained in:
Temirkhan Myrzamadi 2020-01-05 02:35:25 +06:00
parent e9c356c4df
commit 24d76b59b3
19 changed files with 235 additions and 138 deletions

View file

@ -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"

View file

@ -186,79 +186,81 @@ impl<'a, HandlerE, Eh> FilterDispatcher<'a, HandlerE, Eh> {
Eh: ErrorHandler<Either<UpdaterE, HandlerE>>,
{
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::<Infallible, _>::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;

View file

@ -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::<Vec<_>>()
}
updates.into_iter().map(Ok).collect::<Vec<_>>()
}
};
};
Some((stream::iter(updates), (allowed_updates, bot, offset)))
Some((stream::iter(updates), (allowed_updates, bot, offset)))
}
},
)
.flatten()

View file

@ -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,
}
}
}))
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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<Cow<'a, str>>,
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<Cow<'a, str>>,
{
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,
),
}
}

View file

@ -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)
}

View file

@ -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