titanium_model/
poll.rs

1//! Discord Poll Object structures.
2
3// Import removed
4
5use crate::builder::PollBuilder;
6use crate::TitanString;
7use serde::{Deserialize, Serialize};
8
9/// A poll object.
10#[derive(Debug, Clone, Deserialize, Serialize)]
11pub struct Poll<'a> {
12    /// The question of the poll.
13    pub question: PollMedia<'a>,
14
15    /// Each of the answers available in the poll.
16    pub answers: Vec<PollAnswer<'a>>,
17
18    /// The expiration date of the poll.
19    #[serde(default)]
20    pub expiry: Option<TitanString<'a>>,
21
22    /// Whether the poll allows multiple selections.
23    #[serde(default)]
24    pub allow_multiselect: bool,
25
26    /// The layout type of the poll.
27    #[serde(default)]
28    pub layout_type: Option<u8>,
29
30    /// The results of the poll.
31    #[serde(default)]
32    pub results: Option<PollResults>,
33}
34
35impl<'a> Poll<'a> {
36    /// Create a builder for a Poll.
37    pub fn builder(question: impl Into<TitanString<'a>>) -> PollBuilder<'a> {
38        PollBuilder::new(question)
39    }
40}
41
42/// A poll media object (question text/emoji).
43#[derive(Debug, Clone, Deserialize, Serialize)]
44pub struct PollMedia<'a> {
45    /// The text of the field.
46    #[serde(default)]
47    pub text: Option<TitanString<'a>>,
48
49    /// The emoji of the field.
50    #[serde(default)]
51    pub emoji: Option<crate::member::Emoji<'a>>,
52}
53
54/// A poll answer object.
55#[derive(Debug, Clone, Deserialize, Serialize)]
56pub struct PollAnswer<'a> {
57    /// The ID of the answer.
58    #[serde(default)]
59    // Read-only usually, but might be sent in some contexts? Docs say answer_id is integer.
60    pub answer_id: Option<u32>, // ID is usually an integer for poll answers, not snowflake? API says "integer".
61
62    /// The data of the answer.
63    pub poll_media: PollMedia<'a>,
64}
65
66/// The results of a poll.
67#[derive(Debug, Clone, Deserialize, Serialize)]
68pub struct PollResults {
69    /// Whether the poll is finalized.
70    #[serde(default)]
71    pub is_finalized: bool,
72
73    /// The counts for each answer.
74    #[serde(default)]
75    pub answer_counts: Vec<PollAnswerCount>,
76}
77
78/// Count for a specific answer.
79#[derive(Debug, Clone, Deserialize, Serialize)]
80pub struct PollAnswerCount {
81    /// The ID of the answer.
82    pub id: u32,
83
84    /// The number of votes.
85    pub count: u32,
86
87    /// Whether the current user voted for this answer.
88    #[serde(default)]
89    pub me_voted: bool,
90}