Skip to main content

twilight_http/request/channel/
delete_channel.rs

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