use crate::{
client::Client,
error::Error,
request::{Request, TryIntoRequest},
response::{Response, ResponseFuture},
routing::Route,
};
use serde::Serialize;
use std::future::IntoFuture;
use twilight_model::{
channel::{stage_instance::PrivacyLevel, StageInstance},
id::{marker::ChannelMarker, Id},
};
use twilight_validate::request::{stage_topic as validate_stage_topic, ValidationError};
#[derive(Serialize)]
struct CreateStageInstanceFields<'a> {
channel_id: Id<ChannelMarker>,
#[serde(skip_serializing_if = "Option::is_none")]
privacy_level: Option<PrivacyLevel>,
#[serde(skip_serializing_if = "Option::is_none")]
send_start_notification: Option<bool>,
topic: &'a str,
}
#[must_use = "requests must be configured and executed"]
pub struct CreateStageInstance<'a> {
fields: CreateStageInstanceFields<'a>,
http: &'a Client,
}
impl<'a> CreateStageInstance<'a> {
pub(crate) fn new(
http: &'a Client,
channel_id: Id<ChannelMarker>,
topic: &'a str,
) -> Result<Self, ValidationError> {
validate_stage_topic(topic)?;
Ok(Self {
fields: CreateStageInstanceFields {
channel_id,
privacy_level: None,
send_start_notification: None,
topic,
},
http,
})
}
pub const fn privacy_level(mut self, privacy_level: PrivacyLevel) -> Self {
self.fields.privacy_level = Some(privacy_level);
self
}
pub const fn send_start_notification(mut self, send_start_notification: bool) -> Self {
self.fields.send_start_notification = Some(send_start_notification);
self
}
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
pub fn exec(self) -> ResponseFuture<StageInstance> {
self.into_future()
}
}
impl IntoFuture for CreateStageInstance<'_> {
type Output = Result<Response<StageInstance>, Error>;
type IntoFuture = ResponseFuture<StageInstance>;
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 CreateStageInstance<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let mut request = Request::builder(&Route::CreateStageInstance);
request = request.json(&self.fields)?;
Ok(request.build())
}
}