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
use crate::errors::Result;
use crate::types::*;
use uuid::Uuid;

use std::fmt::Debug;

/// Describes the type of a poll
pub trait TDPollType: Debug + RObject {}

/// Describes the type of a poll
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(tag = "@type")]
pub enum PollType {
    #[doc(hidden)]
    #[default]
    _Default,
    /// A poll in quiz mode, which has exactly one correct answer option and can be answered only once
    #[serde(rename = "pollTypeQuiz")]
    Quiz(PollTypeQuiz),
    /// A regular poll
    #[serde(rename = "pollTypeRegular")]
    Regular(PollTypeRegular),
}

impl RObject for PollType {
    #[doc(hidden)]
    fn extra(&self) -> Option<&str> {
        match self {
            PollType::Quiz(t) => t.extra(),
            PollType::Regular(t) => t.extra(),

            _ => None,
        }
    }
    #[doc(hidden)]
    fn client_id(&self) -> Option<i32> {
        match self {
            PollType::Quiz(t) => t.client_id(),
            PollType::Regular(t) => t.client_id(),

            _ => None,
        }
    }
}

impl PollType {
    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
        Ok(serde_json::from_str(json.as_ref())?)
    }
    #[doc(hidden)]
    pub fn _is_default(&self) -> bool {
        matches!(self, PollType::_Default)
    }
}

impl AsRef<PollType> for PollType {
    fn as_ref(&self) -> &PollType {
        self
    }
}

/// A poll in quiz mode, which has exactly one correct answer option and can be answered only once
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PollTypeQuiz {
    #[doc(hidden)]
    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
    extra: Option<String>,
    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
    client_id: Option<i32>,
    /// 0-based identifier of the correct answer option; 1 for a yet unanswered poll

    #[serde(default)]
    correct_option_id: i32,
    /// Text that is shown when the user chooses an incorrect answer or taps on the lamp icon; 0-200 characters with at most 2 line feeds; empty for a yet unanswered poll
    explanation: FormattedText,
}

impl RObject for PollTypeQuiz {
    #[doc(hidden)]
    fn extra(&self) -> Option<&str> {
        self.extra.as_deref()
    }
    #[doc(hidden)]
    fn client_id(&self) -> Option<i32> {
        self.client_id
    }
}

impl TDPollType for PollTypeQuiz {}

impl PollTypeQuiz {
    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
        Ok(serde_json::from_str(json.as_ref())?)
    }
    pub fn builder() -> PollTypeQuizBuilder {
        let mut inner = PollTypeQuiz::default();
        inner.extra = Some(Uuid::new_v4().to_string());

        PollTypeQuizBuilder { inner }
    }

    pub fn correct_option_id(&self) -> i32 {
        self.correct_option_id
    }

    pub fn explanation(&self) -> &FormattedText {
        &self.explanation
    }
}

#[doc(hidden)]
pub struct PollTypeQuizBuilder {
    inner: PollTypeQuiz,
}

#[deprecated]
pub type RTDPollTypeQuizBuilder = PollTypeQuizBuilder;

impl PollTypeQuizBuilder {
    pub fn build(&self) -> PollTypeQuiz {
        self.inner.clone()
    }

    pub fn correct_option_id(&mut self, correct_option_id: i32) -> &mut Self {
        self.inner.correct_option_id = correct_option_id;
        self
    }

    pub fn explanation<T: AsRef<FormattedText>>(&mut self, explanation: T) -> &mut Self {
        self.inner.explanation = explanation.as_ref().clone();
        self
    }
}

impl AsRef<PollTypeQuiz> for PollTypeQuiz {
    fn as_ref(&self) -> &PollTypeQuiz {
        self
    }
}

impl AsRef<PollTypeQuiz> for PollTypeQuizBuilder {
    fn as_ref(&self) -> &PollTypeQuiz {
        &self.inner
    }
}

/// A regular poll
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PollTypeRegular {
    #[doc(hidden)]
    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
    extra: Option<String>,
    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
    client_id: Option<i32>,
    /// True, if multiple answer options can be chosen simultaneously

    #[serde(default)]
    allow_multiple_answers: bool,
}

impl RObject for PollTypeRegular {
    #[doc(hidden)]
    fn extra(&self) -> Option<&str> {
        self.extra.as_deref()
    }
    #[doc(hidden)]
    fn client_id(&self) -> Option<i32> {
        self.client_id
    }
}

impl TDPollType for PollTypeRegular {}

impl PollTypeRegular {
    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
        Ok(serde_json::from_str(json.as_ref())?)
    }
    pub fn builder() -> PollTypeRegularBuilder {
        let mut inner = PollTypeRegular::default();
        inner.extra = Some(Uuid::new_v4().to_string());

        PollTypeRegularBuilder { inner }
    }

    pub fn allow_multiple_answers(&self) -> bool {
        self.allow_multiple_answers
    }
}

#[doc(hidden)]
pub struct PollTypeRegularBuilder {
    inner: PollTypeRegular,
}

#[deprecated]
pub type RTDPollTypeRegularBuilder = PollTypeRegularBuilder;

impl PollTypeRegularBuilder {
    pub fn build(&self) -> PollTypeRegular {
        self.inner.clone()
    }

    pub fn allow_multiple_answers(&mut self, allow_multiple_answers: bool) -> &mut Self {
        self.inner.allow_multiple_answers = allow_multiple_answers;
        self
    }
}

impl AsRef<PollTypeRegular> for PollTypeRegular {
    fn as_ref(&self) -> &PollTypeRegular {
        self
    }
}

impl AsRef<PollTypeRegular> for PollTypeRegularBuilder {
    fn as_ref(&self) -> &PollTypeRegular {
        &self.inner
    }
}