Skip to main content

twilight_http/request/poll/
get_answer_voters.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use serde::{Deserialize, Serialize};
10use std::future::IntoFuture;
11use twilight_model::{
12    id::{
13        Id,
14        marker::{ChannelMarker, MessageMarker, UserMarker},
15    },
16    user::User,
17};
18
19#[derive(Serialize)]
20struct GetAnswerVotersFields {
21    after: Option<Id<UserMarker>>,
22    answer_id: u8,
23    channel_id: Id<ChannelMarker>,
24    limit: Option<u8>,
25    message_id: Id<MessageMarker>,
26}
27
28/// Gets the data for a poll answer.
29#[must_use = "requests must be configured and executed"]
30pub struct GetAnswerVoters<'a> {
31    fields: GetAnswerVotersFields,
32    http: &'a Client,
33}
34
35#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
36pub struct GetAnswerVotersResponse {
37    pub users: Vec<User>,
38}
39
40impl<'a> GetAnswerVoters<'a> {
41    pub(crate) const fn new(
42        http: &'a Client,
43        channel_id: Id<ChannelMarker>,
44        message_id: Id<MessageMarker>,
45        answer_id: u8,
46    ) -> Self {
47        Self {
48            fields: GetAnswerVotersFields {
49                after: None,
50                answer_id,
51                channel_id,
52                limit: None,
53                message_id,
54            },
55            http,
56        }
57    }
58
59    /// Set the user ID to get voters after.
60    pub const fn after(mut self, after: Id<UserMarker>) -> Self {
61        self.fields.after.replace(after);
62
63        self
64    }
65
66    /// Set the limit of voters to get.
67    ///
68    /// The minimum is 1 and the maximum is 100.
69    pub const fn limit(mut self, limit: u8) -> Self {
70        self.fields.limit.replace(limit);
71
72        self
73    }
74}
75
76#[cfg(not(target_os = "wasi"))]
77impl IntoFuture for GetAnswerVoters<'_> {
78    type Output = Result<Response<GetAnswerVotersResponse>, Error>;
79    type IntoFuture = ResponseFuture<GetAnswerVotersResponse>;
80
81    fn into_future(self) -> Self::IntoFuture {
82        let http = self.http;
83
84        match self.try_into_request() {
85            Ok(request) => http.request(request),
86            Err(source) => ResponseFuture::error(source),
87        }
88    }
89}
90
91impl TryIntoRequest for GetAnswerVoters<'_> {
92    fn try_into_request(self) -> Result<Request, Error> {
93        Ok(Request::from_route(&Route::GetAnswerVoters {
94            after: self.fields.after.map(Id::get),
95            answer_id: self.fields.answer_id,
96            channel_id: self.fields.channel_id.get(),
97            limit: self.fields.limit,
98            message_id: self.fields.message_id.get(),
99        }))
100    }
101}