rust_tdlib/types/
poll_type.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5use std::fmt::Debug;
6
7/// Describes the type of a poll
8pub trait TDPollType: Debug + RObject {}
9
10/// Describes the type of a poll
11#[derive(Debug, Clone, Deserialize, Serialize, Default)]
12#[serde(tag = "@type")]
13pub enum PollType {
14    #[doc(hidden)]
15    #[default]
16    _Default,
17    /// A poll in quiz mode, which has exactly one correct answer option and can be answered only once
18    #[serde(rename = "pollTypeQuiz")]
19    Quiz(PollTypeQuiz),
20    /// A regular poll
21    #[serde(rename = "pollTypeRegular")]
22    Regular(PollTypeRegular),
23}
24
25impl RObject for PollType {
26    #[doc(hidden)]
27    fn extra(&self) -> Option<&str> {
28        match self {
29            PollType::Quiz(t) => t.extra(),
30            PollType::Regular(t) => t.extra(),
31
32            _ => None,
33        }
34    }
35    #[doc(hidden)]
36    fn client_id(&self) -> Option<i32> {
37        match self {
38            PollType::Quiz(t) => t.client_id(),
39            PollType::Regular(t) => t.client_id(),
40
41            _ => None,
42        }
43    }
44}
45
46impl PollType {
47    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
48        Ok(serde_json::from_str(json.as_ref())?)
49    }
50    #[doc(hidden)]
51    pub fn _is_default(&self) -> bool {
52        matches!(self, PollType::_Default)
53    }
54}
55
56impl AsRef<PollType> for PollType {
57    fn as_ref(&self) -> &PollType {
58        self
59    }
60}
61
62/// A poll in quiz mode, which has exactly one correct answer option and can be answered only once
63#[derive(Debug, Clone, Default, Serialize, Deserialize)]
64pub struct PollTypeQuiz {
65    #[doc(hidden)]
66    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
67    extra: Option<String>,
68    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
69    client_id: Option<i32>,
70    /// 0-based identifier of the correct answer option; 1 for a yet unanswered poll
71
72    #[serde(default)]
73    correct_option_id: i32,
74    /// 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
75    explanation: FormattedText,
76}
77
78impl RObject for PollTypeQuiz {
79    #[doc(hidden)]
80    fn extra(&self) -> Option<&str> {
81        self.extra.as_deref()
82    }
83    #[doc(hidden)]
84    fn client_id(&self) -> Option<i32> {
85        self.client_id
86    }
87}
88
89impl TDPollType for PollTypeQuiz {}
90
91impl PollTypeQuiz {
92    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
93        Ok(serde_json::from_str(json.as_ref())?)
94    }
95    pub fn builder() -> PollTypeQuizBuilder {
96        let mut inner = PollTypeQuiz::default();
97        inner.extra = Some(Uuid::new_v4().to_string());
98
99        PollTypeQuizBuilder { inner }
100    }
101
102    pub fn correct_option_id(&self) -> i32 {
103        self.correct_option_id
104    }
105
106    pub fn explanation(&self) -> &FormattedText {
107        &self.explanation
108    }
109}
110
111#[doc(hidden)]
112pub struct PollTypeQuizBuilder {
113    inner: PollTypeQuiz,
114}
115
116#[deprecated]
117pub type RTDPollTypeQuizBuilder = PollTypeQuizBuilder;
118
119impl PollTypeQuizBuilder {
120    pub fn build(&self) -> PollTypeQuiz {
121        self.inner.clone()
122    }
123
124    pub fn correct_option_id(&mut self, correct_option_id: i32) -> &mut Self {
125        self.inner.correct_option_id = correct_option_id;
126        self
127    }
128
129    pub fn explanation<T: AsRef<FormattedText>>(&mut self, explanation: T) -> &mut Self {
130        self.inner.explanation = explanation.as_ref().clone();
131        self
132    }
133}
134
135impl AsRef<PollTypeQuiz> for PollTypeQuiz {
136    fn as_ref(&self) -> &PollTypeQuiz {
137        self
138    }
139}
140
141impl AsRef<PollTypeQuiz> for PollTypeQuizBuilder {
142    fn as_ref(&self) -> &PollTypeQuiz {
143        &self.inner
144    }
145}
146
147/// A regular poll
148#[derive(Debug, Clone, Default, Serialize, Deserialize)]
149pub struct PollTypeRegular {
150    #[doc(hidden)]
151    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
152    extra: Option<String>,
153    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
154    client_id: Option<i32>,
155    /// True, if multiple answer options can be chosen simultaneously
156
157    #[serde(default)]
158    allow_multiple_answers: bool,
159}
160
161impl RObject for PollTypeRegular {
162    #[doc(hidden)]
163    fn extra(&self) -> Option<&str> {
164        self.extra.as_deref()
165    }
166    #[doc(hidden)]
167    fn client_id(&self) -> Option<i32> {
168        self.client_id
169    }
170}
171
172impl TDPollType for PollTypeRegular {}
173
174impl PollTypeRegular {
175    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
176        Ok(serde_json::from_str(json.as_ref())?)
177    }
178    pub fn builder() -> PollTypeRegularBuilder {
179        let mut inner = PollTypeRegular::default();
180        inner.extra = Some(Uuid::new_v4().to_string());
181
182        PollTypeRegularBuilder { inner }
183    }
184
185    pub fn allow_multiple_answers(&self) -> bool {
186        self.allow_multiple_answers
187    }
188}
189
190#[doc(hidden)]
191pub struct PollTypeRegularBuilder {
192    inner: PollTypeRegular,
193}
194
195#[deprecated]
196pub type RTDPollTypeRegularBuilder = PollTypeRegularBuilder;
197
198impl PollTypeRegularBuilder {
199    pub fn build(&self) -> PollTypeRegular {
200        self.inner.clone()
201    }
202
203    pub fn allow_multiple_answers(&mut self, allow_multiple_answers: bool) -> &mut Self {
204        self.inner.allow_multiple_answers = allow_multiple_answers;
205        self
206    }
207}
208
209impl AsRef<PollTypeRegular> for PollTypeRegular {
210    fn as_ref(&self) -> &PollTypeRegular {
211        self
212    }
213}
214
215impl AsRef<PollTypeRegular> for PollTypeRegularBuilder {
216    fn as_ref(&self) -> &PollTypeRegular {
217        &self.inner
218    }
219}