Skip to content

Commit

Permalink
Rework EditStageInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Jul 7, 2022
1 parent 090af5b commit eff57b6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/builder/create_stage_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::internal::prelude::*;
use crate::model::prelude::*;

/// Builder for creating a [`StageInstance`].
///
/// [`StageInstance`]: crate::model::channel::StageInstance
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct CreateStageInstance {
Expand Down
46 changes: 43 additions & 3 deletions src/builder/edit_stage_instance.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
#[cfg(feature = "http")]
use crate::http::{CacheHttp, Http};
#[cfg(feature = "http")]
use crate::internal::prelude::*;
#[cfg(feature = "http")]
use crate::model::prelude::*;

/// Edits a [`StageInstance`].
///
/// [`StageInstance`]: crate::model::channel::StageInstance
#[derive(Clone, Debug, Default, Serialize)]
#[must_use]
pub struct EditStageInstance {
#[serde(skip_serializing_if = "Option::is_none")]
topic: Option<String>,
}

impl EditStageInstance {
/// Edits the stage instance
///
/// # Errors
///
/// Returns [`ModelError::InvalidChannelType`] if the channel is not a stage channel.
///
/// Returns [`Error::Http`] if the channel is not a stage channel, or there is no stage
/// instance currently.
#[cfg(feature = "http")]
#[inline]
pub async fn execute(
self,
cache_http: impl CacheHttp,
channel_id: ChannelId,
) -> Result<StageInstance> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
if let Some(channel) = cache.guild_channel(channel_id) {
if channel.kind != ChannelType::Stage {
return Err(Error::Model(ModelError::InvalidChannelType));
}
}
}
}

self._execute(cache_http.http(), channel_id).await
}

#[cfg(feature = "http")]
async fn _execute(self, http: &Http, channel_id: ChannelId) -> Result<StageInstance> {
http.edit_stage_instance(channel_id.into(), &self).await
}

/// Sets the topic of the stage channel instance.
pub fn topic(&mut self, topic: impl Into<String>) -> &mut Self {
pub fn topic(mut self, topic: impl Into<String>) -> Self {
self.topic = Some(topic.into());
self
}
Expand Down
26 changes: 11 additions & 15 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,24 +887,20 @@ impl ChannelId {
builder.channel_id(self).execute(cache_http).await
}

/// Edits a stage instance.
/// Edits the stage instance
///
/// # Errors
///
/// Returns [`Error::Http`] if the channel is not a stage channel,
/// or if there is not stage instance currently.
pub async fn edit_stage_instance<F>(
&self,
http: impl AsRef<Http>,
f: F,
) -> Result<StageInstance>
where
F: FnOnce(&mut EditStageInstance) -> &mut EditStageInstance,
{
let mut instance = EditStageInstance::default();
f(&mut instance);

http.as_ref().edit_stage_instance(self.get(), &instance).await
/// Returns [`ModelError::InvalidChannelType`] if the channel is not a stage channel.
///
/// Returns [`Error::Http`] if the channel is not a stage channel, or there is no stage
/// instance currently.
pub async fn edit_stage_instance(
self,
cache_http: impl CacheHttp,
builder: EditStageInstance,
) -> Result<StageInstance> {
builder.execute(cache_http, self).await
}

/// Edits a thread.
Expand Down
23 changes: 9 additions & 14 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,25 +1143,20 @@ impl GuildChannel {
self.id.create_stage_instance(cache_http, builder).await
}

/// Edits a stage instance.
/// Edits the stage instance
///
/// # Errors
///
/// Returns [`ModelError::InvalidChannelType`] if the channel is not a stage channel.
/// Returns [`Error::Http`] if there is no stage instance currently.
pub async fn edit_stage_instance<F>(
///
/// Returns [`Error::Http`] if the channel is not a stage channel, or there is no stage
/// instance currently.
pub async fn edit_stage_instance(
&self,
http: impl AsRef<Http>,
f: F,
) -> Result<StageInstance>
where
F: FnOnce(&mut EditStageInstance) -> &mut EditStageInstance,
{
if self.kind != ChannelType::Stage {
return Err(Error::Model(ModelError::InvalidChannelType));
}

self.id.edit_stage_instance(http, f).await
cache_http: impl CacheHttp,
builder: EditStageInstance,
) -> Result<StageInstance> {
self.id.edit_stage_instance(cache_http, builder).await
}

/// Deletes a stage instance.
Expand Down

0 comments on commit eff57b6

Please sign in to comment.