twilight_http/request/channel/
delete_channel.rs

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