mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Fixes
This commit is contained in:
parent
a74715f888
commit
620ffe5580
3 changed files with 69 additions and 58 deletions
|
@ -6,46 +6,9 @@ pub struct Chat {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub kind: ChatKind,
|
pub kind: ChatKind,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
|
||||||
pub photo: Option<ChatPhoto>,
|
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)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum ChatKind {
|
pub enum ChatKind {
|
||||||
|
@ -61,7 +24,6 @@ pub enum ChatKind {
|
||||||
/// 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_: (),
|
||||||
username: Option<String>,
|
username: Option<String>,
|
||||||
first_name: 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::core::types::*;
|
use crate::core::types::*;
|
||||||
|
@ -97,9 +89,9 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Chat {
|
Chat {
|
||||||
id: -1,
|
id: -1,
|
||||||
chat_kind: ChatKind::NonPrivate {
|
kind: ChatKind::NonPrivate {
|
||||||
title: None,
|
title: None,
|
||||||
non_private_chat_kind: NonPrivateChatKind::Channel {
|
kind: NonPrivateChatKind::Channel {
|
||||||
username: Some("channelname".into())
|
username: Some("channelname".into())
|
||||||
},
|
},
|
||||||
description: None,
|
description: None,
|
||||||
|
@ -120,7 +112,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Chat {
|
Chat {
|
||||||
id: 0,
|
id: 0,
|
||||||
chat_kind: ChatKind::Private {
|
kind: ChatKind::Private {
|
||||||
type_: (),
|
type_: (),
|
||||||
username: Some("username".into()),
|
username: Some("username".into()),
|
||||||
first_name: Some("Anon".into()),
|
first_name: Some("Anon".into()),
|
||||||
|
|
|
@ -74,13 +74,20 @@ pub enum MessageKind {
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum ForwardKind {
|
pub enum ForwardKind {
|
||||||
ChannelForward {
|
ChannelForward {
|
||||||
forward_from_channel: Box<Chat>,
|
#[serde(rename = "forward_date")]
|
||||||
forward_from_message_id: i64,
|
date: i32,
|
||||||
forward_signature: String,
|
#[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 {
|
NonChannelForward {
|
||||||
|
#[serde(rename = "forward_date")]
|
||||||
|
date: i32,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
forward: UserOrSenderName,
|
from: ForwardedFrom,
|
||||||
},
|
},
|
||||||
Origin {
|
Origin {
|
||||||
reply_to_message: Option<Box<Message>>,
|
reply_to_message: Option<Box<Message>>,
|
||||||
|
@ -90,8 +97,14 @@ pub enum ForwardKind {
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum ForwardedFrom {
|
pub enum ForwardedFrom {
|
||||||
User { from: User },
|
User {
|
||||||
SenderName { sender_name: String },
|
#[serde(rename = "forward_from")]
|
||||||
|
user: User,
|
||||||
|
},
|
||||||
|
SenderName {
|
||||||
|
#[serde(rename = "forward_sender_name")]
|
||||||
|
sender_name: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
||||||
|
@ -155,26 +168,32 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn incoming_origin_de() {
|
fn incoming_origin_de() {
|
||||||
assert_eq!(Message {
|
let expected = Message {
|
||||||
id: 0,
|
id: 0,
|
||||||
date: 0,
|
date: 0,
|
||||||
chat: Box::new(Chat {
|
chat: Box::new(Chat {
|
||||||
id: 0,
|
id: 0,
|
||||||
chat_kind: ChatKind::Private {
|
kind: ChatKind::Private {
|
||||||
type_: (),
|
type_: (),
|
||||||
username: None,
|
username: None,
|
||||||
first_name: None,
|
first_name: None,
|
||||||
last_name: None
|
last_name: None,
|
||||||
},
|
},
|
||||||
photo: None
|
photo: None,
|
||||||
}),
|
}),
|
||||||
message_kind: MessageKind::IncomingMessage {
|
message_kind: MessageKind::IncomingMessage {
|
||||||
forward_kind: ForwardKind::Origin { reply_to_message: None },
|
forward_kind: ForwardKind::Origin {
|
||||||
|
reply_to_message: None,
|
||||||
|
},
|
||||||
edit_date: None,
|
edit_date: None,
|
||||||
media_kind: MediaKind::Text { text: "Hello".to_string(), entities: vec![] },
|
media_kind: MediaKind::Text {
|
||||||
reply_markup: None
|
text: "Hello".to_string(),
|
||||||
}
|
entities: vec![],
|
||||||
},
|
},
|
||||||
from_str(r#"{"message_id":0,"date":0,"chat":{"chat_id":0,"type":"private"},"text":"Hello","entities":[]}"#).unwrap());
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub use self::{
|
||||||
input_media::InputMedia,
|
input_media::InputMedia,
|
||||||
invoice::Invoice,
|
invoice::Invoice,
|
||||||
label_price::LabeledPrice,
|
label_price::LabeledPrice,
|
||||||
message::{ForwardKind, MediaKind, Message, MessageKind, UserOrSenderName},
|
message::{ForwardKind, ForwardedFrom, MediaKind, Message, MessageKind},
|
||||||
message_entity::MessageEntity,
|
message_entity::MessageEntity,
|
||||||
order_info::OrderInfo,
|
order_info::OrderInfo,
|
||||||
parse_mode::ParseMode,
|
parse_mode::ParseMode,
|
||||||
|
|
Loading…
Reference in a new issue