Skip to main content

twilight_http/request/guild/
get_guild_prune_count.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    guild::GuildPrune,
12    id::{
13        Id,
14        marker::{GuildMarker, RoleMarker},
15    },
16};
17use twilight_validate::request::{ValidationError, guild_prune_days as validate_guild_prune_days};
18
19struct GetGuildPruneCountFields<'a> {
20    days: Option<u16>,
21    include_roles: &'a [Id<RoleMarker>],
22}
23
24/// Get the counts of guild members to be pruned.
25#[must_use = "requests must be configured and executed"]
26pub struct GetGuildPruneCount<'a> {
27    fields: Result<GetGuildPruneCountFields<'a>, ValidationError>,
28    guild_id: Id<GuildMarker>,
29    http: &'a Client,
30}
31
32impl<'a> GetGuildPruneCount<'a> {
33    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
34        Self {
35            fields: Ok(GetGuildPruneCountFields {
36                days: None,
37                include_roles: &[],
38            }),
39            guild_id,
40            http,
41        }
42    }
43
44    /// Set the number of days that a user must be inactive before being
45    /// able to be pruned.
46    ///
47    /// The number of days must be greater than 0, and less than or equal to 30.
48    ///
49    /// # Errors
50    ///
51    /// Returns an error of type [`GuildPruneDays`] if the number of days is 0
52    /// or more than 30.
53    ///
54    /// [`GuildPruneDays`]: twilight_validate::request::ValidationErrorType::GuildPruneDays
55    pub fn days(mut self, days: u16) -> Self {
56        self.fields = self.fields.and_then(|mut fields| {
57            validate_guild_prune_days(days)?;
58            fields.days = Some(days);
59
60            Ok(fields)
61        });
62
63        self
64    }
65
66    /// List of roles to include when calculating prune count
67    pub const fn include_roles(mut self, roles: &'a [Id<RoleMarker>]) -> Self {
68        if let Ok(fields) = self.fields.as_mut() {
69            fields.include_roles = roles;
70        }
71
72        self
73    }
74}
75
76#[cfg(not(target_os = "wasi"))]
77impl IntoFuture for GetGuildPruneCount<'_> {
78    type Output = Result<Response<GuildPrune>, Error>;
79
80    type IntoFuture = ResponseFuture<GuildPrune>;
81
82    fn into_future(self) -> Self::IntoFuture {
83        let http = self.http;
84
85        match self.try_into_request() {
86            Ok(request) => http.request(request),
87            Err(source) => ResponseFuture::error(source),
88        }
89    }
90}
91
92impl TryIntoRequest for GetGuildPruneCount<'_> {
93    fn try_into_request(self) -> Result<Request, Error> {
94        let fields = self.fields.map_err(Error::validation)?;
95
96        Ok(Request::from_route(&Route::GetGuildPruneCount {
97            days: fields.days,
98            guild_id: self.guild_id.get(),
99            include_roles: fields.include_roles,
100        }))
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::GetGuildPruneCount;
107    use crate::{Client, request::TryIntoRequest};
108    use twilight_model::id::Id;
109
110    #[test]
111    fn days() {
112        fn days_valid(days: u16) -> bool {
113            let client = Client::new(String::new());
114
115            GetGuildPruneCount::new(&client, Id::new(1))
116                .days(days)
117                .try_into_request()
118                .is_ok()
119        }
120
121        assert!(!days_valid(0));
122        assert!(days_valid(1));
123        assert!(!days_valid(u16::MAX));
124    }
125}