Merge branch 'dev' into dev

This commit is contained in:
Andrew Andersen 2019-09-04 19:00:11 +03:00 committed by GitHub
commit 8be6c49b7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 97 additions and 64 deletions

View file

@ -1,3 +1,4 @@
format_code_in_doc_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 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 {
token: String,
@ -23,7 +20,7 @@ impl Bot {
pub fn with_client(token: &str, client: Client) -> Self {
Bot {
token: String::from(token),
client
client,
}
}
}
@ -31,14 +28,22 @@ impl Bot {
/// Telegram functions
impl Bot {
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
where C: Into<ChatId>, T: Into<String>
where
C: Into<ChatId>,
T: Into<String>,
{
SendMessage::new(
RequestInfo { token: &self.token, client: &self.client },
RequestInfo {
token: &self.token,
client: &self.client,
},
chat_id.into(),
text.into(),
)

View file

@ -21,7 +21,8 @@ impl FormBuilder {
Self {
form: self.form.text(
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 {
form: self.form.text(
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::requests::{Request, RequestFuture, RequestInfo, ResponseResult};
use crate::core::requests::{
Request, RequestFuture, RequestInfo, ResponseResult,
};
use crate::core::types::User;
#[derive(Debug)]
@ -12,7 +14,8 @@ impl<'a> Request<'a> for GetMe<'a> {
fn send(self) -> RequestFuture<'a, ResponseResult<Self::ReturnValue>> {
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::{ChatId, Request, RequestFuture, RequestInfo, ResponseResult};
use crate::core::requests::{
ChatId, Request, RequestFuture, RequestInfo, ResponseResult,
};
use crate::core::{network, types::Message};
#[derive(Debug)]
@ -9,7 +11,8 @@ pub struct SendMessage<'a> {
pub chat_id: ChatId,
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_notification: Option<bool>,
pub reply_to_message_id: Option<i64>,
@ -29,8 +32,14 @@ impl<'a> Request<'a> for SendMessage<'a> {
"disable_web_page_preview",
self.disable_web_page_preview.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())
.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();
network::request(
@ -45,7 +54,11 @@ impl<'a> Request<'a> for 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 {
info,
chat_id,

View file

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

View file

@ -12,10 +12,14 @@ impl serde::Serialize for InputFile {
{
match self {
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(
// 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),

View file

@ -15,8 +15,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
/// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or
/// inline URLs] in the media caption.
/// if you want Telegram apps to show [bold, italic, fixed-width text
/// or inline URLs] in the media caption.
///
/// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html
@ -31,8 +31,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")]
/// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size.
/// A thumbnails width and height should not exceed 320.
/// The thumbnail should be in JPEG format and less than 200 kB in
/// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File].
///
/// [InputFile::File]: crate::core::types::InputFile::File
@ -41,8 +41,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
/// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or
/// inline URLs] in the media caption.
/// if you want Telegram apps to show [bold, italic, fixed-width text
/// or inline URLs] in the media caption.
///
/// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html
@ -70,8 +70,8 @@ pub enum InputMedia {
media: InputFile,
/// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size.
/// A thumbnails width and height should not exceed 320.
/// The thumbnail should be in JPEG format and less than 200 kB in
/// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File].
///
/// [InputFile::File]: crate::core::types::InputFile::File
@ -81,8 +81,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
/// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or
/// inline URLs] in the media caption.
/// if you want Telegram apps to show [bold, italic, fixed-width text
/// or inline URLs] in the media caption.
///
/// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html
@ -106,8 +106,8 @@ pub enum InputMedia {
media: InputFile,
/// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size.
/// A thumbnails width and height should not exceed 320.
/// The thumbnail should be in JPEG format and less than 200 kB in
/// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File].
///
/// [InputFile::File]: crate::core::types::InputFile::File
@ -117,8 +117,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
/// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or
/// inline URLs] in the media caption.
/// if you want Telegram apps to show [bold, italic, fixed-width text
/// or inline URLs] in the media caption.
///
/// [Markdown]: crate::core::types::ParseMode::Markdown
/// [Html]: crate::core::types::ParseMode::Html
@ -142,8 +142,8 @@ pub enum InputMedia {
media: InputFile,
/// Thumbnail of the file sent; can be ignored if thumbnail generation
/// for the file is supported server-side.
/// The thumbnail should be in JPEG format and less than 200 kB in size.
/// A thumbnails width and height should not exceed 320.
/// The thumbnail should be in JPEG format and less than 200 kB in
/// size. A thumbnails width and height should not exceed 320.
/// Ignored if the file is not uploaded using [InputFile::File].
///
/// [InputFile::File]: crate::core::types::InputFile::File
@ -153,8 +153,8 @@ pub enum InputMedia {
#[serde(skip_serializing_if = "Option::is_none")]
caption: Option<String>,
/// Send [Markdown] or [HTML],
/// if you want Telegram apps to show [bold, italic, fixed-width text or
/// inline URLs] in the media caption.
/// if you want Telegram apps to show [bold, italic, fixed-width text
/// or inline URLs] in the media caption.
///
/// [Markdown]: crate::core::types::ParseMode::Markdown
/// [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.
#[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.
Venue {
@ -86,24 +86,26 @@ mod tests {
let text_content = InputMessageContent::Text {
message_text: String::from("text"),
parse_mode: None,
disable_web_page_preview: None
disable_web_page_preview: None,
};
let actual_json = serde_json::to_string(&text_content).unwrap();
assert_eq!(expected_json, actual_json);
}
#[test]
fn location_serialize() {
let expected_json = r#"{"latitude":59.08,"longitude":38.4326}"#;
let location_content = InputMessageContent::Location {
latitude: 59.08,
longitude: 38.4326,
live_period: None
live_period: None,
};
let actual_json = serde_json::to_string(&location_content).unwrap();
assert_eq!(expected_json, actual_json);
}
#[test]
fn venue_serialize() {
let expected_json = r#"{"latitude":59.08,"longitude":38.4326,"title":"some title",
@ -114,12 +116,13 @@ mod tests {
title: String::from("some title"),
address: String::from("some address"),
foursquare_id: None,
foursquare_type: None
foursquare_type: None,
};
let actual_json = serde_json::to_string(&venue_content).unwrap();
assert_eq!(expected_json, actual_json);
}
#[test]
fn contact_serialize() {
let expected_json = r#"{"phone_number":"+3800000000","first_name":"jhon"}"#;
@ -127,7 +130,7 @@ mod tests {
phone_number: String::from("+3800000000"),
first_name: String::from("jhon"),
last_name: None,
vcard: None
vcard: None,
};
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)]
/// This object represents a point on the map.

View file

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

View file

@ -1,5 +1,4 @@
use self::not_implemented_types::*;
pub use self::{
answer_pre_checkout_query::AnswerPreCheckoutQuery,
answer_shipping_query::AnswerShippingQuery,

View file

@ -1,6 +1,5 @@
use crate::core::types::Location;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Venue {
pub location: Location,
@ -9,5 +8,5 @@ pub struct Venue {
#[serde(skip_serializing_if = "Option::is_none")]
pub foursquare_id: Option<String>,
#[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
pub mime_type: Option<String>,
/// 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]
extern crate serde;
pub mod bot;
pub mod core;
pub mod bot;