Skip to main content

twilight_http/request/guild/
update_guild_channel_positions.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{AuditLogReason, Request, TryIntoRequest},
5    response::{Response, ResponseFuture, marker::EmptyBody},
6    routing::Route,
7};
8use std::future::IntoFuture;
9use twilight_model::{
10    http::channel_position::Position,
11    id::{Id, marker::GuildMarker},
12};
13use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
14
15/// Modify the positions of the channels.
16///
17/// The minimum amount of channels to modify, is a swap between two channels.
18#[must_use = "requests must be configured and executed"]
19pub struct UpdateGuildChannelPositions<'a> {
20    guild_id: Id<GuildMarker>,
21    http: &'a Client,
22    positions: &'a [Position],
23    reason: Result<Option<&'a str>, ValidationError>,
24}
25
26impl<'a> UpdateGuildChannelPositions<'a> {
27    pub(crate) const fn new(
28        http: &'a Client,
29        guild_id: Id<GuildMarker>,
30        channel_positions: &'a [Position],
31    ) -> Self {
32        Self {
33            guild_id,
34            http,
35            positions: channel_positions,
36            reason: Ok(None),
37        }
38    }
39}
40
41impl<'a> AuditLogReason<'a> for UpdateGuildChannelPositions<'a> {
42    fn reason(mut self, reason: &'a str) -> Self {
43        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
44
45        self
46    }
47}
48
49impl IntoFuture for UpdateGuildChannelPositions<'_> {
50    type Output = Result<Response<EmptyBody>, Error>;
51
52    type IntoFuture = ResponseFuture<EmptyBody>;
53
54    fn into_future(self) -> Self::IntoFuture {
55        let http = self.http;
56
57        match self.try_into_request() {
58            Ok(request) => http.request(request),
59            Err(source) => ResponseFuture::error(source),
60        }
61    }
62}
63
64impl TryIntoRequest for UpdateGuildChannelPositions<'_> {
65    fn try_into_request(self) -> Result<Request, Error> {
66        Request::builder(&Route::UpdateGuildChannels {
67            guild_id: self.guild_id.get(),
68        })
69        .json(&self.positions)
70        .build()
71    }
72}