From cbb607630847434dabe37adaf05359d949b380be Mon Sep 17 00:00:00 2001 From: nextel Date: Thu, 12 Sep 2019 18:57:14 +0300 Subject: [PATCH] cargo fmt for all project files --- src/core/network/mod.rs | 32 ++++++------ .../requests/edit_message_live_location.rs | 30 ++++++----- src/core/requests/form_builder.rs | 24 ++++----- src/core/requests/forward_message.rs | 31 ++++++------ src/core/requests/get_me.rs | 22 +++----- src/core/requests/mod.rs | 25 +++++----- src/core/requests/send_audio.rs | 27 ++++++---- src/core/requests/send_location.rs | 21 ++++---- src/core/requests/send_media_group.rs | 50 +++++++++++-------- src/core/requests/send_message.rs | 30 ++++++----- src/core/requests/send_photo.rs | 49 ++++++++++-------- .../requests/stop_message_live_location.rs | 26 +++++----- src/core/requests/utils.rs | 31 ++++++++---- src/core/types/answer_pre_checkout_query.rs | 2 +- src/core/types/chat.rs | 10 ++-- src/core/types/force_reply.rs | 2 +- src/core/types/inline_keyboard_button.rs | 17 ++++--- src/core/types/input_media.rs | 10 ++-- src/core/types/keyboard_button.rs | 2 +- src/core/types/message.rs | 6 +-- src/core/types/mod.rs | 20 ++++---- src/core/types/parse_mode.rs | 1 - src/core/types/poll.rs | 4 +- src/core/types/reply_keyboard_markup.rs | 19 +++---- src/core/types/reply_keyboard_remove.rs | 2 +- src/core/types/reply_markup.rs | 5 +- src/core/types/response_parameters.rs | 12 ++--- src/core/types/video.rs | 1 - 28 files changed, 266 insertions(+), 245 deletions(-) diff --git a/src/core/network/mod.rs b/src/core/network/mod.rs index dbc7fcf0..8fdde960 100644 --- a/src/core/network/mod.rs +++ b/src/core/network/mod.rs @@ -4,16 +4,12 @@ use crate::core::{ }; use apply::Apply; -use serde_json::Value; -use serde::{ - Serialize, - de::DeserializeOwned -}; use reqwest::{ + r#async::{multipart::Form, Client}, StatusCode, - r#async::{Client, multipart::Form}, }; - +use serde::{de::DeserializeOwned, Serialize}; +use serde_json::Value; const TELEGRAM_API_URL: &str = "https://api.telegram.org"; @@ -58,12 +54,9 @@ pub async fn request_multipart( .map_err(RequestError::NetworkError)?; let response = serde_json::from_str::>( - &response - .text() - .await - .map_err(RequestError::NetworkError)?, + &response.text().await.map_err(RequestError::NetworkError)?, ) - .map_err(RequestError::InvalidJson)?; + .map_err(RequestError::InvalidJson)?; match response { TelegramResponse::Ok { result, .. } => Ok(result), @@ -72,7 +65,10 @@ pub async fn request_multipart( error_code, response_parameters, .. - } => Err(RequestError::ApiError { description, status_code: StatusCode::from_u16(error_code).unwrap() }) + } => Err(RequestError::ApiError { + description, + status_code: StatusCode::from_u16(error_code).unwrap(), + }), } } @@ -90,10 +86,7 @@ pub async fn request_json( .map_err(RequestError::NetworkError)?; let response = serde_json::from_str::>( - &response - .text() - .await - .map_err(RequestError::NetworkError)?, + &response.text().await.map_err(RequestError::NetworkError)?, ) .map_err(RequestError::InvalidJson)?; @@ -104,7 +97,10 @@ pub async fn request_json( error_code, response_parameters, .. - } => Err(RequestError::ApiError { description, status_code: StatusCode::from_u16(error_code).unwrap() }) + } => Err(RequestError::ApiError { + description, + status_code: StatusCode::from_u16(error_code).unwrap(), + }), } } diff --git a/src/core/requests/edit_message_live_location.rs b/src/core/requests/edit_message_live_location.rs index e6003101..091b9161 100644 --- a/src/core/requests/edit_message_live_location.rs +++ b/src/core/requests/edit_message_live_location.rs @@ -1,7 +1,9 @@ -use serde::Serialize; -use crate::core::requests::{RequestContext, ChatId, Request, RequestFuture, ResponseResult}; -use crate::core::types::{Message, ReplyMarkup}; use crate::core::network; +use crate::core::requests::{ + ChatId, Request, RequestContext, RequestFuture, ResponseResult, +}; +use crate::core::types::{Message, ReplyMarkup}; +use serde::Serialize; #[derive(Debug, Clone, Serialize)] /// Use this method to edit live location messages. A location can be edited @@ -13,16 +15,16 @@ pub struct EditMessageLiveLocation<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// Required if inline_message_id is not specified. Unique identifier for /// the target chat or username of the target channel (in the format /// @channelusername) chat_id: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// Required if inline_message_id is not specified. Identifier of the /// message to edit message_id: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// Required if chat_id and message_id are not specified. Identifier of /// the inline message inline_message_id: Option, @@ -30,9 +32,9 @@ pub struct EditMessageLiveLocation<'a> { latitude: f64, /// Longitude of new location longitude: f64, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// A JSON-serialized object for a new inline keyboard. - reply_markup: Option + reply_markup: Option, } impl<'a> Request<'a> for EditMessageLiveLocation<'a> { @@ -44,14 +46,15 @@ impl<'a> Request<'a> for EditMessageLiveLocation<'a> { &self.ctx.client, &self.ctx.token, "editMessageLiveLocation", - &self - ).await + &self, + ) + .await }) } } impl<'a> EditMessageLiveLocation<'a> { - pub(crate) fn new ( + pub(crate) fn new( ctx: RequestContext<'a>, latitude: f64, longitude: f64, @@ -63,7 +66,7 @@ impl<'a> EditMessageLiveLocation<'a> { inline_message_id: None, latitude, longitude, - reply_markup: None + reply_markup: None, } } @@ -78,7 +81,8 @@ impl<'a> EditMessageLiveLocation<'a> { } pub fn inline_message_id(mut self, inline_message_id: T) -> Self - where T: Into + where + T: Into, { self.inline_message_id = Some(inline_message_id.into()); self diff --git a/src/core/requests/form_builder.rs b/src/core/requests/form_builder.rs index 66d34530..e266c837 100644 --- a/src/core/requests/form_builder.rs +++ b/src/core/requests/form_builder.rs @@ -1,14 +1,13 @@ use std::path::PathBuf; use crate::core::{ - types::{ParseMode, InputMedia}, - requests::{ChatId, utils}, + requests::{utils, ChatId}, + types::{InputMedia, ParseMode}, }; use reqwest::r#async::multipart::Form; use serde::Serialize; - /// This is a convenient struct that builds `reqwest::r#async::multipart::Form` /// from scratch. pub struct FormBuilder { @@ -22,20 +21,19 @@ impl FormBuilder { /// Add the supplied key-value pair to this `FormBuilder`. pub fn add(self, name: &str, value: &T) -> Self - where T: ToFormValue + ?Sized + where + T: ToFormValue + ?Sized, { Self { - form: self.form.text( - name.to_owned(), - value.to_form_value() - ) + form: self.form.text(name.to_owned(), value.to_form_value()), } } /// Adds a key-value pair to the supplied `FormBuilder` if `value` is some. /// Don't forget to implement `serde::Serialize` for `T`! pub fn add_if_some(self, name: &str, value: Option<&T>) -> Self - where T: ToFormValue + ?Sized + where + T: ToFormValue + ?Sized, { match value { None => Self { form: self.form }, @@ -45,7 +43,9 @@ impl FormBuilder { pub fn add_file(self, name: &str, path_to_file: &PathBuf) -> Self { Self { - form: self.form.part(name.to_owned(), utils::file_to_part(path_to_file)) + form: self + .form + .part(name.to_owned(), utils::file_to_part(path_to_file)), } } @@ -70,9 +70,7 @@ macro_rules! impl_for_struct { }; } -impl_for_struct!( - bool, i32, i64, Vec -); +impl_for_struct!(bool, i32, i64, Vec); impl ToFormValue for str { fn to_form_value(&self) -> String { diff --git a/src/core/requests/forward_message.rs b/src/core/requests/forward_message.rs index 1fb0ad6d..8e61bce6 100644 --- a/src/core/requests/forward_message.rs +++ b/src/core/requests/forward_message.rs @@ -1,17 +1,12 @@ use crate::core::{ network, - types::Message, requests::{ - ChatId, - Request, - RequestFuture, - RequestContext, - ResponseResult, - form_builder::FormBuilder, + form_builder::FormBuilder, ChatId, Request, RequestContext, + RequestFuture, ResponseResult, }, + types::Message, }; - #[derive(Debug, Clone, Serialize)] /// Use this method to forward messages of any kind. On success, the sent /// [`Message`] is returned. @@ -28,8 +23,9 @@ pub struct ForwardMessage<'a> { /// Message identifier in the chat specified in from_chat_id pub message_id: i64, - /// Sends the message silently. Users will receive a notification with no sound. - #[serde(skip_serializing_if="Option::is_none")] + /// Sends the message silently. Users will receive a notification with no + /// sound. + #[serde(skip_serializing_if = "Option::is_none")] pub disable_notification: Option, } @@ -43,22 +39,25 @@ impl<'a> Request<'a> for ForwardMessage<'a> { self.ctx.token, "forwardMessage", &self, - ).await + ) + .await }) } } impl<'a> ForwardMessage<'a> { - pub(crate) fn new(ctx: RequestContext<'a>, - chat_id: ChatId, - from_chat_id: ChatId, - message_id: i64) -> Self { + pub(crate) fn new( + ctx: RequestContext<'a>, + chat_id: ChatId, + from_chat_id: ChatId, + message_id: i64, + ) -> Self { Self { ctx, chat_id, from_chat_id, message_id, - disable_notification: None + disable_notification: None, } } diff --git a/src/core/requests/get_me.rs b/src/core/requests/get_me.rs index fc894ae6..343b36e4 100644 --- a/src/core/requests/get_me.rs +++ b/src/core/requests/get_me.rs @@ -1,15 +1,9 @@ use crate::core::{ network, + requests::{Request, RequestContext, RequestFuture, ResponseResult}, types::User, - requests::{ - Request, - RequestFuture, - RequestContext, - ResponseResult - }, }; - #[derive(Debug, Clone)] /// A simple method for testing your bot's auth token. Requires no parameters. /// Returns basic information about the bot in form of a [`User`] object. @@ -21,14 +15,12 @@ impl<'a> Request<'a> for GetMe<'a> { type ReturnValue = User; fn send(self) -> RequestFuture<'a, ResponseResult> { - Box::pin( - network::request_multipart( - self.ctx.client, - self.ctx.token, - "getMe", - None - ) - ) + Box::pin(network::request_multipart( + self.ctx.client, + self.ctx.token, + "getMe", + None, + )) } } diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index a9b4cd61..7e2534c6 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -1,19 +1,17 @@ -use std::pin::Pin; use std::future::Future; +use std::pin::Pin; -use reqwest::{ - r#async::Client, StatusCode -}; +use reqwest::{r#async::Client, StatusCode}; use serde::de::DeserializeOwned; - mod form_builder; mod utils; #[derive(Debug, Display)] pub enum RequestError { #[display(fmt = "Telegram error #{}: {}", status_code, description)] - ApiError { // TODO: add response parameters + ApiError { + // TODO: add response parameters status_code: StatusCode, description: String, }, @@ -82,18 +80,21 @@ mod tests { #[test] fn chat_id_channel_username_serialization() { let expected_json = String::from(r#""@username""#); - let actual_json = serde_json::to_string(&ChatId::ChannelUsername(String::from("@username"))).unwrap(); + let actual_json = serde_json::to_string(&ChatId::ChannelUsername( + String::from("@username"), + )) + .unwrap(); assert_eq!(expected_json, actual_json) } } -pub mod get_me; -pub mod send_message; +pub mod edit_message_live_location; pub mod forward_message; -pub mod send_photo; -pub mod send_media_group; +pub mod get_me; pub mod send_audio; pub mod send_location; -pub mod edit_message_live_location; +pub mod send_media_group; +pub mod send_message; +pub mod send_photo; pub mod stop_message_live_location; diff --git a/src/core/requests/send_audio.rs b/src/core/requests/send_audio.rs index 26c9003b..ab63980d 100644 --- a/src/core/requests/send_audio.rs +++ b/src/core/requests/send_audio.rs @@ -1,8 +1,10 @@ use crate::core::{ network, - requests::{ChatId, Request, RequestFuture, ResponseResult, RequestContext}, requests::form_builder::FormBuilder, - types::{InputFile, ParseMode, Message, ReplyMarkup}, + requests::{ + ChatId, Request, RequestContext, RequestFuture, ResponseResult, + }, + types::{InputFile, Message, ParseMode, ReplyMarkup}, }; /// Use this method to send audio files, if you want Telegram clients to display @@ -71,11 +73,11 @@ impl<'a> Request<'a> for SendAudio<'a> { .add_if_some("title", self.title.as_ref()) .add_if_some( "disable_notification", - self.disable_notification.as_ref() + self.disable_notification.as_ref(), ) .add_if_some( "reply_to_message_id", - self.reply_to_message_id.as_ref() + self.reply_to_message_id.as_ref(), ); params = match self.audio { InputFile::File(file) => params.add_file("audio", &file), @@ -95,8 +97,9 @@ impl<'a> Request<'a> for SendAudio<'a> { &self.ctx.client, &self.ctx.token, "sendAudio", - Some(params) - ).await + Some(params), + ) + .await }) } } @@ -119,7 +122,7 @@ impl<'a> SendAudio<'a> { thumb: None, disable_notification: None, reply_to_message_id: None, - reply_markup: None + reply_markup: None, } } @@ -163,12 +166,18 @@ impl<'a> SendAudio<'a> { self } - pub fn disable_notification>(mut self, disable_notification: T) -> Self { + pub fn disable_notification>( + mut self, + disable_notification: T, + ) -> Self { self.disable_notification = Some(disable_notification.into()); self } - pub fn reply_to_message_id>(mut self, reply_to_message_id: T) -> Self { + pub fn reply_to_message_id>( + mut self, + reply_to_message_id: T, + ) -> Self { self.reply_to_message_id = Some(reply_to_message_id.into()); self } diff --git a/src/core/requests/send_location.rs b/src/core/requests/send_location.rs index 48f2ff94..cf9770ad 100644 --- a/src/core/requests/send_location.rs +++ b/src/core/requests/send_location.rs @@ -1,7 +1,9 @@ use crate::core::{ - requests::{RequestContext, ChatId, Request, RequestFuture, ResponseResult}, - types::{Message, ReplyMarkup}, network, + requests::{ + ChatId, Request, RequestContext, RequestFuture, ResponseResult, + }, + types::{Message, ReplyMarkup}, }; use serde::Serialize; @@ -20,19 +22,19 @@ pub struct SendLocation<'a> { latitude: f64, /// Longitude of the location longitude: f64, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// Period in seconds for which the location will be updated /// (see [Live Locations](https://telegram.org/blog/live-locations)), /// should be between 60 and 86400. live_period: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// Sends the message silently. Users will receive a notification with /// no sound. disable_notification: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] /// If the message is a reply, ID of the original message reply_to_message_id: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] reply_markup: Option, } @@ -45,8 +47,9 @@ impl<'a> Request<'a> for SendLocation<'a> { &self.ctx.client, &self.ctx.token, "sendLocation", - &self - ).await + &self, + ) + .await }) } } @@ -66,7 +69,7 @@ impl<'a> SendLocation<'a> { live_period: None, disable_notification: None, reply_to_message_id: None, - reply_markup: None + reply_markup: None, } } diff --git a/src/core/requests/send_media_group.rs b/src/core/requests/send_media_group.rs index 9fe58575..52c41453 100644 --- a/src/core/requests/send_media_group.rs +++ b/src/core/requests/send_media_group.rs @@ -1,14 +1,10 @@ use crate::core::{ network::request_multipart, - types::{Message, InputMedia, InputFile}, requests::{ - ChatId, - Request, - RequestFuture, - RequestContext, - ResponseResult, - form_builder::FormBuilder, - } + form_builder::FormBuilder, ChatId, Request, RequestContext, + RequestFuture, ResponseResult, + }, + types::{InputFile, InputMedia, Message}, }; use apply::Apply; @@ -32,28 +28,40 @@ impl<'a> Request<'a> for SendMediaGroup<'a> { let params = FormBuilder::new() .add("chat_id", &self.chat_id) .apply(|form| { - self.media - .iter() - .map(|e| e.media()) - .fold(form, |acc, file| { + self.media.iter().map(|e| e.media()).fold( + form, + |acc, file| { if let InputFile::File(path) = file { acc.add_file( - &path - .file_name() - .unwrap() - .to_string_lossy(), - path + &path + .file_name() + .unwrap() + .to_string_lossy(), + path, ) } else { acc } - }) + }, + ) }) .add("media", &self.media) - .add_if_some("disable_notification", self.disable_notification.as_ref()) - .add_if_some("reply_to_message_id", self.reply_to_message_id.as_ref()) + .add_if_some( + "disable_notification", + self.disable_notification.as_ref(), + ) + .add_if_some( + "reply_to_message_id", + self.reply_to_message_id.as_ref(), + ) .build(); - request_multipart(&self.ctx.client, &self.ctx.token, "sendMediaGroup", Some(params)).await + request_multipart( + &self.ctx.client, + &self.ctx.token, + "sendMediaGroup", + Some(params), + ) + .await }) } } diff --git a/src/core/requests/send_message.rs b/src/core/requests/send_message.rs index e0b5c5b1..889bcb14 100644 --- a/src/core/requests/send_message.rs +++ b/src/core/requests/send_message.rs @@ -1,19 +1,15 @@ use crate::core::{ network, - types::{Message, ParseMode, ReplyMarkup}, requests::{ - form_builder::FormBuilder, - ChatId, - Request, - RequestFuture, - RequestContext, - ResponseResult, + form_builder::FormBuilder, ChatId, Request, RequestContext, + RequestFuture, ResponseResult, }, + types::{Message, ParseMode, ReplyMarkup}, }; - #[derive(Debug, Clone, Serialize)] -/// Use this method to send text messages. On success, the sent [`Message`] is returned. +/// Use this method to send text messages. On success, the sent [`Message`] is +/// returned. pub struct SendMessage<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, @@ -32,18 +28,19 @@ pub struct SendMessage<'a> { /// [Html]: crate::core::types::ParseMode::Html /// [bold, italic, fixed-width text or inline URLs]: /// crate::core::types::ParseMode - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub parse_mode: Option, /// Disables link previews for links in this message - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub disable_web_page_preview: Option, - /// Sends the message silently. Users will receive a notification with no sound. - #[serde(skip_serializing_if="Option::is_none")] + /// Sends the message silently. Users will receive a notification with no + /// sound. + #[serde(skip_serializing_if = "Option::is_none")] pub disable_notification: Option, /// If the message is a reply, ID of the original message - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub reply_to_message_id: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub reply_markup: Option, } @@ -57,7 +54,8 @@ impl<'a> Request<'a> for SendMessage<'a> { self.ctx.token, "sendMessage", &self, - ).await + ) + .await }) } } diff --git a/src/core/requests/send_photo.rs b/src/core/requests/send_photo.rs index cae3fa89..0d4e40e3 100644 --- a/src/core/requests/send_photo.rs +++ b/src/core/requests/send_photo.rs @@ -2,20 +2,16 @@ use std::path::Path; use crate::core::{ network, - types::{ParseMode, Message, InputFile, ReplyMarkup}, requests::{ - ChatId, - Request, - RequestFuture, - RequestContext, - ResponseResult, - form_builder::FormBuilder, + form_builder::FormBuilder, ChatId, Request, RequestContext, + RequestFuture, ResponseResult, }, + types::{InputFile, Message, ParseMode, ReplyMarkup}, }; - #[derive(Debug, Clone)] -/// Use this method to send photos. On success, the sent [`Message`] is returned. +/// Use this method to send photos. On success, the sent [`Message`] is +/// returned. pub struct SendPhoto<'a> { ctx: RequestContext<'a>, @@ -23,13 +19,14 @@ pub struct SendPhoto<'a> { /// (in the format @channelusername) pub chat_id: ChatId, /// Photo to send. - /// [`InputFile::FileId`] - Pass a file_id as String to send a photo that exists on the - /// Telegram servers (recommended) + /// [`InputFile::FileId`] - Pass a file_id as String to send a photo that + /// exists on the Telegram servers (recommended) /// [`InputFile::Url`] - Pass an HTTP URL as a String for Telegram /// to get a photo from the Internet /// [`InputFile::File`] - Upload a new photo. pub photo: InputFile, - /// Photo caption (may also be used when resending photos by file_id), 0-1024 characters + /// Photo caption (may also be used when resending photos by file_id), + /// 0-1024 characters pub caption: Option, /// Send [Markdown] or [HTML], /// if you want Telegram apps to show [bold, italic, fixed-width text @@ -40,7 +37,8 @@ pub struct SendPhoto<'a> { /// [bold, italic, fixed-width text or inline URLs]: /// crate::core::types::ParseMode pub parse_mode: Option, - /// Sends the message silently. Users will receive a notification with no sound. + /// Sends the message silently. Users will receive a notification with no + /// sound. pub disable_notification: Option, /// If the message is a reply, ID of the original message pub reply_to_message_id: Option, @@ -58,13 +56,13 @@ impl<'a> Request<'a> for SendPhoto<'a> { .add_if_some("parse_mode", self.parse_mode.as_ref()) .add_if_some( "disable_notification", - self.disable_notification.as_ref() + self.disable_notification.as_ref(), ) .add_if_some( "reply_to_message_id", - self.reply_to_message_id.as_ref() + self.reply_to_message_id.as_ref(), ); - + params = match self.photo { InputFile::File(path) => params.add_file("photo", &path), InputFile::Url(url) => params.add("photo", &url), @@ -76,8 +74,9 @@ impl<'a> Request<'a> for SendPhoto<'a> { &self.ctx.client, &self.ctx.token, "sendPhoto", - Some(params) - ).await + Some(params), + ) + .await }) } } @@ -86,7 +85,7 @@ impl<'a> SendPhoto<'a> { pub(crate) fn new( ctx: RequestContext<'a>, chat_id: ChatId, - photo: InputFile + photo: InputFile, ) -> Self { Self { ctx, @@ -96,7 +95,7 @@ impl<'a> SendPhoto<'a> { parse_mode: None, disable_notification: None, reply_to_message_id: None, - reply_markup: None + reply_markup: None, } } @@ -120,12 +119,18 @@ impl<'a> SendPhoto<'a> { self } - pub fn disable_notification>(mut self, disable_notification: T) -> Self { + pub fn disable_notification>( + mut self, + disable_notification: T, + ) -> Self { self.disable_notification = Some(disable_notification.into()); self } - pub fn reply_to_message_id>(mut self, reply_to_message_id: T) -> Self { + pub fn reply_to_message_id>( + mut self, + reply_to_message_id: T, + ) -> Self { self.reply_to_message_id = Some(reply_to_message_id.into()); self } diff --git a/src/core/requests/stop_message_live_location.rs b/src/core/requests/stop_message_live_location.rs index ee6777a6..e102635e 100644 --- a/src/core/requests/stop_message_live_location.rs +++ b/src/core/requests/stop_message_live_location.rs @@ -3,7 +3,7 @@ use std::path::Path; use crate::core::{ network, requests::{ - ChatId, form_builder::FormBuilder, Request, RequestContext, + form_builder::FormBuilder, ChatId, Request, RequestContext, RequestFuture, ResponseResult, }, types::{InlineKeyboardMarkup, Message, ParseMode}, @@ -18,19 +18,19 @@ struct StopMessageLiveLocation<'a> { /// Required if inline_message_id is not specified. Unique identifier for /// the target chat or username of the target channel (in the format /// @channelusername) - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub chat_id: Option, /// Required if inline_message_id is not specified. Identifier of the /// message with live location to stop - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub message_id: Option, /// Required if chat_id and message_id are not specified. Identifier of the /// inline message - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub inline_message_id: Option, /// A JSON-serialized object InlineKeyboardMarkup for a new inline /// keyboard. - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub reply_markup: Option, } @@ -51,9 +51,7 @@ impl<'a> Request<'a> for StopMessageLiveLocation<'a> { } impl<'a> StopMessageLiveLocation<'a> { - fn new( - ctx: RequestContext<'a>, - ) -> Self { + fn new(ctx: RequestContext<'a>) -> Self { Self { ctx, chat_id: None, @@ -64,28 +62,32 @@ impl<'a> StopMessageLiveLocation<'a> { } pub fn chat_id(mut self, chat_id: T) -> Self - where T: Into + where + T: Into, { self.chat_id = chat_id.into(); self } pub fn message_id(mut self, message_id: T) -> Self - where T: Into + where + T: Into, { self.message_id = Some(message_id.into()); self } pub fn inline_message_id(mut self, inline_message_id: T) -> Self - where T: Into + where + T: Into, { self.inline_message_id = Some(inline_message_id.into()); self } pub fn reply_markup(mut self, reply_markup: T) -> Self - where T: Into + where + T: Into, { self.inline_message_id = Some(reply_markup.into()); self diff --git a/src/core/requests/utils.rs b/src/core/requests/utils.rs index 292d9981..44231ef2 100644 --- a/src/core/requests/utils.rs +++ b/src/core/requests/utils.rs @@ -1,9 +1,9 @@ -use tokio::codec::FramedRead; -use std::fs::File; use bytes::{Bytes, BytesMut}; -use tokio::prelude::*; use reqwest::r#async::multipart::Part; +use std::fs::File; use std::path::PathBuf; +use tokio::codec::FramedRead; +use tokio::prelude::*; struct FileDecoder; @@ -11,9 +11,12 @@ impl tokio::codec::Decoder for FileDecoder { type Item = Bytes; type Error = std::io::Error; - fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { + fn decode( + &mut self, + src: &mut BytesMut, + ) -> Result, Self::Error> { if src.is_empty() { - return Ok(None) + return Ok(None); } Ok(Some(src.take().freeze())) } @@ -21,9 +24,19 @@ impl tokio::codec::Decoder for FileDecoder { pub fn file_to_part(path_to_file: &PathBuf) -> Part { let file = tokio::fs::File::open(path_to_file.clone()) - .map(|file| FramedRead::new(file.unwrap() /* TODO: this can cause panics */, FileDecoder)) + .map(|file| { + FramedRead::new( + file.unwrap(), /* TODO: this can cause panics */ + FileDecoder, + ) + }) .flatten_stream(); - let part = Part::stream(file) - .file_name(path_to_file.file_name().unwrap().to_string_lossy().into_owned()); + let part = Part::stream(file).file_name( + path_to_file + .file_name() + .unwrap() + .to_string_lossy() + .into_owned(), + ); part -} \ No newline at end of file +} diff --git a/src/core/types/answer_pre_checkout_query.rs b/src/core/types/answer_pre_checkout_query.rs index 3420a4ee..07d1c758 100644 --- a/src/core/types/answer_pre_checkout_query.rs +++ b/src/core/types/answer_pre_checkout_query.rs @@ -1,4 +1,4 @@ -#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone )] +#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct AnswerPreCheckoutQuery { pub pre_checkout_query_id: String, pub ok: bool, diff --git a/src/core/types/chat.rs b/src/core/types/chat.rs index e57efa42..558d0a6e 100644 --- a/src/core/types/chat.rs +++ b/src/core/types/chat.rs @@ -1,6 +1,5 @@ use crate::core::types::{ChatPermissions, ChatPhoto, Message}; - #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)] pub struct Chat { pub id: i64, @@ -9,7 +8,6 @@ pub struct Chat { pub photo: Option, } - #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)] #[serde(untagged)] pub enum ChatKind { @@ -32,7 +30,6 @@ pub enum ChatKind { }, } - #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)] #[serde(rename_all = "snake_case")] #[serde(tag = "type")] @@ -101,10 +98,9 @@ mod tests { }, photo: None, }; - let actual = from_str( - r#"{"id":-1,"type":"channel","username":"channelname"}"#, - ) - .unwrap(); + let actual = + from_str(r#"{"id":-1,"type":"channel","username":"channelname"}"#) + .unwrap(); assert_eq!(expected, actual); } diff --git a/src/core/types/force_reply.rs b/src/core/types/force_reply.rs index 4daaeb50..4c3b295e 100644 --- a/src/core/types/force_reply.rs +++ b/src/core/types/force_reply.rs @@ -15,4 +15,4 @@ pub struct ForceReply { /// [`Message`] object; 2) if the bot's message is a reply /// (has reply_to_message_id), sender of the original message. pub selective: Option, -} \ No newline at end of file +} diff --git a/src/core/types/inline_keyboard_button.rs b/src/core/types/inline_keyboard_button.rs index 2d836da2..73389ae6 100644 --- a/src/core/types/inline_keyboard_button.rs +++ b/src/core/types/inline_keyboard_button.rs @@ -8,7 +8,9 @@ pub struct InlineKeyboardButton { pub kind: InlineKeyboardButtonKind, } -#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Eq, Hash, Deserialize)] +#[derive( + Debug, Clone, PartialEq, PartialOrd, Serialize, Eq, Hash, Deserialize, +)] #[serde(rename_all = "snake_case")] pub enum InlineKeyboardButtonKind { /// HTTP or tg:// url to be opened when button is pressed @@ -27,13 +29,14 @@ pub enum InlineKeyboardButtonKind { /// the user will be automatically returned to the chat they switched from, /// skipping the chat selection screen. SwitchInlineQuery(String), - /// Optional. If set, pressing the button will insert the bot‘s username and - /// the specified inline query in the current chat's input field. Can be - /// empty, in which case only the bot’s username will be inserted. + /// Optional. If set, pressing the button will insert the bot‘s username + /// and the specified inline query in the current chat's input field. + /// Can be empty, in which case only the bot’s username will be + /// inserted. /// ///This offers a quick way for the user to open your bot in inline mode in /// the same chat – good for selecting something from multiple options. SwitchInlineQueryCurrentChat(String), - // CallbackGame(CallbackGame), TODO: разобраться, что с этим делать - // TODO: add LoginUrl, pay -} \ No newline at end of file + /* CallbackGame(CallbackGame), TODO: разобраться, что с этим делать + * TODO: add LoginUrl, pay */ +} diff --git a/src/core/types/input_media.rs b/src/core/types/input_media.rs index 3c5021d7..02239d69 100644 --- a/src/core/types/input_media.rs +++ b/src/core/types/input_media.rs @@ -168,11 +168,11 @@ pub enum InputMedia { impl InputMedia { pub fn media(&self) -> &InputFile { match self { - InputMedia::Photo { media, .. } | - InputMedia::Document { media, .. } | - InputMedia::Audio { media, .. } | - InputMedia::Animation { media, .. } | - InputMedia::Video { media, .. } => media, + InputMedia::Photo { media, .. } + | InputMedia::Document { media, .. } + | InputMedia::Audio { media, .. } + | InputMedia::Animation { media, .. } + | InputMedia::Video { media, .. } => media, } } } diff --git a/src/core/types/keyboard_button.rs b/src/core/types/keyboard_button.rs index 712743d2..41d1b156 100644 --- a/src/core/types/keyboard_button.rs +++ b/src/core/types/keyboard_button.rs @@ -16,4 +16,4 @@ pub struct KeyboardButton { /// Optional. If True, the user's current location will be sent when the /// button is pressed. Available in private chats only pub request_location: Option, -} \ No newline at end of file +} diff --git a/src/core/types/message.rs b/src/core/types/message.rs index 76406b4f..ab0d1922 100644 --- a/src/core/types/message.rs +++ b/src/core/types/message.rs @@ -125,13 +125,13 @@ pub enum MediaKind { document: (), caption: Option, #[serde(default = "Vec::new")] - caption_entities: Vec + caption_entities: Vec, }, Audio { audio: Audio, caption: Option, #[serde(default = "Vec::new")] - caption_entities: Vec + caption_entities: Vec, }, Contact { contact: Contact, @@ -140,7 +140,7 @@ pub enum MediaKind { document: Document, caption: Option, #[serde(default = "Vec::new")] - caption_entities: Vec + caption_entities: Vec, }, Game { game: Game, diff --git a/src/core/types/mod.rs b/src/core/types/mod.rs index 06e855e7..bbbd4237 100644 --- a/src/core/types/mod.rs +++ b/src/core/types/mod.rs @@ -8,9 +8,13 @@ pub use self::{ chat_permissions::ChatPermissions, chat_photo::ChatPhoto, document::Document, + force_reply::ForceReply, + inline_keyboard_button::InlineKeyboardButton, + inline_keyboard_markup::InlineKeyboardMarkup, input_file::InputFile, input_media::InputMedia, invoice::Invoice, + keyboard_button::KeyboardButton, label_price::LabeledPrice, message::{ ForwardKind, ForwardedFrom, MediaKind, Message, MessageKind, Sender, @@ -21,6 +25,9 @@ pub use self::{ photo_size::PhotoSize, poll::{Poll, PollOption}, pre_checkout_query::PreCheckoutQuery, + reply_keyboard_markup::ReplyKeyboardMarkup, + reply_keyboard_remove::ReplyKeyboardRemove, + reply_markup::ReplyMarkup, response_parameters::ResponseParameters, send_invoice::SendInvoice, shipping_address::ShippingAddress, @@ -30,13 +37,6 @@ pub use self::{ successful_payment::SuccessfulPayment, user::User, video::Video, - reply_markup::ReplyMarkup, - force_reply::ForceReply, - inline_keyboard_button::InlineKeyboardButton, - inline_keyboard_markup::InlineKeyboardMarkup, - reply_keyboard_remove::ReplyKeyboardRemove, - reply_keyboard_markup::ReplyKeyboardMarkup, - keyboard_button::KeyboardButton, }; mod answer_pre_checkout_query; @@ -48,8 +48,8 @@ mod chat_permissions; mod chat_photo; mod document; mod force_reply; -mod inline_keyboard_markup; mod inline_keyboard_button; +mod inline_keyboard_markup; mod input_file; mod input_media; mod invoice; @@ -63,10 +63,10 @@ mod parse_mode; mod photo_size; mod poll; mod pre_checkout_query; -mod response_parameters; -mod reply_markup; mod reply_keyboard_markup; mod reply_keyboard_remove; +mod reply_markup; +mod response_parameters; mod send_invoice; mod shipping_address; mod shipping_option; diff --git a/src/core/types/parse_mode.rs b/src/core/types/parse_mode.rs index f50d1fc4..44d456de 100644 --- a/src/core/types/parse_mode.rs +++ b/src/core/types/parse_mode.rs @@ -69,7 +69,6 @@ pub enum ParseMode { Markdown, } - #[cfg(test)] mod tests { use super::*; diff --git a/src/core/types/poll.rs b/src/core/types/poll.rs index 160870c5..229e0d82 100644 --- a/src/core/types/poll.rs +++ b/src/core/types/poll.rs @@ -3,11 +3,11 @@ pub struct Poll { pub id: String, pub question: String, pub options: Vec, - pub is_closed: bool + pub is_closed: bool, } #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub struct PollOption { pub text: String, - pub voter_count: i32 + pub voter_count: i32, } diff --git a/src/core/types/reply_keyboard_markup.rs b/src/core/types/reply_keyboard_markup.rs index ddb73044..458c6c3a 100644 --- a/src/core/types/reply_keyboard_markup.rs +++ b/src/core/types/reply_keyboard_markup.rs @@ -3,23 +3,24 @@ use crate::core::types::KeyboardButton; /// This object represents a custom keyboard with reply options. #[derive(Debug, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)] pub struct ReplyKeyboardMarkup { - /// Array of button rows, each represented by an Array of [`KeyboardButton`] - /// objects + /// Array of button rows, each represented by an Array of + /// [`KeyboardButton`] objects pub keyboard: Vec>, #[serde(skip_serializing_if = "Option::is_none")] - /// Optional. Requests clients to resize the keyboard vertically for optimal - /// fit (e.g., make the keyboard smaller if there are just two rows of - /// buttons). Defaults to false, in which case the custom keyboard is always - /// of the same height as the app's standard keyboard. + /// Optional. Requests clients to resize the keyboard vertically for + /// optimal fit (e.g., make the keyboard smaller if there are just two + /// rows of buttons). Defaults to false, in which case the custom + /// keyboard is always of the same height as the app's standard + /// keyboard. pub resize_keyboard: Option, #[serde(skip_serializing_if = "Option::is_none")] /// Optional. Requests clients to hide the keyboard as soon as it's been /// used. The keyboard will still be available, but clients will /// automatically display the usual letter-keyboard in the chat – the user - /// can press a special button in the input field to see the custom keyboard - /// again. Defaults to false. + /// can press a special button in the input field to see the custom + /// keyboard again. Defaults to false. pub one_time_keyboard: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -32,4 +33,4 @@ pub struct ReplyKeyboardMarkup { /// the request with a keyboard to select the new language. Other users in /// the group don’t see the keyboard. pub selective: Option, -} \ No newline at end of file +} diff --git a/src/core/types/reply_keyboard_remove.rs b/src/core/types/reply_keyboard_remove.rs index 4797cde7..34190703 100644 --- a/src/core/types/reply_keyboard_remove.rs +++ b/src/core/types/reply_keyboard_remove.rs @@ -20,4 +20,4 @@ pub struct ReplyKeyboardRemove { /// the request with a keyboard to select the new language. Other users in /// the group don’t see the keyboard. pub selective: Option, -} \ No newline at end of file +} diff --git a/src/core/types/reply_markup.rs b/src/core/types/reply_markup.rs index 1c63a8e9..5da72b2e 100644 --- a/src/core/types/reply_markup.rs +++ b/src/core/types/reply_markup.rs @@ -1,8 +1,5 @@ use crate::core::types::{ - InlineKeyboardMarkup, - ReplyKeyboardMarkup, - ReplyKeyboardRemove, - ForceReply, + ForceReply, InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, }; #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/core/types/response_parameters.rs b/src/core/types/response_parameters.rs index 901d1492..c9b71243 100644 --- a/src/core/types/response_parameters.rs +++ b/src/core/types/response_parameters.rs @@ -12,9 +12,8 @@ mod tests { #[test] fn migrate_to_chat_id_deserialization() { let expected = ResponseParameters::MigrateToChatId(123456); - let actual: ResponseParameters = serde_json::from_str( - r#"{"migrate_to_chat_id":123456}"# - ).unwrap(); + let actual: ResponseParameters = + serde_json::from_str(r#"{"migrate_to_chat_id":123456}"#).unwrap(); assert_eq!(expected, actual); } @@ -22,10 +21,9 @@ mod tests { #[test] fn retry_after_deserialization() { let expected = ResponseParameters::RetryAfter(123456); - let actual: ResponseParameters = serde_json::from_str( - r#"{"retry_after":123456}"# - ).unwrap(); + let actual: ResponseParameters = + serde_json::from_str(r#"{"retry_after":123456}"#).unwrap(); assert_eq!(expected, actual); } -} \ No newline at end of file +} diff --git a/src/core/types/video.rs b/src/core/types/video.rs index bb10ac84..8860a372 100644 --- a/src/core/types/video.rs +++ b/src/core/types/video.rs @@ -1,6 +1,5 @@ use crate::core::types::PhotoSize; - /// This object represents a video file. #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)] pub struct Video {