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