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 16, 2022
1 parent 894f8aa commit 71e567e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
23 changes: 7 additions & 16 deletions src/builder/create_scheduled_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,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> {
let map = json::hashmap_to_json_map(self.map);

http.as_ref().create_scheduled_event(self.id.into(), &map, None).await
}

/// Creates a new scheduled event in the guild with the data set, if any.
///
/// **Note**: Requres the [Manage Events] permission.
Expand All @@ -144,7 +129,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 @@ -160,4 +145,10 @@ impl CreateScheduledEvent {

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

pub async fn execute(self, http: impl AsRef<Http>) -> Result<ScheduledEvent> {
let map = json::hashmap_to_json_map(self.map);

http.as_ref().create_scheduled_event(self.id.into(), &map, None).await
}
}
42 changes: 17 additions & 25 deletions src/builder/create_sticker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,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 map = json::hashmap_to_json_map(self.map);

let file = match self.file {
Some(f) => f,
None => return Err(Error::Model(ModelError::NoStickerFileSet)),
};

let sticker = http.as_ref().create_sticker(self.id.into(), map, file, None).await?;

Ok(sticker)
}

/// 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 @@ -105,7 +83,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 @@ -119,6 +98,19 @@ 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 map = json::hashmap_to_json_map(self.map);

let file = match self.file {
Some(f) => f,
None => return Err(Error::Model(ModelError::NoStickerFileSet)),
};

let sticker = http.as_ref().create_sticker(self.id.into(), map, file, None).await?;

Ok(sticker)
}
}

0 comments on commit 71e567e

Please sign in to comment.