Skip to main content

twilight_http/request/channel/invite/
delete_invite.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_validate::request::{ValidationError, audit_reason as validate_audit_reason};
11
12/// Delete an invite by its code.
13///
14/// Requires the [`MANAGE_CHANNELS`] permission on the channel this invite
15/// belongs to, or [`MANAGE_GUILD`] to remove any invite across the guild.
16///
17/// [`MANAGE_CHANNELS`]: twilight_model::guild::Permissions::MANAGE_CHANNELS
18/// [`MANAGE_GUILD`]: twilight_model::guild::Permissions::MANAGE_GUILD
19#[must_use = "requests must be configured and executed"]
20pub struct DeleteInvite<'a> {
21    code: &'a str,
22    http: &'a Client,
23    reason: Result<Option<&'a str>, ValidationError>,
24}
25
26impl<'a> DeleteInvite<'a> {
27    pub(crate) const fn new(http: &'a Client, code: &'a str) -> Self {
28        Self {
29            code,
30            http,
31            reason: Ok(None),
32        }
33    }
34}
35
36impl<'a> AuditLogReason<'a> for DeleteInvite<'a> {
37    fn reason(mut self, reason: &'a str) -> Self {
38        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
39
40        self
41    }
42}
43
44#[cfg(not(target_os = "wasi"))]
45impl IntoFuture for DeleteInvite<'_> {
46    type Output = Result<Response<EmptyBody>, Error>;
47
48    type IntoFuture = ResponseFuture<EmptyBody>;
49
50    fn into_future(self) -> Self::IntoFuture {
51        let http = self.http;
52
53        match self.try_into_request() {
54            Ok(request) => http.request(request),
55            Err(source) => ResponseFuture::error(source),
56        }
57    }
58}
59
60impl TryIntoRequest for DeleteInvite<'_> {
61    fn try_into_request(self) -> Result<Request, Error> {
62        let mut request = Request::builder(&Route::DeleteInvite { code: self.code });
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}