Set the line width to 80 (rustfmt.toml)

This commit is contained in:
Temirkhan Myrzamadi 2019-09-04 20:07:56 +06:00
parent 8fe2db0259
commit 1ab1ae0224
17 changed files with 112 additions and 76 deletions

View file

@ -1,3 +1,4 @@
format_code_in_doc_comments = true format_code_in_doc_comments = true
wrap_comments = true wrap_comments = true
format_strings = true format_strings = true
max_width = 80

View file

@ -1,11 +1,8 @@
use reqwest::r#async::Client; use reqwest::r#async::Client;
use crate::core::requests::{
get_me::GetMe,
send_message::SendMessage,
RequestInfo,
ChatId,
};
use crate::core::requests::{
get_me::GetMe, send_message::SendMessage, ChatId, RequestInfo,
};
pub struct Bot { pub struct Bot {
token: String, token: String,
@ -23,7 +20,7 @@ impl Bot {
pub fn with_client(token: &str, client: Client) -> Self { pub fn with_client(token: &str, client: Client) -> Self {
Bot { Bot {
token: String::from(token), token: String::from(token),
client client,
} }
} }
} }
@ -31,14 +28,22 @@ impl Bot {
/// Telegram functions /// Telegram functions
impl Bot { impl Bot {
pub fn get_me(&self) -> GetMe { pub fn get_me(&self) -> GetMe {
GetMe::new(RequestInfo { token: &self.token, client: &self.client }) GetMe::new(RequestInfo {
token: &self.token,
client: &self.client,
})
} }
pub fn send_message<C, T>(&self, chat_id: C, text: T) -> SendMessage pub fn send_message<C, T>(&self, chat_id: C, text: T) -> SendMessage
where C: Into<ChatId>, T: Into<String> where
C: Into<ChatId>,
T: Into<String>,
{ {
SendMessage::new( SendMessage::new(
RequestInfo { token: &self.token, client: &self.client }, RequestInfo {
token: &self.token,
client: &self.client,
},
chat_id.into(), chat_id.into(),
text.into(), text.into(),
) )

View file

@ -21,7 +21,8 @@ impl FormBuilder {
Self { Self {
form: self.form.text( form: self.form.text(
name.to_owned(), name.to_owned(),
serde_json::to_string(value).expect("serde_json::to_string failed"), serde_json::to_string(value)
.expect("serde_json::to_string failed"),
), ),
} }
} }
@ -37,7 +38,8 @@ impl FormBuilder {
Some(value) => Self { Some(value) => Self {
form: self.form.text( form: self.form.text(
name.to_owned(), name.to_owned(),
serde_json::to_string(value).expect("serde_json::to_string failed"), serde_json::to_string(value)
.expect("serde_json::to_string failed"),
), ),
}, },
} }

View file

@ -1,5 +1,7 @@
use crate::core::network; use crate::core::network;
use crate::core::requests::{Request, RequestFuture, RequestInfo, ResponseResult}; use crate::core::requests::{
Request, RequestFuture, RequestInfo, ResponseResult,
};
use crate::core::types::User; use crate::core::types::User;
#[derive(Debug)] #[derive(Debug)]
@ -12,7 +14,8 @@ impl<'a> Request<'a> for GetMe<'a> {
fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> { fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
Box::pin(async move { Box::pin(async move {
network::request(self.info.client, self.info.token, "getMe", None).await network::request(self.info.client, self.info.token, "getMe", None)
.await
}) })
} }
} }

View file

@ -1,5 +1,7 @@
use crate::core::requests::form_builder::FormBuilder; use crate::core::requests::form_builder::FormBuilder;
use crate::core::requests::{ChatId, Request, RequestFuture, RequestInfo, ResponseResult}; use crate::core::requests::{
ChatId, Request, RequestFuture, RequestInfo, ResponseResult,
};
use crate::core::{network, types::Message}; use crate::core::{network, types::Message};
#[derive(Debug)] #[derive(Debug)]
@ -9,7 +11,8 @@ pub struct SendMessage<'a> {
pub chat_id: ChatId, pub chat_id: ChatId,
pub text: String, pub text: String,
pub parse_mode: Option<String>, // TODO: ParseMode enum pub parse_mode: Option<String>,
// TODO: ParseMode enum
pub disable_web_page_preview: Option<bool>, pub disable_web_page_preview: Option<bool>,
pub disable_notification: Option<bool>, pub disable_notification: Option<bool>,
pub reply_to_message_id: Option<i64>, pub reply_to_message_id: Option<i64>,
@ -29,8 +32,14 @@ impl<'a> Request<'a> for SendMessage<'a> {
"disable_web_page_preview", "disable_web_page_preview",
self.disable_web_page_preview.as_ref(), self.disable_web_page_preview.as_ref(),
) )
.add_if_some("disable_notification", self.disable_notification.as_ref()) .add_if_some(
.add_if_some("reply_to_message_id", self.reply_to_message_id.as_ref()) "disable_notification",
self.disable_notification.as_ref(),
)
.add_if_some(
"reply_to_message_id",
self.reply_to_message_id.as_ref(),
)
.build(); .build();
network::request( network::request(
@ -45,7 +54,11 @@ impl<'a> Request<'a> for SendMessage<'a> {
} }
impl<'a> SendMessage<'a> { impl<'a> SendMessage<'a> {
pub(crate) fn new(info: RequestInfo<'a>, chat_id: ChatId, text: String) -> Self { pub(crate) fn new(
info: RequestInfo<'a>,
chat_id: ChatId,
text: String,
) -> Self {
SendMessage { SendMessage {
info, info,
chat_id, chat_id,

View file

@ -59,10 +59,11 @@ fn test_chat_de() {
}, },
description: None, description: None,
invite_link: None, invite_link: None,
pinned_message: None pinned_message: None,
}, },
photo: None, photo: None,
}, },
from_str(r#"{"id":0,"type":"channel","username":"channelname"}"#).unwrap() from_str(r#"{"id":0,"type":"channel","username":"channelname"}"#)
.unwrap()
); );
} }

