This commit is contained in:
Mr-Andersen 2019-09-07 19:08:32 +03:00
parent a74715f888
commit 620ffe5580
3 changed files with 69 additions and 58 deletions

View file

@ -6,46 +6,9 @@ pub struct Chat {
pub id: i64,
#[serde(flatten)]
pub kind: ChatKind,
#[serde(skip_serializing_if = "Option::is_none")]
pub photo: Option<ChatPhoto>,
}
struct PrivateChatKindVisitor;
impl<'de> serde::de::Visitor<'de> for PrivateChatKindVisitor {
type Value = ();
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, r#"field equal to "private""#)
}
fn visit_borrowed_str<E: serde::de::Error>(
self,
v: &'de str,
) -> Result<Self::Value, E> {
match v {
"private" => Ok(()),
_ => Err(E::invalid_value(
serde::de::Unexpected::Str(v),
&r#""private""#,
)),
}
}
}
fn assert_private_field<'de, D: serde::Deserializer<'de>>(
des: D,
) -> Result<(), D::Error> {
des.deserialize_str(PrivateChatKindVisitor)
}
/*fn serialize_private_field<S: serde::Serializer>(
_: &(),
ser: S,
) -> Result<S::Ok, S::Error> {
ser.serialize_str("private")
}*/
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(untagged)]
pub enum ChatKind {
@ -61,7 +24,6 @@ pub enum ChatKind {
/// Dummy field. Used to ensure that "type" field is equal to "private"
#[serde(rename = "type")]
#[serde(deserialize_with = "assert_private_field")]
// #[serde(serialize_with = "serialize_private_field")]
type_: (),
username: Option<String>,
first_name: Option<String>,
@ -87,6 +49,36 @@ pub enum NonPrivateChatKind {
},
}
struct PrivateChatKindVisitor;
impl<'de> serde::de::Visitor<'de> for PrivateChatKindVisitor {
type Value = ();
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, r#"field equal to "private""#)
}
fn visit_borrowed_str<E: serde::de::Error>(
self,
v: &'de str,
) -> Result<Self::Value, E> {
match v {
"private" => Ok(()),
_ => Err(E::invalid_value(
serde::de::Unexpected::Str(v),
&r#""private""#,
)),
}
}
}
fn assert_private_field<'de, D>(des: D) -> Result<(), D::Error>
where
D: serde::Deserializer<'de>,
{
des.deserialize_str(PrivateChatKindVisitor)
}
#[cfg(test)]
mod tests {
use crate::core::types::*;
@ -97,9 +89,9 @@ mod tests {
assert_eq!(
Chat {
id: -1,
chat_kind: ChatKind::NonPrivate {
kind: ChatKind::NonPrivate {
title: None,
non_private_chat_kind: NonPrivateChatKind::Channel {
kind: NonPrivateChatKind::Channel {
username: Some("channelname".into())
},
description: None,
@ -120,7 +112,7 @@ mod tests {
assert_eq!(
Chat {
id: 0,
chat_kind: ChatKind::Private {
kind: ChatKind::Private {
type_: (),
username: Some("username".into()),
first_name: Some("Anon".into()),

View file

@ -74,13 +74,20 @@ pub enum MessageKind {
#[serde(untagged)]
pub enum ForwardKind {
ChannelForward {
forward_from_channel: Box<Chat>,
forward_from_message_id: i64,
forward_signature: String,
#[serde(rename = "forward_date")]
date: i32,
#[serde(rename = "forward_from_chat")]
chat: Box<Chat>,
#[serde(rename = "forward_from_message_id")]
message_id: i64,
#[serde(rename = "forward_signature")]
signature: Option<String>,
},
NonChannelForward {
#[serde(rename = "forward_date")]
date: i32,
#[serde(flatten)]
forward: UserOrSenderName,
from: ForwardedFrom,
},
Origin {
reply_to_message: Option<Box<Message>>,
@ -90,8 +97,14 @@ pub enum ForwardKind {
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(untagged)]
pub enum ForwardedFrom {
User { from: User },
SenderName { sender_name: String },
User {
#[serde(rename = "forward_from")]
user: User,
},
SenderName {
#[serde(rename = "forward_sender_name")]
sender_name: String,
},
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
@ -155,26 +168,32 @@ mod tests {
#[test]
fn incoming_origin_de() {
assert_eq!(Message {
let expected = Message {
id: 0,
date: 0,
chat: Box::new(Chat {
id: 0,
chat_kind: ChatKind::Private {
kind: ChatKind::Private {
type_: (),
username: None,
first_name: None,
last_name: None
last_name: None,
},
photo: None
photo: None,
}),
message_kind: MessageKind::IncomingMessage {
forward_kind: ForwardKind::Origin { reply_to_message: None },
edit_date: None,
media_kind: MediaKind::Text { text: "Hello".to_string(), entities: vec![] },
reply_markup: None
}
forward_kind: ForwardKind::Origin {
reply_to_message: None,
},
from_str(r#"{"message_id":0,"date":0,"chat":{"chat_id":0,"type":"private"},"text":"Hello","entities":[]}"#).unwrap());
edit_date: None,
media_kind: MediaKind::Text {
text: "Hello".to_string(),
entities: vec![],
},
reply_markup: None,
},
};
let actual = from_str(r#"{"message_id":0,"date":0,"chat":{"chat_id":0,"type":"private"},"text":"Hello","entities":[]}"#).unwrap();
assert_eq!(expected, actual);
}
}

View file

@ -13,7 +13,7 @@ pub use self::{
input_media::InputMedia,
invoice::Invoice,
label_price::LabeledPrice,
message::{ForwardKind, MediaKind, Message, MessageKind, UserOrSenderName},
message::{ForwardKind, ForwardedFrom, MediaKind, Message, MessageKind},
message_entity::MessageEntity,
order_info::OrderInfo,
parse_mode::ParseMode,