Skip to main content

twilight_http/request/guild/role/
update_role_positions.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::ListBody};
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    guild::{Role, RolePosition},
12    id::{Id, marker::GuildMarker},
13};
14use twilight_validate::request::{ValidationError, audit_reason as validate_audit_reason};
15
16/// Modify the position of the roles.
17///
18/// The minimum amount of roles to modify, is a swap between two roles.
19#[must_use = "requests must be configured and executed"]
20pub struct UpdateRolePositions<'a> {
21    guild_id: Id<GuildMarker>,
22    http: &'a Client,
23    roles: &'a [RolePosition],
24    reason: Result<Option<&'a str>, ValidationError>,
25}
26
27impl<'a> UpdateRolePositions<'a> {
28    pub(crate) const fn new(
29        http: &'a Client,
30        guild_id: Id<GuildMarker>,
31        roles: &'a [RolePosition],
32    ) -> Self {
33        Self {
34            guild_id,
35            http,
36            roles,
37            reason: Ok(None),
38        }
39    }
40}
41
42impl<'a> AuditLogReason<'a> for UpdateRolePositions<'a> {
43    fn reason(mut self, reason: &'a str) -> Self {
44        self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
45
46        self
47    }
48}
49
50#[cfg(not(target_os = "wasi"))]
51impl IntoFuture for UpdateRolePositions<'_> {
52    type Output = Result<Response<ListBody<Role>>, Error>;
53
54    type IntoFuture = ResponseFuture<ListBody<Role>>;
55
56    fn into_future(self) -> Self::IntoFuture {
57        let http = self.http;
58
59        match self.try_into_request() {
60            Ok(request) => http.request(request),
61            Err(source) => ResponseFuture::error(source),
62        }
63    }
64}
65
66impl TryIntoRequest for UpdateRolePositions<'_> {
67    fn try_into_request(self) -> Result<Request, Error> {
68        let mut request = Request::builder(&Route::UpdateRolePositions {
69            guild_id: self.guild_id.get(),
70        })
71        .json(&self.roles);
72
73        if let Some(reason) = self.reason.map_err(Error::validation)? {
74            request = request.headers(request::audit_header(reason)?);
75        }
76
77        request.build()
78    }
79}