Skip to main content

twilight_http/request/poll/
end_poll.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::Serialize;
10use std::future::IntoFuture;
11use twilight_model::{
12    channel::Message,
13    id::{
14        Id,
15        marker::{ChannelMarker, MessageMarker},
16    },
17};
18
19#[derive(Serialize)]
20struct EndPollFields {
21    channel_id: Id<ChannelMarker>,
22    message_id: Id<MessageMarker>,
23}
24
25// Ends a poll in a channel.
26#[must_use = "requests must be configured and executed"]
27pub struct EndPoll<'a> {
28    fields: EndPollFields,
29    http: &'a Client,
30}
31
32impl<'a> EndPoll<'a> {
33    pub(crate) const fn new(
34        http: &'a Client,
35        channel_id: Id<ChannelMarker>,
36        message_id: Id<MessageMarker>,
37    ) -> Self {
38        Self {
39            fields: EndPollFields {
40                channel_id,
41                message_id,
42            },
43            http,
44        }
45    }
46}
47
48#[cfg(not(target_os = "wasi"))]
49impl IntoFuture for EndPoll<'_> {
50    type Output = Result<Response<Message>, Error>;
51    type IntoFuture = ResponseFuture<Message>;
52
53    fn into_future(self) -> Self::IntoFuture {
54        let http = self.http;
55
56        match self.try_into_request() {
57            Ok(request) => http.request(request),
58            Err(source) => ResponseFuture::error(source),
59        }
60    }
61}
62
63impl TryIntoRequest for EndPoll<'_> {
64    fn try_into_request(self) -> Result<Request, Error> {
65        Ok(Request::from_route(&Route::EndPoll {
66            channel_id: self.fields.channel_id.get(),
67            message_id: self.fields.message_id.get(),
68        }))
69    }
70}