1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use is_macro::Is;
use serde::{ser::SerializeMap, Serialize};
use crate::types::{
parameters::{ChatId, ImplicitChatId},
user,
};
#[derive(Debug, PartialEq, Eq, Clone, Hash, Default, Is)]
#[non_exhaustive]
#[must_use]
pub enum Scope {
#[default]
Default,
AllPrivateChats,
AllGroupChats,
AllChatAdministrators,
Chat(ChatId),
ChatAdministrators(ChatId),
ChatMember(ChatId, user::Id),
}
impl Scope {
pub const fn with_all_private_chats() -> Self {
Self::AllPrivateChats
}
pub const fn with_all_group_chats() -> Self {
Self::AllGroupChats
}
pub const fn with_all_chat_administrators() -> Self {
Self::AllChatAdministrators
}
pub fn with_chat(chat_id: impl ImplicitChatId) -> Self {
Self::Chat(chat_id.into())
}
pub fn with_chat_administrators(chat_id: impl ImplicitChatId) -> Self {
Self::ChatAdministrators(chat_id.into())
}
pub fn with_chat_member(
chat_id: impl ImplicitChatId,
user_id: user::Id,
) -> Self {
Self::ChatMember(chat_id.into(), user_id)
}
}
impl Serialize for Scope {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Default => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("type", "default")?;
map.end()
}
Self::AllPrivateChats => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("type", "all_private_chats")?;
map.end()
}
Self::AllGroupChats => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("type", "all_group_chats")?;
map.end()
}
Self::AllChatAdministrators => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("type", "all_chat_administrators")?;
map.end()
}
Self::Chat(chat_id) => {
let mut map = serializer.serialize_map(Some(2))?;
map.serialize_entry("type", "chat")?;
map.serialize_entry("chat_id", chat_id)?;
map.end()
}
Self::ChatAdministrators(chat_id) => {
let mut map = serializer.serialize_map(Some(2))?;
map.serialize_entry("type", "chat_administrators")?;
map.serialize_entry("chat_id", chat_id)?;
map.end()
}
Self::ChatMember(chat_id, user_id) => {
let mut map = serializer.serialize_map(Some(3))?;
map.serialize_entry("type", "chat_member")?;
map.serialize_entry("chat_id", chat_id)?;
map.serialize_entry("user_id", user_id)?;
map.end()
}
}
}
}