twilight_http/request/guild/
get_guild_prune_count.rs

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