twilight_http/request/channel/invite/
create_invite.rs1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture};
3use crate::{
4 client::Client,
5 error::Error,
6 request::{self, AuditLogReason, Request, TryIntoRequest},
7 routing::Route,
8};
9use serde::Serialize;
10use std::future::IntoFuture;
11use twilight_model::{
12 guild::invite::{Invite, TargetType},
13 id::{
14 Id,
15 marker::{ApplicationMarker, ChannelMarker, UserMarker},
16 },
17};
18use twilight_validate::request::{
19 ValidationError, audit_reason as validate_audit_reason,
20 invite_max_age as validate_invite_max_age, invite_max_uses as validate_invite_max_uses,
21};
22
23#[derive(Serialize)]
24struct CreateInviteFields {
25 #[serde(skip_serializing_if = "Option::is_none")]
26 max_age: Option<u32>,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 max_uses: Option<u16>,
29 #[serde(skip_serializing_if = "Option::is_none")]
30 temporary: Option<bool>,
31 #[serde(skip_serializing_if = "Option::is_none")]
32 target_application_id: Option<Id<ApplicationMarker>>,
33 #[serde(skip_serializing_if = "Option::is_none")]
34 target_user_id: Option<Id<UserMarker>>,
35 #[serde(skip_serializing_if = "Option::is_none")]
36 target_type: Option<TargetType>,
37 #[serde(skip_serializing_if = "Option::is_none")]
38 unique: Option<bool>,
39}
40
41#[must_use = "requests must be configured and executed"]
62pub struct CreateInvite<'a> {
63 channel_id: Id<ChannelMarker>,
64 fields: Result<CreateInviteFields, ValidationError>,
65 http: &'a Client,
66 reason: Result<Option<&'a str>, ValidationError>,
67}
68
69impl<'a> CreateInvite<'a> {
70 pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
71 Self {
72 channel_id,
73 fields: Ok(CreateInviteFields {
74 max_age: None,
75 max_uses: None,
76 temporary: None,
77 target_application_id: None,
78 target_user_id: None,
79 target_type: None,
80 unique: None,
81 }),
82 http,
83 reason: Ok(None),
84 }
85 }
86
87 pub fn max_age(mut self, max_age: u32) -> Self {
122 self.fields = self.fields.and_then(|mut fields| {
123 validate_invite_max_age(max_age)?;
124 fields.max_age = Some(max_age);
125
126 Ok(fields)
127 });
128
129 self
130 }
131
132 pub fn max_uses(mut self, max_uses: u16) -> Self {
165 self.fields = self.fields.and_then(|mut fields| {
166 validate_invite_max_uses(max_uses)?;
167 fields.max_uses = Some(max_uses);
168
169 Ok(fields)
170 });
171
172 self
173 }
174
175 pub const fn target_application_id(
181 mut self,
182 target_application_id: Id<ApplicationMarker>,
183 ) -> Self {
184 if let Ok(fields) = self.fields.as_mut() {
185 fields.target_application_id = Some(target_application_id);
186 }
187
188 self
189 }
190
191 pub const fn target_user_id(mut self, target_user_id: Id<UserMarker>) -> Self {
193 if let Ok(fields) = self.fields.as_mut() {
194 fields.target_user_id = Some(target_user_id);
195 }
196
197 self
198 }
199
200 pub const fn target_type(mut self, target_type: TargetType) -> Self {
202 if let Ok(fields) = self.fields.as_mut() {
203 fields.target_type = Some(target_type);
204 }
205
206 self
207 }
208
209 pub const fn temporary(mut self, temporary: bool) -> Self {
213 if let Ok(fields) = self.fields.as_mut() {
214 fields.temporary = Some(temporary);
215 }
216
217 self
218 }
219
220 pub const fn unique(mut self, unique: bool) -> Self {
227 if let Ok(fields) = self.fields.as_mut() {
228 fields.unique = Some(unique);
229 }
230
231 self
232 }
233}
234
235impl<'a> AuditLogReason<'a> for CreateInvite<'a> {
236 fn reason(mut self, reason: &'a str) -> Self {
237 self.reason = validate_audit_reason(reason).and(Ok(Some(reason)));
238
239 self
240 }
241}
242
243#[cfg(not(target_os = "wasi"))]
244impl IntoFuture for CreateInvite<'_> {
245 type Output = Result<Response<Invite>, Error>;
246
247 type IntoFuture = ResponseFuture<Invite>;
248
249 fn into_future(self) -> Self::IntoFuture {
250 let http = self.http;
251
252 match self.try_into_request() {
253 Ok(request) => http.request(request),
254 Err(source) => ResponseFuture::error(source),
255 }
256 }
257}
258
259impl TryIntoRequest for CreateInvite<'_> {
260 fn try_into_request(self) -> Result<Request, Error> {
261 let fields = self.fields.map_err(Error::validation)?;
262 let mut request = Request::builder(&Route::CreateInvite {
263 channel_id: self.channel_id.get(),
264 })
265 .json(&fields);
266
267 if let Some(reason) = self.reason.map_err(Error::validation)? {
268 request = request.headers(request::audit_header(reason)?);
269 }
270
271 request.build()
272 }
273}
274
275#[cfg(test)]
276mod tests {
277 use super::CreateInvite;
278 use crate::Client;
279 use std::error::Error;
280 use twilight_model::id::Id;
281
282 #[test]
283 fn max_age() -> Result<(), Box<dyn Error>> {
284 let client = Client::new("foo".to_owned());
285 let mut builder = CreateInvite::new(&client, Id::new(1)).max_age(0);
286 assert_eq!(Some(0), builder.fields.as_ref().unwrap().max_age);
287 builder = builder.max_age(604_800);
288 assert_eq!(Some(604_800), builder.fields.as_ref().unwrap().max_age);
289 builder = builder.max_age(604_801);
290 assert!(builder.fields.is_err());
291
292 Ok(())
293 }
294
295 #[test]
296 fn max_uses() -> Result<(), Box<dyn Error>> {
297 let client = Client::new("foo".to_owned());
298 let mut builder = CreateInvite::new(&client, Id::new(1)).max_uses(0);
299 assert_eq!(Some(0), builder.fields.as_ref().unwrap().max_uses);
300 builder = builder.max_uses(100);
301 assert_eq!(Some(100), builder.fields.as_ref().unwrap().max_uses);
302 builder = builder.max_uses(101);
303 assert!(builder.fields.is_err());
304
305 Ok(())
306 }
307}