Merge pull request #13 from Mr-Andersen/dev

Much
This commit is contained in:
Andrew Andersen 2019-09-12 18:50:23 +03:00 committed by GitHub
commit df4259bf60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 227 additions and 260 deletions

View file

@ -0,0 +1,13 @@
use crate::core::types::PhotoSize;
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Animation {
pub file_id: String,
pub width: u32,
pub height: u32,
pub duration: u32,
pub thumb: PhotoSize,
pub file_name: Option<String>,
pub mime_type: Option<String>,
pub file_size: Option<u32>
}

View file

@ -1,8 +1,6 @@
use serde::Deserialization;
#[derive(Debug, Deserialization, Clone)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
/// This object represents a phone contact.
struct Contact {
pub struct Contact {
/// Contact's phone number
pub phone_number: String,
/// Contact's first name

6
src/core/types/file.rs Normal file
View file

@ -0,0 +1,6 @@
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)]
pub struct File {
pub file_id: String,
pub file_size: u32,
pub file_path: String
}

View file

@ -1,8 +1,6 @@
use serde::{Deserialization, Serialization};
#[derive(Debug, Serialization, Deserialization, Clone)]
/// This object represents a point on the map.
struct Location {
pub struct Location {
/// Longitude as defined by sender
pub longitude: f64,
/// Latitude as defined by sender

View file

@ -4,7 +4,7 @@ use crate::core::types::{
SuccessfulPayment, User, Venue, Video, VideoNote, Voice,
};
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Message {
#[serde(rename = "message_id")]
pub id: i64,
@ -14,7 +14,7 @@ pub struct Message {
pub message_kind: MessageKind,
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(untagged)]
pub enum MessageKind {
IncomingMessage {
@ -72,7 +72,7 @@ pub enum MessageKind {
},
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Sender {
/// If message is sent from Chat
#[serde(rename = "from")]
@ -82,7 +82,7 @@ pub enum Sender {
Signature(String),
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(untagged)]
pub enum ForwardKind {
ChannelForward {
@ -106,7 +106,7 @@ pub enum ForwardKind {
},
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum ForwardedFrom {
#[serde(rename = "forward_from")]
User(User),
@ -114,7 +114,7 @@ pub enum ForwardedFrom {
SenderName(String),
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(untagged)]
pub enum MediaKind {
Animation {
@ -193,232 +193,192 @@ mod tests {
use serde_json::from_str;
#[test]
fn sent_message_de() {
let expected = Message {
id: 6534,
date: 1567898953,
chat: Chat {
id: 218485655,
photo: None,
kind: ChatKind::Private {
type_: (),
first_name: Some("W".to_string()),
last_name: None,
username: Some("WaffleLapkin".to_string()),
},
},
message_kind: MessageKind::IncomingMessage {
from: Sender::User(User {
id: 457569668,
is_bot: true,
first_name: "BT".to_string(),
last_name: None,
username: Some("BloodyTestBot".to_string()),
language_code: None,
}),
forward_kind: ForwardKind::Origin {
reply_to_message: None,
},
edit_date: None,
media_kind: MediaKind::Text {
text: "text".to_string(),
entities: vec![],
},
reply_markup: None,
},
};
// actual message from telegram
fn de_media_forwarded() {
let json = r#"{
"message_id": 6534,
"from": {
"id": 457569668,
"is_bot": true,
"first_name": "BT",
"username": "BloodyTestBot"
},
"chat": {
"id": 218485655,
"first_name": "W",
"username": "WaffleLapkin",
"type": "private"
},
"date": 1567898953,
"text": "text"
}"#;
let actual = from_str::<Message>(json).unwrap();
assert_eq!(expected, actual);
"message_id": 198283,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1567927221,
"video": {
"duration": 13,
"width": 512,
"height": 640,
"mime_type": "video/mp4",
"thumb": {
"file_id": "AAQCAAOmBAACBf2oS53pByA-I4CWWCObDwAEAQAHbQADMWcAAhYE",
"file_size": 10339,
"width": 256,
"height": 320
},
"file_id": "BAADAgADpgQAAgX9qEud6QcgPiOAlhYE",
"file_size": 1381334
}
}"#;
let message = from_str::<Message>(json);
assert!(message.is_ok());
}
#[test]
fn media_message_de() {
fn de_media_group_forwarded() {
let json = r#"{
"message_id": 198283,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1567927221,
"video": {
"duration": 13,
"width": 512,
"height": 640,
"mime_type": "video/mp4",
"thumb": {
"file_id": "AAQCAAOmBAACBf2oS53pByA-I4CWWCObDwAEAQAHbQADMWcAAhYE",
"file_size": 10339,
"width": 256,
"height": 320
},
"file_id": "BAADAgADpgQAAgX9qEud6QcgPiOAlhYE",
"file_size": 1381334
}
}"#;
let actual = from_str::<Message>(json).unwrap();
let expected = Message {
id: 198283,
date: 1567927221,
chat: Chat {
id: 250918540,
photo: None,
kind: ChatKind::Private {
first_name: Some("Андрей".to_string()),
last_name: Some("Власов".to_string()),
username: Some("aka_dude".to_string()),
type_: ()
}
"message_id": 198283,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1567927221,
"media_group_id": "12543417770506682",
"video": {
"duration": 13,
"width": 512,
"height": 640,
"mime_type": "video/mp4",
"thumb": {
"file_id": "AAQCAAOmBAACBf2oS53pByA-I4CWWCObDwAEAQAHbQADMWcAAhYE",
"file_size": 10339,
"width": 256,
"height": 320
},
message_kind: MessageKind::IncomingMessage {
from: Sender::User(User {
id: 250918540,
is_bot: false,
first_name: "Андрей".to_string(),
last_name: Some("Власов".to_string()),
username: Some("aka_dude".to_string()),
language_code: Some("en".to_string())
}),
forward_kind: ForwardKind::Origin { reply_to_message: None },
edit_date: None,
media_kind: MediaKind::Video {
video: Video {
duration: 13,
width: 512,
height: 640,
mime_type: Some("video/mp4".to_string()),
thumb: Some(PhotoSize {
file_id: "AAQCAAOmBAACBf2oS53pByA-I4CWWCObDwAEAQAHbQADMWcAAhYE".to_string(),
file_size: Some(10339),
width: 256,
height: 320
}),
file_id: "BAADAgADpgQAAgX9qEud6QcgPiOAlhYE".to_string(),
file_size: Some(1381334)
},
caption: None,
caption_entities: vec![],
media_group_id: None
},
reply_markup: None
},
};
assert_eq!(actual, expected);
"file_id": "BAADAgADpgQAAgX9qEud6QcgPiOAlhYE",
"file_size": 1381334
}
}"#;
let message = from_str::<Message>(json);
assert!(message.is_ok());
}
#[test]
fn media_group_message_de() {
fn de_text() {
let json = r#"{
"message_id": 198283,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1567927221,
"media_group_id": "12543417770506682",
"video": {
"duration": 13,
"width": 512,
"height": 640,
"mime_type": "video/mp4",
"thumb": {
"file_id": "AAQCAAOmBAACBf2oS53pByA-I4CWWCObDwAEAQAHbQADMWcAAhYE",
"file_size": 10339,
"width": 256,
"height": 320
},
"file_id": "BAADAgADpgQAAgX9qEud6QcgPiOAlhYE",
"file_size": 1381334
}
}"#;
let actual = from_str::<Message>(json).unwrap();
let expected = Message {
id: 198283,
date: 1567927221,
chat: Chat {
id: 250918540,
photo: None,
kind: ChatKind::Private {
first_name: Some("Андрей".to_string()),
last_name: Some("Власов".to_string()),
username: Some("aka_dude".to_string()),
type_: ()
}
},
message_kind: MessageKind::IncomingMessage {
from: Sender::User(User {
id: 250918540,
is_bot: false,
first_name: "Андрей".to_string(),
last_name: Some("Власов".to_string()),
username: Some("aka_dude".to_string()),
language_code: Some("en".to_string())
}),
forward_kind: ForwardKind::Origin { reply_to_message: None },
edit_date: None,
media_kind: MediaKind::Video {
video: Video {
duration: 13,
width: 512,
height: 640,
mime_type: Some("video/mp4".to_string()),
thumb: Some(PhotoSize {
file_id: "AAQCAAOmBAACBf2oS53pByA-I4CWWCObDwAEAQAHbQADMWcAAhYE".to_string(),
file_size: Some(10339),
width: 256,
height: 320
}),
file_id: "BAADAgADpgQAAgX9qEud6QcgPiOAlhYE".to_string(),
file_size: Some(1381334)
},
caption: None,
caption_entities: vec![],
media_group_id: Some("12543417770506682".to_string())
},
reply_markup: None
},
};
assert_eq!(actual, expected);
"message_id": 199785,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1568289890,
"text": "Лол кек 😂"
}"#;
let message = from_str::<Message>(json);
assert!(message.is_ok());
}
#[test]
fn de_sticker() {
let json = r#"{
"message_id": 199787,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1568290188,
"sticker": {
"width": 512,
"height": 512,
"emoji": "😡",
"set_name": "AdvenTimeAnim",
"is_animated": true,
"thumb": {
"file_id": "AAQCAAMjAAOw0PgMaabKAcaXKCBLubkPAAQBAAdtAAPGKwACFgQ",
"file_size": 4118,
"width": 128,
"height": 128
},
"file_id": "CAADAgADIwADsND4DGmmygHGlyggFgQ",
"file_size": 16639
}
}"#;
let message = from_str::<Message>(json);
assert!(message.is_ok());
}
#[test]
fn de_image() {
let json = r#"{
"message_id": 199791,
"from": {
"id": 250918540,
"is_bot": false,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"language_code": "en"
},
"chat": {
"id": 250918540,
"first_name": "Андрей",
"last_name": "Власов",
"username": "aka_dude",
"type": "private"
},
"date": 1568290622,
"photo": [
{
"file_id": "AgADAgAD36sxG-PX0UvQSXIn9rccdw-ACA4ABAEAAwIAA20AAybcBAABFgQ",
"file_size": 18188,
"width": 320,
"height": 239
},
{
"file_id": "AgADAgAD36sxG-PX0UvQSXIn9rccdw-ACA4ABAEAAwIAA3gAAyfcBAABFgQ",
"file_size": 62123,
"width": 800,
"height": 598
},
{
"file_id": "AgADAgAD36sxG-PX0UvQSXIn9rccdw-ACA4ABAEAAwIAA3kAAyTcBAABFgQ",
"file_size": 75245,
"width": 962,
"height": 719
}
]
}"#;
let message = from_str::<Message>(json);
assert!(message.is_ok());
}
}

View file

@ -19,6 +19,7 @@ pub use self::{
order_info::OrderInfo,
parse_mode::ParseMode,
photo_size::PhotoSize,
poll::{Poll, PollOption},
pre_checkout_query::PreCheckoutQuery,
response_parameters::ResponseParameters,
send_invoice::SendInvoice,
@ -60,6 +61,7 @@ mod not_implemented_types;
mod order_info;
mod parse_mode;
mod photo_size;
mod poll;
mod pre_checkout_query;
mod response_parameters;
mod reply_markup;

View file

@ -1,35 +1,5 @@
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Location;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct InlineKeyboardMarkup;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct PassportData;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Poll;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Animation;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Game;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Contact;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct VideoNote;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Venue;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct Voice;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct MaskPosition;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)]
pub struct ChatMemberStatus;

13
src/core/types/poll.rs Normal file
View file

@ -0,0 +1,13 @@
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct Poll {
pub id: String,
pub question: String,
pub options: Vec<PollOption>,
pub is_closed: bool
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct PollOption {
pub text: String,
pub voter_count: i32
}

View file

@ -0,0 +1,7 @@
use crate::core::types::PhotoSize;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
pub struct UserProfilePhotos {
pub total_count: u32,
pub photos: Vec<Vec<PhotoSize>>
}