View file

@ -13,8 +13,8 @@ pub struct ChatMember {
///Optional. Administrators only. True, if the bot is allowed to edit ///Optional. Administrators only. True, if the bot is allowed to edit
/// administrator privileges of that user /// administrator privileges of that user
pub can_be_edited: Option<bool>, pub can_be_edited: Option<bool>,
///Optional. Administrators only. True, if the administrator can change the ///Optional. Administrators only. True, if the administrator can change
/// chat title, photo and other settings /// the chat title, photo and other settings
pub can_change_info: Option<bool>, pub can_change_info: Option<bool>,
///Optional. Administrators only. True, if the administrator can post in ///Optional. Administrators only. True, if the administrator can post in
/// the channel, channels only /// the channel, channels only
@ -25,8 +25,8 @@ pub struct ChatMember {
///Optional. Administrators only. True, if the administrator can delete ///Optional. Administrators only. True, if the administrator can delete
/// messages of other users /// messages of other users
pub can_delete_messages: Option<bool>, pub can_delete_messages: Option<bool>,
///Optional. Administrators only. True, if the administrator can invite new ///Optional. Administrators only. True, if the administrator can invite
/// users to the chat /// new users to the chat
pub can_invite_users: Option<bool>, pub can_invite_users: Option<bool>,
///Optional. Administrators only. True, if the administrator can restrict, ///Optional. Administrators only. True, if the administrator can restrict,
/// ban or unban chat members /// ban or unban chat members
@ -36,17 +36,19 @@ pub struct ChatMember {
pub can_pin_messages: Option<bool>, pub can_pin_messages: Option<bool>,
///Optional. Administrators only. True, if the administrator can add new ///Optional. Administrators only. True, if the administrator can add new
/// administrators with a subset of his own privileges or demote /// administrators with a subset of his own privileges or demote
/// administrators that he has promoted, directly or indirectly (promoted by /// administrators that he has promoted, directly or indirectly (promoted
/// administrators that were appointed by the user) /// by administrators that were appointed by the user)
pub can_promote_members: Option<bool>, pub can_promote_members: Option<bool>,
///Optional. Restricted only. True, if the user can send text messages, ///Optional. Restricted only. True, if the user can send text messages,
/// contacts, locations and venues /// contacts, locations and venues
pub can_send_messages: Option<bool>, pub can_send_messages: Option<bool>,
///Optional. Restricted only. True, if the user can send audios, documents, ///Optional. Restricted only. True, if the user can send audios,
/// photos, videos, video notes and voice notes, implies can_send_messages /// documents, photos, videos, video notes and voice notes, implies
/// can_send_messages
pub can_send_media_messages: Option<bool>, pub can_send_media_messages: Option<bool>,
///Optional. Restricted only. True, if the user can send animations, games, ///Optional. Restricted only. True, if the user can send animations,
/// stickers and use inline bots, implies can_send_media_messages /// games, stickers and use inline bots, implies
/// can_send_media_messages
pub can_send_other_messages: Option<bool>, pub can_send_other_messages: Option<bool>,
///Optional. Restricted only. True, if user may add web page previews to ///Optional. Restricted only. True, if user may add web page previews to
/// his messages, implies can_send_media_messages /// his messages, implies can_send_media_messages

View file

@ -12,10 +12,14 @@ impl serde::Serialize for InputFile {
{ {
match self { match self {
InputFile::File(path) => { InputFile::File(path) => {
// NOTE: file should be actually attached with multipart/form-data // NOTE: file should be actually attached with
// multipart/form-data
serializer.serialize_str( serializer.serialize_str(
// TODO: remove unwrap (?) // TODO: remove unwrap (?)
&format!("attach://{}", path.file_name().unwrap().to_string_lossy()), &format!(
"attach://{}",
path.file_name().unwrap().to_string_lossy()
),
) )
} }
InputFile::Url(url) => serializer.serialize_str(url), InputFile::Url(url) => serializer.serialize_str(url),

View file

@ -15,8 +15,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>, caption: Option<String>,
/// Send [Markdown] or [HTML], /// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or /// if you want Telegram apps to show [bold, italic, fixed-width text
/// inline URLs] in the media caption. /// or inline URLs] in the media caption.
/// ///
/// [Markdown]: crate::core::types::ParseMode::Markdown /// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html /// [Html]: crate::core::types::ParseMode::Html
@ -31,8 +31,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
/// Thumbnail of the file sent; can be ignored if thumbnail generation /// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side. /// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size. /// The thumbnail should be in JPEG format and less than 200 kB in
/// A thumbnails width and height should not exceed 320. /// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File]. /// Ignored if the file is not uploaded using [InputFile::File].
/// ///
/// [InputFile::File]: crate::core::types::InputFile::File /// [InputFile::File]: crate::core::types::InputFile::File
@ -41,8 +41,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>, caption: Option<String>,
/// Send [Markdown] or [HTML], /// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or /// if you want Telegram apps to show [bold, italic, fixed-width text
/// inline URLs] in the media caption. /// or inline URLs] in the media caption.
/// ///
/// [Markdown]: crate::core::types::ParseMode::Markdown /// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html /// [Html]: crate::core::types::ParseMode::Html
@ -70,8 +70,8 @@ pub enum InputMedia {
media: InputFile, media: InputFile,
/// Thumbnail of the file sent; can be ignored if thumbnail generation /// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side. /// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size. /// The thumbnail should be in JPEG format and less than 200 kB in
/// A thumbnails width and height should not exceed 320. /// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File]. /// Ignored if the file is not uploaded using [InputFile::File].
/// ///
/// [InputFile::File]: crate::core::types::InputFile::File /// [InputFile::File]: crate::core::types::InputFile::File
@ -81,8 +81,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>, caption: Option<String>,
/// Send [Markdown] or [HTML], /// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or /// if you want Telegram apps to show [bold, italic, fixed-width text
/// inline URLs] in the media caption. /// or inline URLs] in the media caption.
/// ///
/// [Markdown]: crate::core::types::ParseMode::Markdown /// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html /// [Html]: crate::core::types::ParseMode::Html
@ -106,8 +106,8 @@ pub enum InputMedia {
media: InputFile, media: InputFile,
/// Thumbnail of the file sent; can be ignored if thumbnail generation /// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side. /// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size. /// The thumbnail should be in JPEG format and less than 200 kB in
/// A thumbnails width and height should not exceed 320. /// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File]. /// Ignored if the file is not uploaded using [InputFile::File].
/// ///
/// [InputFile::File]: crate::core::types::InputFile::File /// [InputFile::File]: crate::core::types::InputFile::File
@ -117,8 +117,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>, caption: Option<String>,
/// Send [Markdown] or [HTML], /// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or /// if you want Telegram apps to show [bold, italic, fixed-width text
/// inline URLs] in the media caption. /// or inline URLs] in the media caption.
/// ///
/// [Markdown]: crate::core::types::ParseMode::Markdown /// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html /// [Html]: crate::core::types::ParseMode::Html
@ -142,8 +142,8 @@ pub enum InputMedia {
media: InputFile, media: InputFile,
/// Thumbnail of the file sent; can be ignored if thumbnail generation /// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side. /// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size. /// The thumbnail should be in JPEG format and less than 200 kB in
/// A thumbnails width and height should not exceed 320. /// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File]. /// Ignored if the file is not uploaded using [InputFile::File].
/// ///
/// [InputFile::File]: crate::core::types::InputFile::File /// [InputFile::File]: crate::core::types::InputFile::File
@ -153,8 +153,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>, caption: Option<String>,
/// Send [Markdown] or [HTML], /// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or /// if you want Telegram apps to show [bold, italic, fixed-width text
/// inline URLs] in the media caption. /// or inline URLs] in the media caption.
/// ///
/// [Markdown]: crate::core::types::ParseMode::Markdown /// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html /// [Html]: crate::core::types::ParseMode::Html

View file

@ -36,7 +36,7 @@ pub enum InputMessageContent {
/// Period in seconds for which the location can be updated, should be between 60 and 86400. /// Period in seconds for which the location can be updated, should be between 60 and 86400.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
live_period: Option<u32> live_period: Option<u32>,
}, },
/// Represents the content of a venue message to be sent as the result of an inline query. /// Represents the content of a venue message to be sent as the result of an inline query.
Venue { Venue {
@ -86,24 +86,26 @@ mod tests {
let text_content = InputMessageContent::Text { let text_content = InputMessageContent::Text {
message_text: String::from("text"), message_text: String::from("text"),
parse_mode: None, parse_mode: None,
disable_web_page_preview: None disable_web_page_preview: None,
}; };
let actual_json = serde_json::to_string(&text_content).unwrap(); let actual_json = serde_json::to_string(&text_content).unwrap();
assert_eq!(expected_json, actual_json); assert_eq!(expected_json, actual_json);
} }
#[test] #[test]
fn location_serialize() { fn location_serialize() {
let expected_json = r#"{"latitude":59.08,"longitude":38.4326}"#; let expected_json = r#"{"latitude":59.08,"longitude":38.4326}"#;
let location_content = InputMessageContent::Location { let location_content = InputMessageContent::Location {
latitude: 59.08, latitude: 59.08,
longitude: 38.4326, longitude: 38.4326,
live_period: None live_period: None,
}; };
let actual_json = serde_json::to_string(&location_content).unwrap(); let actual_json = serde_json::to_string(&location_content).unwrap();
assert_eq!(expected_json, actual_json); assert_eq!(expected_json, actual_json);
} }
#[test] #[test]
fn venue_serialize() { fn venue_serialize() {
let expected_json = r#"{"latitude":59.08,"longitude":38.4326,"title":"some title", let expected_json = r#"{"latitude":59.08,"longitude":38.4326,"title":"some title",
@ -114,12 +116,13 @@ mod tests {
title: String::from("some title"), title: String::from("some title"),
address: String::from("some address"), address: String::from("some address"),
foursquare_id: None, foursquare_id: None,
foursquare_type: None foursquare_type: None,
}; };
let actual_json = serde_json::to_string(&venue_content).unwrap(); let actual_json = serde_json::to_string(&venue_content).unwrap();
assert_eq!(expected_json, actual_json); assert_eq!(expected_json, actual_json);
} }
#[test] #[test]
fn contact_serialize() { fn contact_serialize() {
let expected_json = r#"{"phone_number":"+3800000000","first_name":"jhon"}"#; let expected_json = r#"{"phone_number":"+3800000000","first_name":"jhon"}"#;
@ -127,7 +130,7 @@ mod tests {
phone_number: String::from("+3800000000"), phone_number: String::from("+3800000000"),
first_name: String::from("jhon"), first_name: String::from("jhon"),
last_name: None, last_name: None,
vcard: None vcard: None,
}; };
let actual_json = serde_json::to_string(&contact_content).unwrap(); let actual_json = serde_json::to_string(&contact_content).unwrap();

View file

@ -1,4 +1,4 @@
use serde::{Serialization, Deserialization}; use serde::{Deserialization, Serialization};
#[derive(Debug, Serialization, Deserialization)] #[derive(Debug, Serialization, Deserialization)]
/// This object represents a point on the map. /// This object represents a point on the map.

View file

@ -1,7 +1,7 @@
use crate::core::types::{ use crate::core::types::{
Animation, Audio, Chat, Contact, Document, Game, InlineKeyboardMarkup, Invoice, Location, Animation, Audio, Chat, Contact, Document, Game, InlineKeyboardMarkup,
MessageEntity, PassportData, PhotoSize, Poll, Sticker, SuccessfulPayment, User, Venue, Video, Invoice, Location, MessageEntity, PassportData, PhotoSize, Poll, Sticker,
VideoNote, Voice, SuccessfulPayment, User, Venue, Video, VideoNote, Voice,
}; };
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq)]

View file

@ -37,9 +37,11 @@ fn recursive_kind() {
url: "ya.ru".into() url: "ya.ru".into()
}, },
offset: 1, offset: 1,
length: 2 length: 2,
}, },
from_str::<MessageEntity>(r#"{"type":"text_link","url":"ya.ru","offset":1,"length":2}"#) from_str::<MessageEntity>(
r#"{"type":"text_link","url":"ya.ru","offset":1,"length":2}"#
)
.unwrap() .unwrap()
); );
} }

View file

@ -1,15 +1,16 @@
use self::not_implemented_types::*; use self::not_implemented_types::*;
pub use self::{ pub use self::{
answer_pre_checkout_query::AnswerPreCheckoutQuery, answer_shipping_query::AnswerShippingQuery, answer_pre_checkout_query::AnswerPreCheckoutQuery,
audio::Audio, chat::Chat, chat_member::ChatMember, chat_permissions::ChatPermissions, answer_shipping_query::AnswerShippingQuery, audio::Audio, chat::Chat,
chat_photo::ChatPhoto, document::Document, input_file::InputFile, input_media::InputMedia, chat_member::ChatMember, chat_permissions::ChatPermissions,
invoice::Invoice, label_price::LabeledPrice, message::Message, message_entity::MessageEntity, chat_photo::ChatPhoto, document::Document, input_file::InputFile,
order_info::OrderInfo, parse_mode::ParseMode, photo_size::PhotoSize, input_media::InputMedia, invoice::Invoice, label_price::LabeledPrice,
message::Message, message_entity::MessageEntity, order_info::OrderInfo,
parse_mode::ParseMode, photo_size::PhotoSize,
pre_checkout_query::PreCheckoutQuery, send_invoice::SendInvoice, pre_checkout_query::PreCheckoutQuery, send_invoice::SendInvoice,
shipping_address::ShippingAddress, shipping_option::ShippingOption, shipping_address::ShippingAddress, shipping_option::ShippingOption,
shipping_query::ShippingQuery, sticker::Sticker, successful_payment::SuccessfulPayment, shipping_query::ShippingQuery, sticker::Sticker,
user::User, video::Video, successful_payment::SuccessfulPayment, user::User, video::Video,
}; };
mod answer_pre_checkout_query; mod answer_pre_checkout_query;

View file

@ -1,6 +1,5 @@
use crate::core::types::Location; use crate::core::types::Location;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Venue { pub struct Venue {
pub location: Location, pub location: Location,
@ -9,5 +8,5 @@ pub struct Venue {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub foursquare_id: Option<String>, pub foursquare_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub foursquare_type: Option<String> pub foursquare_type: Option<String>,
} }

View file

@ -10,5 +10,5 @@ struct Voice {
/// Optional. MIME type of the file as defined by sender /// Optional. MIME type of the file as defined by sender
pub mime_type: Option<String>, pub mime_type: Option<String>,
/// Optional. File size /// Optional. File size
pub file_size: Option<u64> pub file_size: Option<u64>,
} }

View file

@ -3,5 +3,5 @@ extern crate derive_more;
#[macro_use] #[macro_use]
extern crate serde; extern crate serde;
pub mod core;
pub mod bot; pub mod bot;
pub mod core;