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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use super::{ParseMode, Text};
use is_macro::Is;
use serde::Serialize;
use std::convert::From;
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize, Is)]
#[must_use]
pub enum Answer {
Single,
Multiple,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize, Is)]
#[non_exhaustive]
#[must_use]
#[serde(rename_all = "snake_case")]
pub enum AutoClose {
OpenPeriod(u16),
CloseDate(i64),
}
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)]
#[must_use]
pub struct Quiz {
correct_option_id: usize,
#[serde(skip_serializing_if = "Option::is_none")]
explanation: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
explanation_parse_mode: Option<ParseMode>,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Serialize)]
#[must_use]
pub struct Poll {
allows_multiple_answers: bool,
}
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Is)]
#[serde(tag = "type", rename_all = "snake_case")]
#[must_use]
pub enum Kind {
Quiz(Quiz),
#[serde(rename = "regular")]
Poll(Poll),
}
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize)]
#[must_use]
pub struct Any {
#[serde(flatten)]
kind: Kind,
question: String,
options: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
is_closed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
is_anonymous: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(flatten)]
auto_close: Option<AutoClose>,
}
impl Quiz {
pub const fn new(correct_option_id: usize) -> Self {
Self {
correct_option_id,
explanation: None,
explanation_parse_mode: None,
}
}
pub fn explanation(mut self, explanation: impl Into<Text>) -> Self {
let explanation = explanation.into();
self.explanation = Some(explanation.text);
self.explanation_parse_mode = explanation.parse_mode;
self
}
}
impl Poll {
pub fn new(answer: Answer) -> Self {
Self {
allows_multiple_answers: answer == Answer::Multiple,
}
}
}
impl Any {
pub fn new(
question: impl Into<String>,
options: impl Into<Vec<String>>,
kind: impl Into<Kind>,
) -> Self {
Self {
kind: kind.into(),
question: question.into(),
options: options.into(),
is_closed: None,
is_anonymous: None,
auto_close: None,
}
}
pub const fn is_immediately_closed(mut self, is_closed: bool) -> Self {
self.is_closed = Some(is_closed);
self
}
pub const fn is_anonymous(mut self, is_anonymous: bool) -> Self {
self.is_anonymous = Some(is_anonymous);
self
}
pub const fn auto_close(mut self, auto_close: AutoClose) -> Self {
self.auto_close = Some(auto_close);
self
}
}
impl From<Quiz> for Kind {
fn from(quiz: Quiz) -> Self {
Self::Quiz(quiz)
}
}
impl From<Poll> for Kind {
fn from(poll: Poll) -> Self {
Self::Poll(poll)
}
}