diff --git a/src/core/types/chat.rs b/src/core/types/chat.rs index 535d7769..3d8f72df 100644 --- a/src/core/types/chat.rs +++ b/src/core/types/chat.rs @@ -1,18 +1,72 @@ -use crate::core::types::{ChatPermissions, ChatPhoto, Message}; +use crate::core::types::{ChatPermissions, ChatPhoto, Message, Integer}; -#[derive(Debug, Deserialize, Hash, PartialEq, Eq)] + +#[derive(Debug, Deserialize, Eq, Hash, PartialEq)] pub struct Chat { - pub id: i64, - pub chat_type: String, - pub title: Option, - pub username: Option, - pub first_name: Option, - pub last_name: Option, + pub id: Integer, + #[serde(flatten)] + pub type_: ChatType, pub photo: Option, - pub description: Option, - pub invite_link: Option, - pub pinned_message: Option>, - pub permissions: Option, - pub sticker_set_name: Option, - pub can_set_sticker_set: Option, +} + + +#[derive(Debug, Deserialize, Eq, Hash, PartialEq)] +#[serde(rename_all = "snake_case")] +#[serde(untagged)] +pub enum ChatType { + NotPrivate { + title: Option, + #[serde(flatten)] + type_: NotPrivateChatType, + description: Option, + invite_link: Option, + pinned_message: Option> + }, + Private { + username: Option, + first_name: Option, + last_name: Option + } +} + + +#[derive(Debug, Deserialize, Eq, Hash, PartialEq)] +#[serde(rename_all = "snake_case")] +#[serde(tag = "type")] +pub enum NotPrivateChatType { + Channel { + username: Option + }, + Group { + permissions: Option + }, + Supergroup { + username: Option, + sticker_set_name: Option, + can_set_sticker_set: Option, + permissions: Option + } +} + + +#[test] +fn test_chat_de() { + use serde_json::from_str; + + assert_eq!( + Chat { + id: 0, + type_: ChatType::NotPrivate { + title: None, + type_: NotPrivateChatType::Channel { + username: Some("channelname".into()) + }, + description: None, + invite_link: None, + pinned_message: None + }, + photo: None, + }, + from_str(r#"{"id":0,"type":"channel","username":"channelname"}"#).unwrap() + ); }