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

/// Describes a poll
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Poll {
    #[doc(hidden)]
    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
    extra: Option<String>,
    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
    client_id: Option<i32>,
    /// Unique poll identifier

    #[serde(
        deserialize_with = "super::_common::number_from_string",
        serialize_with = "super::_common::string_to_number"
    )]
    #[serde(default)]
    id: i64,
    /// Poll question; 1-300 characters

    #[serde(default)]
    question: String,
    /// List of poll answer options

    #[serde(default)]
    options: Vec<PollOption>,
    /// Total number of voters, participating in the poll

    #[serde(default)]
    total_voter_count: i32,
    /// User identifiers of recent voters, if the poll is non-anonymous

    #[serde(default)]
    recent_voter_user_ids: Vec<i64>,
    /// True, if the poll is anonymous

    #[serde(default)]
    is_anonymous: bool,
    /// Type of the poll

    #[serde(rename(serialize = "type", deserialize = "type"))]
    #[serde(skip_serializing_if = "PollType::_is_default")]
    type_: PollType,
    /// Amount of time the poll will be active after creation, in seconds

    #[serde(default)]
    open_period: i32,
    /// Point in time (Unix timestamp) when the poll will automatically be closed

    #[serde(default)]
    close_date: i32,
    /// True, if the poll is closed

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

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

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

        PollBuilder { inner }
    }

    pub fn id(&self) -> i64 {
        self.id
    }

    pub fn question(&self) -> &String {
        &self.question
    }

    pub fn options(&self) -> &Vec<PollOption> {
        &self.options
    }

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

    pub fn recent_voter_user_ids(&self) -> &Vec<i64> {
        &self.recent_voter_user_ids
    }

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

    pub fn type_(&self) -> &PollType {
        &self.type_
    }

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

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

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

#[doc(hidden)]
pub struct PollBuilder {
    inner: Poll,
}

#[deprecated]
pub type RTDPollBuilder = PollBuilder;

impl PollBuilder {
    pub fn build(&self) -> Poll {
        self.inner.clone()
    }

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

    pub fn question<T: AsRef<str>>(&mut self, question: T) -> &mut Self {
        self.inner.question = question.as_ref().to_string();
        self
    }

    pub fn options(&mut self, options: Vec<PollOption>) -> &mut Self {
        self.inner.options = options;
        self
    }

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

    pub fn recent_voter_user_ids(&mut self, recent_voter_user_ids: Vec<i64>) -> &mut Self {
        self.inner.recent_voter_user_ids = recent_voter_user_ids;
        self
    }

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

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

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

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

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

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

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