Skip to main content

twilight_http/request/channel/
delete_channel_permission_configured.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::{Id, marker::ChannelMarker};
11use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
12
13/// Clear the permissions for a target ID in a channel.
14///
15/// The `target_id` is a `u64`, but it should point to a `Id<RoleMarker>` or a `Id<UserMarker>`.
16#[must_use = "requests must be configured and executed"]
17pub struct DeleteChannelPermissionConfigured<'a> {
18    channel_id: Id<ChannelMarker>,
19    http: &'a Client,
20    reason: Result<Option<&'a str>, ValidationError>,
21    target_id: u64,
22}
23
24impl<'a> DeleteChannelPermissionConfigured<'a> {
25    pub(crate) const fn new(
26        http: &'a Client,
27        channel_id: Id<ChannelMarker>,
28        target_id: u64,
29    ) -> Self {
30        Self {
31            channel_id,
32            http,
33            reason: Ok(None),
34            target_id,
35        }
36    }
37}
38
39impl<'a> AuditLogReason<'a> for DeleteChannelPermissionConfigured<'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
47#[cfg(not(target_os = "wasi"))]
48impl IntoFuture for DeleteChannelPermissionConfigured<'_> {
49    type Output = Result<Response<EmptyBody>, Error>;
50
51    type IntoFuture = ResponseFuture<EmptyBody>;
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 DeleteChannelPermissionConfigured<'_> {
64    fn try_into_request(self) -> Result<Request, Error> {
65        let mut request = Request::builder(&Route::DeletePermissionOverwrite {
66            channel_id: self.channel_id.get(),
67            target_id: self.target_id,
68        });
69
70        if let Some(reason) = self.reason.map_err(Error::validation)? {
71            request = request.headers(request::audit_header(reason)?);
72        }
73
74        request.build()
75    }
76}