use crate::{
client::Client,
error::Error,
request::{self, AuditLogReason, Request, TryIntoRequest},
response::{Response, ResponseFuture},
routing::Route,
};
use serde::Serialize;
use std::future::IntoFuture;
use twilight_model::{
guild::invite::{Invite, TargetType},
id::{
marker::{ApplicationMarker, ChannelMarker, UserMarker},
Id,
},
};
use twilight_validate::request::{
audit_reason as validate_audit_reason, invite_max_age as validate_invite_max_age,
invite_max_uses as validate_invite_max_uses, ValidationError,
};
#[derive(Serialize)]
struct CreateInviteFields {
#[serde(skip_serializing_if = "Option::is_none")]
max_age: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
max_uses: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
temporary: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
target_application_id: Option<Id<ApplicationMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
target_user_id: Option<Id<UserMarker>>,
#[serde(skip_serializing_if = "Option::is_none")]
target_type: Option<TargetType>,
#[serde(skip_serializing_if = "Option::is_none")]
unique: Option<bool>,
}
#[must_use = "requests must be configured and executed"]
pub struct CreateInvite<'a> {
channel_id: Id<ChannelMarker>,
fields: CreateInviteFields,
http: &'a Client,
reason: Option<&'a str>,
}
impl<'a> CreateInvite<'a> {
pub(crate) const fn new(http: &'a Client, channel_id: Id<ChannelMarker>) -> Self {
Self {
channel_id,
fields: CreateInviteFields {
max_age: None,
max_uses: None,
temporary: None,
target_application_id: None,
target_user_id: None,
target_type: None,
unique: None,
},
http,
reason: None,
}
}
pub const fn max_age(mut self, max_age: u32) -> Result<Self, ValidationError> {
#[allow(clippy::question_mark)]
if let Err(source) = validate_invite_max_age(max_age) {
return Err(source);
}
self.fields.max_age = Some(max_age);
Ok(self)
}
pub const fn max_uses(mut self, max_uses: u16) -> Result<Self, ValidationError> {
#[allow(clippy::question_mark)]
if let Err(source) = validate_invite_max_uses(max_uses) {
return Err(source);
}
self.fields.max_uses = Some(max_uses);
Ok(self)
}
pub const fn target_application_id(
mut self,
target_application_id: Id<ApplicationMarker>,
) -> Self {
self.fields.target_application_id = Some(target_application_id);
self
}
pub const fn target_user_id(mut self, target_user_id: Id<UserMarker>) -> Self {
self.fields.target_user_id = Some(target_user_id);
self
}
pub const fn target_type(mut self, target_type: TargetType) -> Self {
self.fields.target_type = Some(target_type);
self
}
pub const fn temporary(mut self, temporary: bool) -> Self {
self.fields.temporary = Some(temporary);
self
}
pub const fn unique(mut self, unique: bool) -> Self {
self.fields.unique = Some(unique);
self
}
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
pub fn exec(self) -> ResponseFuture<Invite> {
self.into_future()
}
}
impl<'a> AuditLogReason<'a> for CreateInvite<'a> {
fn reason(mut self, reason: &'a str) -> Result<Self, ValidationError> {
validate_audit_reason(reason)?;
self.reason.replace(reason);
Ok(self)
}
}
impl IntoFuture for CreateInvite<'_> {
type Output = Result<Response<Invite>, Error>;
type IntoFuture = ResponseFuture<Invite>;
fn into_future(self) -> Self::IntoFuture {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}
impl TryIntoRequest for CreateInvite<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let mut request = Request::builder(&Route::CreateInvite {
channel_id: self.channel_id.get(),
});
request = request.json(&self.fields)?;
if let Some(reason) = self.reason {
let header = request::audit_header(reason)?;
request = request.headers(header);
}
Ok(request.build())
}
}
#[cfg(test)]
mod tests {
use super::CreateInvite;
use crate::Client;
use std::error::Error;
use twilight_model::id::Id;
#[test]
fn max_age() -> Result<(), Box<dyn Error>> {
let client = Client::new("foo".to_owned());
let mut builder = CreateInvite::new(&client, Id::new(1)).max_age(0)?;
assert_eq!(Some(0), builder.fields.max_age);
builder = builder.max_age(604_800)?;
assert_eq!(Some(604_800), builder.fields.max_age);
assert!(builder.max_age(604_801).is_err());
Ok(())
}
#[test]
fn max_uses() -> Result<(), Box<dyn Error>> {
let client = Client::new("foo".to_owned());
let mut builder = CreateInvite::new(&client, Id::new(1)).max_uses(0)?;
assert_eq!(Some(0), builder.fields.max_uses);
builder = builder.max_uses(100)?;
assert_eq!(Some(100), builder.fields.max_uses);
assert!(builder.max_uses(101).is_err());
Ok(())
}
}