Skip to content

Commit

Permalink
Unify execute and execute_with_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Jun 17, 2022
1 parent 32d0244 commit f81e5e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
21 changes: 6 additions & 15 deletions src/builder/create_scheduled_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,6 @@ impl CreateScheduledEvent {
Ok(self)
}

/// Creates a new scheduled event in the guild with the data set, if any.
///
/// **Note**: Requres the [Manage Events] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Events]: Permissions::MANAGE_EVENTS
pub async fn execute(self, http: impl AsRef<Http>) -> Result<ScheduledEvent> {
http.as_ref().create_scheduled_event(self.id.into(), &self.fields, None).await
}

/// Creates a new scheduled event in the guild with the data set, if any.
///
/// **Note**: Requres the [Manage Events] permission.
Expand All @@ -152,7 +139,7 @@ impl CreateScheduledEvent {
/// Otherwise will return [`Error::Http`] if the current user does not have permission.
///
/// [Manage Events]: Permissions::MANAGE_EVENTS
pub async fn execute_with_cache(self, cache_http: impl CacheHttp) -> Result<ScheduledEvent> {
pub async fn execute(self, cache_http: impl CacheHttp) -> Result<ScheduledEvent> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
Expand All @@ -166,7 +153,11 @@ impl CreateScheduledEvent {
}
}

self.execute(cache_http.http()).await
self._execute(cache_http.http()).await
}

async fn _execute(self, http: impl AsRef<Http>) -> Result<ScheduledEvent> {
http.as_ref().create_scheduled_event(self.id.into(), &self.fields, None).await
}
}

Expand Down
50 changes: 21 additions & 29 deletions src/builder/create_sticker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,7 @@ impl<'a> CreateSticker<'a> {
self
}

/// Executes the request to create a new sticker in the guild with the data set, if any.
///
/// **Note**: Requires the [Manage Emojis and Stickers] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
///
/// [Manage Emojis and Stickers]: crate::model::permissions::Permissions::MANAGE_EMOJIS_AND_STICKERS
pub async fn execute(self, http: impl AsRef<Http>) -> Result<Sticker> {
let file = self.file.ok_or(Error::Model(ModelError::NoStickerFileSet))?;

let mut map = Vec::with_capacity(3);
if let Some(name) = self.fields.name {
map.push(("name", name));
}
if let Some(tags) = self.fields.tags {
map.push(("tags", tags));
}
if let Some(description) = self.fields.description {
map.push(("description", description));
}

http.as_ref().create_sticker(self.id.into(), map, file, None).await
}

/// Executes the request to create a new sticker in the guild with the data set, if any.
/// Creates a new sticker in the guild with the data set, if any.
///
/// **Note**: Requires the [Manage Emojis and Stickers] permission.
///
Expand All @@ -115,7 +89,8 @@ impl<'a> CreateSticker<'a> {
/// lacks permission. Otherwise, returns [`Error::Http`] - see [`Self::execute`].
///
/// [Manage Emojis and Stickers]: crate::model::permissions::Permissions::MANAGE_EMOJIS_AND_STICKERS
pub async fn execute_with_cache(self, cache_http: impl CacheHttp) -> Result<Sticker> {
#[inline]
pub async fn execute(self, cache_http: impl CacheHttp) -> Result<Sticker> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
Expand All @@ -129,6 +104,23 @@ impl<'a> CreateSticker<'a> {
}
}

self.execute(cache_http.http()).await
self._execute(cache_http.http()).await
}

async fn _execute(self, http: impl AsRef<Http>) -> Result<Sticker> {
let file = self.file.ok_or(Error::Model(ModelError::NoStickerFileSet))?;

let mut map = Vec::with_capacity(3);
if let Some(name) = self.fields.name {
map.push(("name", name));
}
if let Some(tags) = self.fields.tags {
map.push(("tags", tags));
}
if let Some(description) = self.fields.description {
map.push(("description", description));
}

http.as_ref().create_sticker(self.id.into(), map, file, None).await
}
}

0 comments on commit f81e5e9

Please sign in to comment.