rust_tdlib/types/
poll.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Describes a poll
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Poll {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// Unique poll identifier
14
15    #[serde(
16        deserialize_with = "super::_common::number_from_string",
17        serialize_with = "super::_common::string_to_number"
18    )]
19    #[serde(default)]
20    id: i64,
21    /// Poll question; 1-300 characters
22
23    #[serde(default)]
24    question: String,
25    /// List of poll answer options
26
27    #[serde(default)]
28    options: Vec<PollOption>,
29    /// Total number of voters, participating in the poll
30
31    #[serde(default)]
32    total_voter_count: i32,
33    /// User identifiers of recent voters, if the poll is non-anonymous
34
35    #[serde(default)]
36    recent_voter_user_ids: Vec<i64>,
37    /// True, if the poll is anonymous
38
39    #[serde(default)]
40    is_anonymous: bool,
41    /// Type of the poll
42
43    #[serde(rename(serialize = "type", deserialize = "type"))]
44    #[serde(skip_serializing_if = "PollType::_is_default")]
45    type_: PollType,
46    /// Amount of time the poll will be active after creation, in seconds
47
48    #[serde(default)]
49    open_period: i32,
50    /// Point in time (Unix timestamp) when the poll will automatically be closed
51
52    #[serde(default)]
53    close_date: i32,
54    /// True, if the poll is closed
55
56    #[serde(default)]
57    is_closed: bool,
58}
59
60impl RObject for Poll {
61    #[doc(hidden)]
62    fn extra(&self) -> Option<&str> {
63        self.extra.as_deref()
64    }
65    #[doc(hidden)]
66    fn client_id(&self) -> Option<i32> {
67        self.client_id
68    }
69}
70
71impl Poll {
72    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
73        Ok(serde_json::from_str(json.as_ref())?)
74    }
75    pub fn builder() -> PollBuilder {
76        let mut inner = Poll::default();
77        inner.extra = Some(Uuid::new_v4().to_string());
78
79        PollBuilder { inner }
80    }
81
82    pub fn id(&self) -> i64 {
83        self.id
84    }
85
86    pub fn question(&self) -> &String {
87        &self.question
88    }
89
90    pub fn options(&self) -> &Vec<PollOption> {
91        &self.options
92    }
93
94    pub fn total_voter_count(&self) -> i32 {
95        self.total_voter_count
96    }
97
98    pub fn recent_voter_user_ids(&self) -> &Vec<i64> {
99        &self.recent_voter_user_ids
100    }
101
102    pub fn is_anonymous(&self) -> bool {
103        self.is_anonymous
104    }
105
106    pub fn type_(&self) -> &PollType {
107        &self.type_
108    }
109
110    pub fn open_period(&self) -> i32 {
111        self.open_period
112    }
113
114    pub fn close_date(&self) -> i32 {
115        self.close_date
116    }
117
118    pub fn is_closed(&self) -> bool {
119        self.is_closed
120    }
121}
122
123#[doc(hidden)]
124pub struct PollBuilder {
125    inner: Poll,
126}
127
128#[deprecated]
129pub type RTDPollBuilder = PollBuilder;
130
131impl PollBuilder {
132    pub fn build(&self) -> Poll {
133        self.inner.clone()
134    }
135
136    pub fn id(&mut self, id: i64) -> &mut Self {
137        self.inner.id = id;
138        self
139    }
140
141    pub fn question<T: AsRef<str>>(&mut self, question: T) -> &mut Self {
142        self.inner.question = question.as_ref().to_string();
143        self
144    }
145
146    pub fn options(&mut self, options: Vec<PollOption>) -> &mut Self {
147        self.inner.options = options;
148        self
149    }
150
151    pub fn total_voter_count(&mut self, total_voter_count: i32) -> &mut Self {
152        self.inner.total_voter_count = total_voter_count;
153        self
154    }
155
156    pub fn recent_voter_user_ids(&mut self, recent_voter_user_ids: Vec<i64>) -> &mut Self {
157        self.inner.recent_voter_user_ids = recent_voter_user_ids;
158        self
159    }
160
161    pub fn is_anonymous(&mut self, is_anonymous: bool) -> &mut Self {
162        self.inner.is_anonymous = is_anonymous;
163        self
164    }
165
166    pub fn type_<T: AsRef<PollType>>(&mut self, type_: T) -> &mut Self {
167        self.inner.type_ = type_.as_ref().clone();
168        self
169    }
170
171    pub fn open_period(&mut self, open_period: i32) -> &mut Self {
172        self.inner.open_period = open_period;
173        self
174    }
175
176    pub fn close_date(&mut self, close_date: i32) -> &mut Self {
177        self.inner.close_date = close_date;
178        self
179    }
180
181    pub fn is_closed(&mut self, is_closed: bool) -> &mut Self {
182        self.inner.is_closed = is_closed;
183        self
184    }
185}
186
187impl AsRef<Poll> for Poll {
188    fn as_ref(&self) -> &Poll {
189        self
190    }
191}
192
193impl AsRef<Poll> for PollBuilder {
194    fn as_ref(&self) -> &Poll {
195        &self.inner
196    }
197}