Re-factored Chat in "enum style"

This commit is contained in:
Mr-Andersen 2019-09-03 22:29:37 +03:00
parent 52e8507be4
commit 59360cbc7d

View file

@ -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<String>,
pub username: Option<String>,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub id: Integer,
#[serde(flatten)]
pub type_: ChatType,
pub photo: Option<ChatPhoto>,
pub description: Option<String>,
pub invite_link: Option<String>,
pub pinned_message: Option<Box<Message>>,
pub permissions: Option<ChatPermissions>,
pub sticker_set_name: Option<String>,
pub can_set_sticker_set: Option<bool>,
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(rename_all = "snake_case")]
#[serde(untagged)]
pub enum ChatType {
NotPrivate {
title: Option<String>,
#[serde(flatten)]
type_: NotPrivateChatType,
description: Option<String>,
invite_link: Option<String>,
pinned_message: Option<Box<Message>>
},
Private {
username: Option<String>,
first_name: Option<String>,
last_name: Option<String>
}
}
#[derive(Debug, Deserialize, Eq, Hash, PartialEq)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum NotPrivateChatType {
Channel {
username: Option<String>
},
Group {
permissions: Option<ChatPermissions>
},
Supergroup {
username: Option<String>,
sticker_set_name: Option<String>,
can_set_sticker_set: Option<bool>,
permissions: Option<ChatPermissions>
}
}
#[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()
);
}