Skip to main content

twilight_http/request/channel/
create_pin.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::EmptyBody};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{self, AuditLogReason, Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::id::{
11    Id,
12    marker::{ChannelMarker, MessageMarker},
13};
14use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
15
16/// Create a new pin in a channel.
17#[must_use = "requests must be configured and executed"]
18pub struct CreatePin<'a> {
19    channel_id: Id<ChannelMarker>,
20    http: &'a Client,
21    message_id: Id<MessageMarker>,
22    reason: Result<Option<&'a str>, ValidationError>,
23}
24
25impl<'a> CreatePin<'a> {
26    pub(crate) const fn new(
27        http: &'a Client,
28        channel_id: Id<ChannelMarker>,
29        message_id: Id<MessageMarker>,
30    ) -> Self {
31        Self {
32            channel_id,
33            http,
34            message_id,
35            reason: Ok(None),
36        }
37    }
38}
39
40impl<'a> AuditLogReason<'a> for CreatePin<'a> {
41    fn reason(mut self, reason: &'a str) -> Self {
42        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
43
44        self
45    }
46}
47
48#[cfg(not(target_os = "wasi"))]
49impl IntoFuture for CreatePin<'_> {
50    type Output = Result<Response<EmptyBody>, Error>;
51
52    type IntoFuture = ResponseFuture<EmptyBody>;
53
54    fn into_future(self) -> Self::IntoFuture {
55        let http = self.http;
56
57        match self.try_into_request() {
58            Ok(request) => http.request(request),
59            Err(source) => ResponseFuture::error(source),
60        }
61    }
62}
63
64impl TryIntoRequest for CreatePin<'_> {
65    fn try_into_request(self) -> Result<Request, Error> {
66        let mut request = Request::builder(&Route::PinMessage {
67            channel_id: self.channel_id.get(),
68            message_id: self.message_id.get(),
69        });
70
71        if let Some(reason) = self.reason.map_err(Error::validation)? {
72            request = request.headers(request::audit_header(reason)?);
73        }
74
75        request.build()
76    }
77}