rtdlib 1.8.70

TDLib for rust
Documentation
use crate::errors::*;
use crate::types::*;
use uuid::Uuid;

use serde::de::{Deserialize, Deserializer};
use std::fmt::Debug;

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

/// Describes the type of poll
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum PollType {
  #[doc(hidden)]
  _Default(()),
  /// A poll in quiz mode, which has predefined correct answers
  Quiz(PollTypeQuiz),
  /// A regular poll
  Regular(PollTypeRegular),
}

impl Default for PollType {
  fn default() -> Self {
    PollType::_Default(())
  }
}

impl<'de> Deserialize<'de> for PollType {
  fn deserialize<D>(deserializer: D) -> Result<PollType, D::Error>
  where
    D: Deserializer<'de>,
  {
    use serde::de::Error;
    rtd_enum_deserialize!(
      PollType,
      (pollTypeQuiz, Quiz);
      (pollTypeRegular, Regular);

    )(deserializer)
  }
}

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

      _ => "-1",
    }
  }
  #[doc(hidden)]
  fn extra(&self) -> Option<String> {
    match self {
      PollType::Quiz(t) => t.extra(),
      PollType::Regular(t) => t.extra(),

      _ => None,
    }
  }
  fn to_json(&self) -> RTDResult<String> {
    Ok(serde_json::to_string(self)?)
  }
}

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

  pub fn is_quiz(&self) -> bool {
    matches!(self, PollType::Quiz(_))
  }
  pub fn is_regular(&self) -> bool {
    matches!(self, PollType::Regular(_))
  }

  pub fn on_quiz<F: FnOnce(&PollTypeQuiz)>(&self, fnc: F) -> &Self {
    if let PollType::Quiz(t) = self {
      fnc(t)
    };
    self
  }
  pub fn on_regular<F: FnOnce(&PollTypeRegular)>(&self, fnc: F) -> &Self {
    if let PollType::Regular(t) = self {
      fnc(t)
    };
    self
  }

  pub fn as_quiz(&self) -> Option<&PollTypeQuiz> {
    if let PollType::Quiz(t) = self {
      return Some(t);
    }
    None
  }
  pub fn as_regular(&self) -> Option<&PollTypeRegular> {
    if let PollType::Regular(t) = self {
      return Some(t);
    }
    None
  }

  pub fn quiz<T: AsRef<PollTypeQuiz>>(t: T) -> Self {
    PollType::Quiz(t.as_ref().clone())
  }

  pub fn regular<T: AsRef<PollTypeRegular>>(t: T) -> Self {
    PollType::Regular(t.as_ref().clone())
  }
}

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

/// A poll in quiz mode, which has predefined correct answers
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PollTypeQuiz {
  #[doc(hidden)]
  #[serde(rename(serialize = "@type", deserialize = "@type"))]
  td_name: String,
  #[doc(hidden)]
  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
  extra: Option<String>,
  /// Increasing list of 0-based identifiers of the correct answer options; empty for a yet unanswered poll
  correct_option_ids: Vec<i64>,
  /// Text that is shown when the user chooses an incorrect answer or taps on the lamp icon; empty for a yet unanswered poll
  explanation: FormattedText,
  /// Media that is shown when the user chooses an incorrect answer or taps on the lamp icon; may be null if none or the poll is unanswered yet. If present, currently, can be only of the types pollMediaAnimation, pollMediaAudio, pollMediaDocument, pollMediaLocation, pollMediaPhoto, pollMediaVenue, or pollMediaVideo
  explanation_media: Option<PollMedia>,
}

impl RObject for PollTypeQuiz {
  #[doc(hidden)]
  fn td_name(&self) -> &'static str {
    "pollTypeQuiz"
  }
  #[doc(hidden)]
  fn extra(&self) -> Option<String> {
    self.extra.clone()
  }
  fn to_json(&self) -> RTDResult<String> {
    Ok(serde_json::to_string(self)?)
  }
}

impl TDPollType for PollTypeQuiz {}

impl PollTypeQuiz {
  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> {
    Ok(serde_json::from_str(json.as_ref())?)
  }
  pub fn builder() -> RTDPollTypeQuizBuilder {
    let inner = PollTypeQuiz {
      td_name: "pollTypeQuiz".to_string(),
      extra: Some(Uuid::new_v4().to_string()),
      ..Default::default()
    };
    RTDPollTypeQuizBuilder { inner }
  }

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

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

  pub fn explanation_media(&self) -> &Option<PollMedia> {
    &self.explanation_media
  }
}

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

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

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

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

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

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

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

/// A regular poll
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PollTypeRegular {
  #[doc(hidden)]
  #[serde(rename(serialize = "@type", deserialize = "@type"))]
  td_name: String,
  #[doc(hidden)]
  #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
  extra: Option<String>,
}

impl RObject for PollTypeRegular {
  #[doc(hidden)]
  fn td_name(&self) -> &'static str {
    "pollTypeRegular"
  }
  #[doc(hidden)]
  fn extra(&self) -> Option<String> {
    self.extra.clone()
  }
  fn to_json(&self) -> RTDResult<String> {
    Ok(serde_json::to_string(self)?)
  }
}

impl TDPollType for PollTypeRegular {}

impl PollTypeRegular {
  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> {
    Ok(serde_json::from_str(json.as_ref())?)
  }
  pub fn builder() -> RTDPollTypeRegularBuilder {
    let inner = PollTypeRegular {
      td_name: "pollTypeRegular".to_string(),
      extra: Some(Uuid::new_v4().to_string()),
    };
    RTDPollTypeRegularBuilder { inner }
  }
}

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

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

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

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