mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Added Serialization for Chat
This commit is contained in:
parent
b254de2564
commit
05899b53d2
5 changed files with 80 additions and 65 deletions
|
@ -1,11 +1,12 @@
|
||||||
use crate::core::types::{ChatPermissions, ChatPhoto, Message};
|
use crate::core::types::{ChatPermissions, ChatPhoto, Message};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
|
||||||
pub struct Chat {
|
pub struct Chat {
|
||||||
#[serde(rename = "chat_id")]
|
#[serde(rename = "chat_id")]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub type_: ChatType,
|
pub type_: ChatType,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub photo: Option<ChatPhoto>,
|
pub photo: Option<ChatPhoto>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,30 +39,45 @@ fn assert_private_field<'de, D: serde::Deserializer<'de>>(
|
||||||
des.deserialize_str(PrivateChatTypeVisitor)
|
des.deserialize_str(PrivateChatTypeVisitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
fn serialize_private_field<S: serde::Serializer>(
|
||||||
|
_: &(),
|
||||||
|
ser: S,
|
||||||
|
) -> Result<S::Ok, S::Error> {
|
||||||
|
ser.serialize_str("private")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum ChatType {
|
pub enum ChatType {
|
||||||
NotPrivate {
|
NotPrivate {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
title: Option<String>,
|
title: Option<String>,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
type_: NotPrivateChatType,
|
type_: NotPrivateChatType,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
invite_link: Option<String>,
|
invite_link: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pinned_message: Option<Box<Message>>,
|
pinned_message: Option<Box<Message>>,
|
||||||
},
|
},
|
||||||
Private {
|
Private {
|
||||||
/// Dummy field. Used to ensure that "type" field is equal to "private"
|
/// Dummy field. Used to ensure that "type" field is equal to "private"
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
#[serde(deserialize_with = "assert_private_field")]
|
#[serde(deserialize_with = "assert_private_field")]
|
||||||
|
#[serde(serialize_with = "serialize_private_field")]
|
||||||
type_: (),
|
type_: (),
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
username: Option<String>,
|
username: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
first_name: Option<String>,
|
first_name: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
last_name: Option<String>,
|
last_name: Option<String>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
pub enum NotPrivateChatType {
|
pub enum NotPrivateChatType {
|
||||||
|
@ -82,7 +98,7 @@ pub enum NotPrivateChatType {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::core::types::*;
|
use crate::core::types::*;
|
||||||
use serde_json::from_str;
|
use serde_json::{from_str, to_string};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn channel_de() {
|
fn channel_de() {
|
||||||
|
@ -129,4 +145,22 @@ mod tests {
|
||||||
fn private_chat_de_wrong_type_field() {
|
fn private_chat_de_wrong_type_field() {
|
||||||
assert!(from_str::<Chat>(r#"{"chat_id":0,"type":"WRONG"}"#).is_err());
|
assert!(from_str::<Chat>(r#"{"chat_id":0,"type":"WRONG"}"#).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn private_chat_ser() {
|
||||||
|
assert_eq!(
|
||||||
|
to_string(&Chat {
|
||||||
|
id: 0,
|
||||||
|
type_: ChatType::Private {
|
||||||
|
type_: (),
|
||||||
|
username: None,
|
||||||
|
first_name: None,
|
||||||
|
last_name: None
|
||||||
|
},
|
||||||
|
photo: None
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
r#"{"chat_id":0,"type":"private"}"#.to_owned()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#[derive(Debug, Deserialize, Hash, PartialEq, Eq)]
|
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Serialize)]
|
||||||
pub struct ChatPermissions {
|
pub struct ChatPermissions {
|
||||||
pub can_send_messages: Option<bool>,
|
pub can_send_messages: Option<bool>,
|
||||||
pub can_send_media_messages: Option<bool>,
|
pub can_send_media_messages: Option<bool>,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#[derive(Debug, Deserialize, Hash, PartialEq, Eq)]
|
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Serialize)]
|
||||||
pub struct ChatPhoto {
|
pub struct ChatPhoto {
|
||||||
pub small_file_id: String,
|
pub small_file_id: String,
|
||||||
pub big_file_id: String,
|
pub big_file_id: String,
|
||||||
|
|
|
@ -1,56 +1,20 @@
|
||||||
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, Serialize)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub message_id: i64,
|
#[serde(rename = "message_id")]
|
||||||
pub from: Option<Box<User>>,
|
pub id: i32,
|
||||||
pub date: i64,
|
pub date: i32,
|
||||||
pub chat: Chat,
|
pub chat: Chat,
|
||||||
pub forward_from: Option<User>,
|
#[serde(flatten)]
|
||||||
pub forward_from_chat: Option<Chat>,
|
pub type_: MessageType,
|
||||||
pub forward_from_message_id: Option<i64>,
|
|
||||||
pub forward_signature: Option<String>,
|
|
||||||
pub forward_sender_name: Option<String>,
|
|
||||||
pub forward_date: Option<i64>,
|
|
||||||
pub reply_to_message: Option<Box<Message>>,
|
|
||||||
pub edit_date: Option<i64>,
|
|
||||||
pub media_group_id: Option<String>,
|
|
||||||
pub author_signature: Option<String>,
|
|
||||||
pub text: Option<String>,
|
|
||||||
pub entities: Option<Vec<MessageEntity>>,
|
|
||||||
pub caption_entities: Option<Vec<MessageEntity>>,
|
|
||||||
pub audio: Option<Audio>,
|
|
||||||
pub document: Option<Document>,
|
|
||||||
pub animation: Option<Animation>,
|
|
||||||
pub game: Option<Game>,
|
|
||||||
pub photo: Option<Vec<PhotoSize>>,
|
|
||||||
pub sticker: Option<Sticker>,
|
|
||||||
pub video: Option<Video>,
|
|
||||||
pub voice: Option<Voice>,
|
|
||||||
pub video_note: Option<VideoNote>,
|
|
||||||
pub caption: Option<String>,
|
|
||||||
pub contact: Option<Contact>,
|
|
||||||
pub location: Option<Location>,
|
|
||||||
pub venue: Option<Venue>,
|
|
||||||
pub poll: Option<Poll>,
|
|
||||||
pub new_chat_members: Option<Vec<User>>,
|
|
||||||
pub left_chat_member: Option<User>,
|
|
||||||
pub new_chat_title: Option<String>,
|
|
||||||
pub new_chat_photo: Option<Vec<PhotoSize>>,
|
|
||||||
pub delete_chat_photo: Option<bool>,
|
|
||||||
pub group_chat_created: Option<bool>,
|
|
||||||
pub supergroup_chat_created: Option<bool>,
|
|
||||||
pub channel_chat_created: Option<bool>,
|
|
||||||
pub migrate_to_chat_id: Option<i64>,
|
|
||||||
pub migrate_from_chat_id: Option<i64>,
|
|
||||||
pub pinned_message: Option<Box<Message>>,
|
|
||||||
pub invoice: Option<Invoice>,
|
|
||||||
pub successful_payment: Option<SuccessfulPayment>,
|
|
||||||
pub connected_website: Option<String>,
|
|
||||||
pub passport_data: Option<PassportData>,
|
|
||||||
pub reply_markup: Option<InlineKeyboardMarkup>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum MessageType {}
|
||||||
|
|
|
@ -1,15 +1,32 @@
|
||||||
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,
|
||||||
chat_photo::ChatPhoto, document::Document, input_file::InputFile, input_media::InputMedia,
|
audio::Audio,
|
||||||
invoice::Invoice, label_price::LabeledPrice, message::Message, message_entity::MessageEntity,
|
chat::{Chat, ChatType, NotPrivateChatType},
|
||||||
order_info::OrderInfo, parse_mode::ParseMode, photo_size::PhotoSize,
|
chat_member::ChatMember,
|
||||||
pre_checkout_query::PreCheckoutQuery, send_invoice::SendInvoice,
|
chat_permissions::ChatPermissions,
|
||||||
shipping_address::ShippingAddress, shipping_option::ShippingOption,
|
chat_photo::ChatPhoto,
|
||||||
shipping_query::ShippingQuery, sticker::Sticker, successful_payment::SuccessfulPayment,
|
document::Document,
|
||||||
user::User, video::Video,
|
input_file::InputFile,
|
||||||
|
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,
|
||||||
|
shipping_address::ShippingAddress,
|
||||||
|
shipping_option::ShippingOption,
|
||||||
|
shipping_query::ShippingQuery,
|
||||||
|
sticker::Sticker,
|
||||||
|
successful_payment::SuccessfulPayment,
|
||||||
|
user::User,
|
||||||
|
video::Video,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod answer_pre_checkout_query;
|
mod answer_pre_checkout_query;
|
||||||
|
|
Loading…
Reference in a new issue