Merge pull request #12 from Mr-Andersen/dev

Fixed bug
This commit is contained in:
Waffle Lapkin 2019-09-08 19:30:38 +03:00 committed by GitHub
commit 0abc9e66ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 247 additions and 67 deletions

View file

@ -1,7 +1,7 @@
use crate::core::types::{ChatPermissions, ChatPhoto, Message}; use crate::core::types::{ChatPermissions, ChatPhoto, Message};
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
pub struct Chat { pub struct Chat {
pub id: i64, pub id: i64,
#[serde(flatten)] #[serde(flatten)]
@ -10,7 +10,7 @@ pub struct Chat {
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum ChatKind { pub enum ChatKind {
NonPrivate { NonPrivate {
@ -33,7 +33,7 @@ pub enum ChatKind {
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum NonPrivateChatKind { pub enum NonPrivateChatKind {

View file

@ -1,6 +1,6 @@
use crate::core::types::PhotoSize; use crate::core::types::PhotoSize;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
pub struct Document { pub struct Document {
pub file_id: String, pub file_id: String,
pub thumb: Option<PhotoSize>, pub thumb: Option<PhotoSize>,

View file

@ -1,4 +1,4 @@
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Serialize)]
pub struct Invoice { pub struct Invoice {
pub title: String, pub title: String,
pub description: String, pub description: String,

View file

@ -4,8 +4,7 @@ use crate::core::types::{
SuccessfulPayment, User, Venue, Video, VideoNote, Voice, SuccessfulPayment, User, Venue, Video, VideoNote, Voice,
}; };
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)]
pub struct Message { pub struct Message {
#[serde(rename = "message_id")] #[serde(rename = "message_id")]
pub id: i64, pub id: i64,
@ -15,11 +14,12 @@ pub struct Message {
pub message_kind: MessageKind, pub message_kind: MessageKind,
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)]
#[serde(untagged)] #[serde(untagged)]
pub enum MessageKind { pub enum MessageKind {
IncomingMessage { IncomingMessage {
#[serde(flatten)]
from: Sender,
#[serde(flatten)] #[serde(flatten)]
forward_kind: ForwardKind, forward_kind: ForwardKind,
edit_date: Option<i32>, edit_date: Option<i32>,
@ -72,7 +72,17 @@ pub enum MessageKind {
}, },
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
pub enum Sender {
/// If message is sent from Chat
#[serde(rename = "from")]
User(User),
/// If message is sent from Channel
#[serde(rename = "author_signature")]
Signature(String),
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum ForwardKind { pub enum ForwardKind {
ChannelForward { ChannelForward {
@ -96,7 +106,7 @@ pub enum ForwardKind {
}, },
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
pub enum ForwardedFrom { pub enum ForwardedFrom {
#[serde(rename = "forward_from")] #[serde(rename = "forward_from")]
User(User), User(User),
@ -104,16 +114,24 @@ pub enum ForwardedFrom {
SenderName(String), SenderName(String),
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum MediaKind { pub enum MediaKind {
Animation { Animation {
animation: Animation, animation: Animation,
#[doc(hidden)]
/// "For backward compatibility" (c) Telegram Docs
#[serde(skip)]
document: (),
caption: Option<String>, caption: Option<String>,
#[serde(default = "Vec::new")]
caption_entities: Vec<MessageEntity>
}, },
Audio { Audio {
audio: Audio, audio: Audio,
caption: Option<String>, caption: Option<String>,
#[serde(default = "Vec::new")]
caption_entities: Vec<MessageEntity>
}, },
Contact { Contact {
contact: Contact, contact: Contact,
@ -121,6 +139,8 @@ pub enum MediaKind {
Document { Document {
document: Document, document: Document,
caption: Option<String>, caption: Option<String>,
#[serde(default = "Vec::new")]
caption_entities: Vec<MessageEntity>
}, },
Game { Game {
game: Game, game: Game,
@ -129,8 +149,11 @@ pub enum MediaKind {
location: Location, location: Location,
}, },
Photo { Photo {
sizes: Vec<PhotoSize>, photo: Vec<PhotoSize>,
caption: Option<String>, caption: Option<String>,
#[serde(default = "Vec::new")]
caption_entities: Vec<MessageEntity>,
media_group_id: Option<String>,
}, },
Poll { Poll {
poll: Poll, poll: Poll,
@ -140,11 +163,15 @@ pub enum MediaKind {
}, },
Text { Text {
text: String, text: String,
#[serde(default = "Vec::new")]
entities: Vec<MessageEntity>, entities: Vec<MessageEntity>,
}, },
Video { Video {
video: Video, video: Video,
caption: Option<String>, caption: Option<String>,
#[serde(default = "Vec::new")]
caption_entities: Vec<MessageEntity>,
media_group_id: Option<String>,
}, },
VideoNote { VideoNote {
video_note: VideoNote, video_note: VideoNote,
@ -152,6 +179,8 @@ pub enum MediaKind {
Voice { Voice {
voice: Voice, voice: Voice,
caption: Option<String>, caption: Option<String>,
#[serde(default = "Vec::new")]
caption_entities: Vec<MessageEntity>,
}, },
Venue { Venue {
venue: Venue, venue: Venue,
@ -164,83 +193,232 @@ mod tests {
use serde_json::from_str; use serde_json::from_str;
#[test] #[test]
fn origin_de() { fn sent_message_de() {
let expected = Message { let expected = Message {
id: 0, id: 6534,
date: 0, date: 1567898953,
chat: Chat { chat: Chat {
id: 0, id: 218485655,
photo: None,
kind: ChatKind::Private { kind: ChatKind::Private {
type_: (), type_: (),
username: None, first_name: Some("W".to_string()),
first_name: None,
last_name: None, last_name: None,
username: Some("WaffleLapkin".to_string()),
}, },
photo: None,
}, },
message_kind: MessageKind::IncomingMessage { 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 { forward_kind: ForwardKind::Origin {
reply_to_message: None, reply_to_message: None,
}, },
edit_date: None, edit_date: None,
media_kind: MediaKind::Text { media_kind: MediaKind::Text {
text: "Hello".to_string(), text: "text".to_string(),
entities: vec![], entities: vec![],
}, },
reply_markup: None, reply_markup: None,
}, },
}; };
let actual = from_str::<Message>(r#"{"message_id":0,"date":0,"chat":{"id":0,"type":"private"},"text":"Hello","entities":[]}"#).unwrap(); // actual message from telegram
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); assert_eq!(expected, actual);
} }
#[test] #[test]
fn forward_de() { fn media_message_de() {
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 { let expected = Message {
id: 1, id: 198283,
date: 1, date: 1567927221,
chat: Chat { chat: Chat {
id: 1, id: 250918540,
kind: ChatKind::Private {
type_: (),
username: None,
first_name: None,
last_name: None,
},
photo: None, 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 { message_kind: MessageKind::IncomingMessage {
forward_kind: ForwardKind::NonChannelForward { from: Sender::User(User {
date: 1, id: 250918540,
from: ForwardedFrom::User(User { is_bot: false,
id: 123, first_name: "Андрей".to_string(),
is_bot: false, last_name: Some("Власов".to_string()),
first_name: "Name".to_string(), username: Some("aka_dude".to_string()),
last_name: None, language_code: Some("en".to_string())
username: None, }),
language_code: None, forward_kind: ForwardKind::Origin { reply_to_message: None },
}),
},
edit_date: None, edit_date: None,
media_kind: MediaKind::Text { media_kind: MediaKind::Video {
text: "Message".into(), video: Video {
entities: vec![], 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, reply_markup: None
}, },
}; };
let actual = from_str::<Message>( assert_eq!(actual, expected);
r#"{"message_id":1,"date":1,"chat":{"id":1,"type":"private"},"forward_date":1,"forward_from":{"id":123,"is_bot":false,"first_name":"Name"},"text":"Message","entities":[]}"#,
)
.unwrap();
assert_eq!(expected, actual);
} }
#[test] #[test]
fn sent_message_de() { fn media_group_message_de() {
// actual message from telegram let json = r#"{
let json = "{\"message_id\":6534,\"from\":{\"id\":457569668,\"is_bot\":true,\"first_name\":\"\\u0424\\u044b\\u0440\\u044c\\u043a\",\"username\":\"BloodyTestBot\"},\"chat\":{\"id\":218485655,\"first_name\":\"\\u0412\\u0430\\u0444\\u0435\\u043b\\u044c\",\"username\":\"WaffleLapkin\",\"type\":\"private\"},\"date\":1567898953,\"text\":\"text\"}"; "message_id": 198283,
let actual: Result<Message, _>= from_str(json); "from": {
assert!(actual.is_ok()); "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);
} }
} }

View file

@ -1,6 +1,6 @@
use crate::core::types::User; use crate::core::types::User;
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
pub struct MessageEntity { pub struct MessageEntity {
#[serde(flatten)] #[serde(flatten)]
pub kind: MessageEntityKind, pub kind: MessageEntityKind,
@ -8,7 +8,7 @@ pub struct MessageEntity {
pub length: usize, pub length: usize,
} }
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum MessageEntityKind { pub enum MessageEntityKind {

View file

@ -12,12 +12,15 @@ pub use self::{
input_media::InputMedia, input_media::InputMedia,
invoice::Invoice, invoice::Invoice,
label_price::LabeledPrice, label_price::LabeledPrice,
message::{ForwardKind, ForwardedFrom, MediaKind, Message, MessageKind}, message::{
ForwardKind, ForwardedFrom, MediaKind, Message, MessageKind, Sender,
},
message_entity::MessageEntity, message_entity::MessageEntity,
order_info::OrderInfo, order_info::OrderInfo,
parse_mode::ParseMode, parse_mode::ParseMode,
photo_size::PhotoSize, photo_size::PhotoSize,
pre_checkout_query::PreCheckoutQuery, pre_checkout_query::PreCheckoutQuery,
response_parameters::ResponseParameters,
send_invoice::SendInvoice, send_invoice::SendInvoice,
shipping_address::ShippingAddress, shipping_address::ShippingAddress,
shipping_option::ShippingOption, shipping_option::ShippingOption,
@ -26,7 +29,6 @@ pub use self::{
successful_payment::SuccessfulPayment, successful_payment::SuccessfulPayment,
user::User, user::User,
video::Video, video::Video,
response_parameters::ResponseParameters
}; };
mod answer_pre_checkout_query; mod answer_pre_checkout_query;
@ -48,6 +50,7 @@ mod order_info;
mod parse_mode; mod parse_mode;
mod photo_size; mod photo_size;
mod pre_checkout_query; mod pre_checkout_query;
mod response_parameters;
mod send_invoice; mod send_invoice;
mod shipping_address; mod shipping_address;
mod shipping_option; mod shipping_option;
@ -56,4 +59,3 @@ mod sticker;
mod successful_payment; mod successful_payment;
mod user; mod user;
mod video; mod video;
mod response_parameters;

View file

@ -1,6 +1,6 @@
use crate::core::types::ShippingAddress; use crate::core::types::ShippingAddress;
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Serialize)]
pub struct OrderInfo { pub struct OrderInfo {
pub name: String, pub name: String,
pub phone_number: String, pub phone_number: String,

View file

@ -1,4 +1,4 @@
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Serialize)]
pub struct ShippingAddress { pub struct ShippingAddress {
pub country_code: String, pub country_code: String,
pub state: String, pub state: String,

View file

@ -1,6 +1,6 @@
use crate::core::types::{MaskPosition, PhotoSize}; use crate::core::types::{MaskPosition, PhotoSize};
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Serialize)]
pub struct Sticker { pub struct Sticker {
pub file_id: String, pub file_id: String,
pub width: u16, pub width: u16,

View file

@ -1,6 +1,6 @@
use crate::core::types::OrderInfo; use crate::core::types::OrderInfo;
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Serialize)]
pub struct SuccessfulPayment { pub struct SuccessfulPayment {
pub currency: String, pub currency: String,
pub total_amount: i64, pub total_amount: i64,

View file

@ -1,4 +1,4 @@
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Serialize)]
pub struct User { pub struct User {
pub id: i64, pub id: i64,
pub is_bot: bool, pub is_bot: bool,