use crate::errors::*;
use crate::types::*;
use uuid::Uuid;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Poll {
#[doc(hidden)]
#[serde(rename(serialize = "@type", deserialize = "@type"))]
td_name: String,
#[doc(hidden)]
#[serde(rename(serialize = "@extra", deserialize = "@extra"))]
extra: Option<String>,
#[serde(deserialize_with = "serde_aux::field_attributes::deserialize_number_from_string")]
id: isize,
question: FormattedText,
options: Vec<PollOption>,
total_voter_count: i64,
recent_voter_ids: Vec<MessageSender>,
can_get_voters: bool,
can_see_results: bool,
is_anonymous: bool,
allows_multiple_answers: bool,
allows_revoting: bool,
members_only: bool,
country_codes: Vec<String>,
option_order: Vec<i64>,
#[serde(rename(serialize = "type", deserialize = "type"))]
type_: PollType,
open_period: i64,
close_date: i64,
is_closed: bool,
vote_restriction_reason: Option<PollVoteRestrictionReason>,
}
impl RObject for Poll {
#[doc(hidden)]
fn td_name(&self) -> &'static str {
"poll"
}
#[doc(hidden)]
fn extra(&self) -> Option<String> {
self.extra.clone()
}
fn to_json(&self) -> RTDResult<String> {
Ok(serde_json::to_string(self)?)
}
}
impl Poll {
pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> {
Ok(serde_json::from_str(json.as_ref())?)
}
pub fn builder() -> RTDPollBuilder {
let inner = Poll {
td_name: "poll".to_string(),
extra: Some(Uuid::new_v4().to_string()),
..Default::default()
};
RTDPollBuilder { inner }
}
pub fn id(&self) -> isize {
self.id
}
pub fn question(&self) -> &FormattedText {
&self.question
}
pub fn options(&self) -> &Vec<PollOption> {
&self.options
}
pub fn total_voter_count(&self) -> i64 {
self.total_voter_count
}
pub fn recent_voter_ids(&self) -> &Vec<MessageSender> {
&self.recent_voter_ids
}
pub fn can_get_voters(&self) -> bool {
self.can_get_voters
}
pub fn can_see_results(&self) -> bool {
self.can_see_results
}
pub fn is_anonymous(&self) -> bool {
self.is_anonymous
}
pub fn allows_multiple_answers(&self) -> bool {
self.allows_multiple_answers
}
pub fn allows_revoting(&self) -> bool {
self.allows_revoting
}
pub fn members_only(&self) -> bool {
self.members_only
}
pub fn country_codes(&self) -> &Vec<String> {
&self.country_codes
}
pub fn option_order(&self) -> &Vec<i64> {
&self.option_order
}
pub fn type_(&self) -> &PollType {
&self.type_
}
pub fn open_period(&self) -> i64 {
self.open_period
}
pub fn close_date(&self) -> i64 {
self.close_date
}
pub fn is_closed(&self) -> bool {
self.is_closed
}
pub fn vote_restriction_reason(&self) -> &Option<PollVoteRestrictionReason> {
&self.vote_restriction_reason
}
}
#[doc(hidden)]
pub struct RTDPollBuilder {
inner: Poll,
}
impl RTDPollBuilder {
pub fn build(&self) -> Poll {
self.inner.clone()
}
pub fn id(&mut self, id: isize) -> &mut Self {
self.inner.id = id;
self
}
pub fn question<T: AsRef<FormattedText>>(&mut self, question: T) -> &mut Self {
self.inner.question = question.as_ref().clone();
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: i64) -> &mut Self {
self.inner.total_voter_count = total_voter_count;
self
}
pub fn recent_voter_ids(&mut self, recent_voter_ids: Vec<MessageSender>) -> &mut Self {
self.inner.recent_voter_ids = recent_voter_ids;
self
}
pub fn can_get_voters(&mut self, can_get_voters: bool) -> &mut Self {
self.inner.can_get_voters = can_get_voters;
self
}
pub fn can_see_results(&mut self, can_see_results: bool) -> &mut Self {
self.inner.can_see_results = can_see_results;
self
}
pub fn is_anonymous(&mut self, is_anonymous: bool) -> &mut Self {
self.inner.is_anonymous = is_anonymous;
self
}
pub fn allows_multiple_answers(&mut self, allows_multiple_answers: bool) -> &mut Self {
self.inner.allows_multiple_answers = allows_multiple_answers;
self
}
pub fn allows_revoting(&mut self, allows_revoting: bool) -> &mut Self {
self.inner.allows_revoting = allows_revoting;
self
}
pub fn members_only(&mut self, members_only: bool) -> &mut Self {
self.inner.members_only = members_only;
self
}
pub fn country_codes(&mut self, country_codes: Vec<String>) -> &mut Self {
self.inner.country_codes = country_codes;
self
}
pub fn option_order(&mut self, option_order: Vec<i64>) -> &mut Self {
self.inner.option_order = option_order;
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: i64) -> &mut Self {
self.inner.open_period = open_period;
self
}
pub fn close_date(&mut self, close_date: i64) -> &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
}
pub fn vote_restriction_reason<T: AsRef<PollVoteRestrictionReason>>(
&mut self,
vote_restriction_reason: T,
) -> &mut Self {
self.inner.vote_restriction_reason = Some(vote_restriction_reason.as_ref().clone());
self
}
}
impl AsRef<Poll> for Poll {
fn as_ref(&self) -> &Poll {
self
}
}
impl AsRef<Poll> for RTDPollBuilder {
fn as_ref(&self) -> &Poll {
&self.inner
}
}