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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use serde::{Deserialize, Serialize};
/// This object contains information about a poll.
/// Currently, it can be one of
/// - [`crate::types::PollQuiz`]
/// - [`crate::types::PollRegular`]
/// # Documentation
/// <https://core.telegram.org/bots/api#poll>
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Poll {
Regular(crate::types::PollRegular),
Quiz(crate::types::PollQuiz),
}
impl Poll {
/// Helper method for field `allows_multiple_answers`.
///
/// `true`, if the poll allows multiple answers
#[must_use]
pub fn allows_multiple_answers(&self) -> bool {
match self {
Self::Regular(val) => val.allows_multiple_answers,
Self::Quiz(val) => val.allows_multiple_answers,
}
}
/// Helper method for field `allows_revoting`.
///
/// `true`, if the poll allows to change the chosen answer options
#[must_use]
pub fn allows_revoting(&self) -> bool {
match self {
Self::Regular(val) => val.allows_revoting,
Self::Quiz(val) => val.allows_revoting,
}
}
/// Helper method for field `close_date`.
///
/// Point in time (Unix timestamp) when the poll will be automatically closed
#[must_use]
pub fn close_date(&self) -> Option<i64> {
match self {
Self::Regular(val) => val.close_date,
Self::Quiz(val) => val.close_date,
}
}
/// Helper method for field `correct_option_ids`.
///
/// Array of 0-based identifiers of the correct answer options. Available only for polls in quiz mode which are closed or were sent (not forwarded) by the bot or to the private chat with the bot.
#[must_use]
pub fn correct_option_ids(&self) -> Option<&[i64]> {
match self {
Self::Quiz(val) => val.correct_option_ids.as_deref(),
Self::Regular(_) => None,
}
}
/// Helper method for field `description`.
///
/// Description of the poll; for polls inside the Message object only
#[must_use]
pub fn description(&self) -> Option<&str> {
match self {
Self::Regular(val) => val.description.as_deref(),
Self::Quiz(val) => val.description.as_deref(),
}
}
/// Helper method for field `description_entities`.
///
/// Special entities like usernames, URLs, bot commands, etc. that appear in the description
#[must_use]
pub fn description_entities(&self) -> Option<&[crate::types::MessageEntity]> {
match self {
Self::Regular(val) => val.description_entities.as_deref(),
Self::Quiz(val) => val.description_entities.as_deref(),
}
}
/// Helper method for field `explanation`.
///
/// Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in a quiz-style poll, 0-200 characters
#[must_use]
pub fn explanation(&self) -> Option<&str> {
match self {
Self::Quiz(val) => val.explanation.as_deref(),
Self::Regular(_) => None,
}
}
/// Helper method for field `explanation_entities`.
///
/// Special entities like usernames, URLs, bot commands, etc. that appear in the explanation
#[must_use]
pub fn explanation_entities(&self) -> Option<&[crate::types::MessageEntity]> {
match self {
Self::Regular(val) => val.explanation_entities.as_deref(),
Self::Quiz(val) => val.explanation_entities.as_deref(),
}
}
/// Helper method for field `id`.
///
/// Unique poll identifier
#[must_use]
pub fn id(&self) -> &str {
match self {
Self::Regular(val) => val.id.as_ref(),
Self::Quiz(val) => val.id.as_ref(),
}
}
/// Helper method for field `is_anonymous`.
///
/// `true`, if the poll is anonymous
#[must_use]
pub fn is_anonymous(&self) -> bool {
match self {
Self::Regular(val) => val.is_anonymous,
Self::Quiz(val) => val.is_anonymous,
}
}
/// Helper method for field `is_closed`.
///
/// `true`, if the poll is closed
#[must_use]
pub fn is_closed(&self) -> bool {
match self {
Self::Regular(val) => val.is_closed,
Self::Quiz(val) => val.is_closed,
}
}
/// Helper method for field `open_period`.
///
/// Amount of time in seconds the poll will be active after creation
#[must_use]
pub fn open_period(&self) -> Option<i64> {
match self {
Self::Regular(val) => val.open_period,
Self::Quiz(val) => val.open_period,
}
}
/// Helper method for field `options`.
///
/// List of poll options
#[must_use]
pub fn options(&self) -> &[crate::types::PollOption] {
match self {
Self::Regular(val) => val.options.as_ref(),
Self::Quiz(val) => val.options.as_ref(),
}
}
/// Helper method for field `question`.
///
/// Poll question, 1-300 characters
#[must_use]
pub fn question(&self) -> &str {
match self {
Self::Regular(val) => val.question.as_ref(),
Self::Quiz(val) => val.question.as_ref(),
}
}
/// Helper method for field `question_entities`.
///
/// Special entities that appear in the question. Currently, only custom emoji entities are allowed in poll questions
#[must_use]
pub fn question_entities(&self) -> Option<&[crate::types::MessageEntity]> {
match self {
Self::Regular(val) => val.question_entities.as_deref(),
Self::Quiz(val) => val.question_entities.as_deref(),
}
}
/// Helper method for field `total_voter_count`.
///
/// Total number of users that voted in the poll
#[must_use]
pub fn total_voter_count(&self) -> i64 {
match self {
Self::Regular(val) => val.total_voter_count,
Self::Quiz(val) => val.total_voter_count,
}
}
}
impl From<crate::types::PollRegular> for Poll {
fn from(val: crate::types::PollRegular) -> Self {
Self::Regular(val)
}
}
impl TryFrom<Poll> for crate::types::PollRegular {
type Error = crate::errors::ConvertToTypeError;
fn try_from(val: Poll) -> Result<Self, Self::Error> {
match val {
Poll::Regular(inner) => Ok(inner),
Poll::Quiz(_) => Err(Self::Error::new(stringify!(Poll), stringify!(PollRegular))),
}
}
}
impl From<crate::types::PollQuiz> for Poll {
fn from(val: crate::types::PollQuiz) -> Self {
Self::Quiz(val)
}
}
impl TryFrom<Poll> for crate::types::PollQuiz {
type Error = crate::errors::ConvertToTypeError;
fn try_from(val: Poll) -> Result<Self, Self::Error> {
match val {
Poll::Quiz(inner) => Ok(inner),
Poll::Regular(_) => Err(Self::Error::new(stringify!(Poll), stringify!(PollQuiz))),
}
}
}