use crate::{
client::Client,
error::Error,
request::{Request, TryIntoRequest},
response::{Response, ResponseFuture, marker::ListBody},
routing::Route,
};
use std::future::IntoFuture;
use twilight_model::{
guild::Ban,
id::{
Id,
marker::{GuildMarker, UserMarker},
},
};
use twilight_validate::request::{
ValidationError, get_guild_bans_limit as validate_get_guild_bans_limit,
};
struct GetBansFields {
after: Option<Id<UserMarker>>,
before: Option<Id<UserMarker>>,
limit: Option<u16>,
}
#[must_use = "requests must be configured and executed"]
pub struct GetBans<'a> {
fields: Result<GetBansFields, ValidationError>,
guild_id: Id<GuildMarker>,
http: &'a Client,
}
impl<'a> GetBans<'a> {
pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
Self {
fields: Ok(GetBansFields {
after: None,
before: None,
limit: None,
}),
guild_id,
http,
}
}
pub const fn after(mut self, user_id: Id<UserMarker>) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.after = Some(user_id);
}
self
}
pub const fn before(mut self, user_id: Id<UserMarker>) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.before = Some(user_id);
}
self
}
pub fn limit(mut self, limit: u16) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_get_guild_bans_limit(limit)?;
fields.limit.replace(limit);
Ok(fields)
});
self
}
}
impl IntoFuture for GetBans<'_> {
type Output = Result<Response<ListBody<Ban>>, Error>;
type IntoFuture = ResponseFuture<ListBody<Ban>>;
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 GetBans<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let fields = self.fields.map_err(Error::validation)?;
Ok(Request::from_route(&Route::GetBansWithParameters {
after: fields.after.map(Id::get),
before: fields.before.map(Id::get),
limit: fields.limit,
guild_id: self.guild_id.get(),
}))
}
}