mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Fixed bug with Chat
This commit is contained in:
parent
386c73b47a
commit
b254de2564
1 changed files with 80 additions and 16 deletions
|
@ -2,12 +2,42 @@ use crate::core::types::{ChatPermissions, ChatPhoto, Message};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
||||||
pub struct Chat {
|
pub struct Chat {
|
||||||
|
#[serde(rename = "chat_id")]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub type_: ChatType,
|
pub type_: ChatType,
|
||||||
pub photo: Option<ChatPhoto>,
|
pub photo: Option<ChatPhoto>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct PrivateChatTypeVisitor;
|
||||||
|
|
||||||
|
impl<'de> serde::de::Visitor<'de> for PrivateChatTypeVisitor {
|
||||||
|
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(PrivateChatTypeVisitor)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
|
@ -21,6 +51,10 @@ pub enum ChatType {
|
||||||
pinned_message: Option<Box<Message>>,
|
pinned_message: Option<Box<Message>>,
|
||||||
},
|
},
|
||||||
Private {
|
Private {
|
||||||
|
/// Dummy field. Used to ensure that "type" field is equal to "private"
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
#[serde(deserialize_with = "assert_private_field")]
|
||||||
|
type_: (),
|
||||||
username: Option<String>,
|
username: Option<String>,
|
||||||
first_name: Option<String>,
|
first_name: Option<String>,
|
||||||
last_name: Option<String>,
|
last_name: Option<String>,
|
||||||
|
@ -45,24 +79,54 @@ pub enum NotPrivateChatType {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_chat_de() {
|
mod tests {
|
||||||
|
use crate::core::types::*;
|
||||||
use serde_json::from_str;
|
use serde_json::from_str;
|
||||||
|
|
||||||
assert_eq!(
|
#[test]
|
||||||
Chat {
|
fn channel_de() {
|
||||||
id: 0,
|
assert_eq!(
|
||||||
type_: ChatType::NotPrivate {
|
Chat {
|
||||||
title: None,
|
id: -1,
|
||||||
type_: NotPrivateChatType::Channel {
|
type_: ChatType::NotPrivate {
|
||||||
username: Some("channelname".into())
|
title: None,
|
||||||
|
type_: NotPrivateChatType::Channel {
|
||||||
|
username: Some("channelname".into())
|
||||||
|
},
|
||||||
|
description: None,
|
||||||
|
invite_link: None,
|
||||||
|
pinned_message: None
|
||||||
},
|
},
|
||||||
description: None,
|
photo: None,
|
||||||
invite_link: None,
|
|
||||||
pinned_message: None
|
|
||||||
},
|
},
|
||||||
photo: None,
|
from_str(
|
||||||
},
|
r#"{"chat_id":-1,"type":"channel","username":"channelname"}"#
|
||||||
from_str(r#"{"id":0,"type":"channel","username":"channelname"}"#).unwrap()
|
)
|
||||||
);
|
.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn private_chat_de() {
|
||||||
|
assert_eq!(
|
||||||
|
Chat {
|
||||||
|
id: 0,
|
||||||
|
type_: ChatType::Private {
|
||||||
|
type_: (),
|
||||||
|
username: Some("username".into()),
|
||||||
|
first_name: Some("Anon".into()),
|
||||||
|
last_name: None
|
||||||
|
},
|
||||||
|
photo: None
|
||||||
|
},
|
||||||
|
from_str(
|
||||||
|
r#"{"chat_id":0,"type":"private","username":"username","first_name":"Anon"}"#
|
||||||
|
).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn private_chat_de_wrong_type_field() {
|
||||||
|
assert!(from_str::<Chat>(r#"{"chat_id":0,"type":"WRONG"}"#).is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue