twilight_http/request/guild/
get_guild_prune_count.rs1use 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 Id,
13 marker::{GuildMarker, RoleMarker},
14 },
15};
16use twilight_validate::request::{ValidationError, guild_prune_days as validate_guild_prune_days};
17
18struct GetGuildPruneCountFields<'a> {
19 days: Option<u16>,
20 include_roles: &'a [Id<RoleMarker>],
21}
22
23#[must_use = "requests must be configured and executed"]
39pub struct GetGuildPruneCount<'a> {
40 fields: Result<GetGuildPruneCountFields<'a>, ValidationError>,
41 guild_id: Id<GuildMarker>,
42 http: &'a Client,
43}
44
45impl<'a> GetGuildPruneCount<'a> {
46 pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
47 Self {
48 fields: Ok(GetGuildPruneCountFields {
49 days: None,
50 include_roles: &[],
51 }),
52 guild_id,
53 http,
54 }
55 }
56
57 pub fn days(mut self, days: u16) -> Self {
69 self.fields = self.fields.and_then(|mut fields| {
70 validate_guild_prune_days(days)?;
71 fields.days = Some(days);
72
73 Ok(fields)
74 });
75
76 self
77 }
78
79 pub const fn include_roles(mut self, roles: &'a [Id<RoleMarker>]) -> Self {
81 if let Ok(fields) = self.fields.as_mut() {
82 fields.include_roles = roles;
83 }
84
85 self
86 }
87}
88
89impl IntoFuture for GetGuildPruneCount<'_> {
90 type Output = Result<Response<GuildPrune>, Error>;
91
92 type IntoFuture = ResponseFuture<GuildPrune>;
93
94 fn into_future(self) -> Self::IntoFuture {
95 let http = self.http;
96
97 match self.try_into_request() {
98 Ok(request) => http.request(request),
99 Err(source) => ResponseFuture::error(source),
100 }
101 }
102}
103
104impl TryIntoRequest for GetGuildPruneCount<'_> {
105 fn try_into_request(self) -> Result<Request, Error> {
106 let fields = self.fields.map_err(Error::validation)?;
107
108 Ok(Request::from_route(&Route::GetGuildPruneCount {
109 days: fields.days,
110 guild_id: self.guild_id.get(),
111 include_roles: fields.include_roles,
112 }))
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::GetGuildPruneCount;
119 use crate::{Client, request::TryIntoRequest};
120 use twilight_model::id::Id;
121
122 #[test]
123 fn days() {
124 fn days_valid(days: u16) -> bool {
125 let client = Client::new(String::new());
126
127 GetGuildPruneCount::new(&client, Id::new(1))
128 .days(days)
129 .try_into_request()
130 .is_ok()
131 }
132
133 assert!(!days_valid(0));
134 assert!(days_valid(1));
135 assert!(!days_valid(u16::MAX));
136 }
137}