Skip to content

Commit

Permalink
Apply Strum derives for FullEvent and Event (serenity-rs#2788)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheesycod authored and GnomedDev committed Oct 20, 2024
1 parent 24836bf commit e65a416
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ serde_cow = { version = "0.1.0" }
small-fixed-array = { version = "0.4", features = ["serde"] }
bool_to_bitflags = { version = "0.1.0" }
nonmax = { version = "0.5.5", features = ["serde"] }
strum = { version = "0.26", features = ["derive"] }
# Optional dependencies
fxhash = { version = "0.2.1", optional = true }
chrono = { version = "0.4.31", default-features = false, features = ["clock", "serde"], optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::VecDeque;
use std::num::NonZeroU16;

use async_trait::async_trait;
use strum::{EnumCount, IntoStaticStr, VariantNames};

use super::context::Context;
use crate::gateway::ShardStageUpdateEvent;
Expand Down Expand Up @@ -33,7 +34,7 @@ macro_rules! event_handler {

/// This enum stores every possible event that an [`EventHandler`] can receive.
#[non_exhaustive]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, VariantNames, IntoStaticStr, EnumCount)]
pub enum FullEvent {
$(
$( #[doc = $doc] )*
Expand Down
9 changes: 5 additions & 4 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use nonmax::NonMaxU64;
use serde::de::Error as DeError;
use serde::Serialize;
use strum::{EnumCount, IntoStaticStr, VariantNames};
use tracing::{debug, warn};

use crate::constants::Opcode;
Expand Down Expand Up @@ -1147,7 +1148,8 @@ impl<'de> Deserialize<'de> for GatewayEvent {
///
/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#receive-events).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, EnumCount, VariantNames, IntoStaticStr)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[serde(tag = "t", content = "d")]
#[non_exhaustive]
Expand Down Expand Up @@ -1355,9 +1357,8 @@ pub enum Event {
impl Event {
/// Returns the event name of this event.
#[must_use]
pub fn name(&self) -> Option<String> {
let map = serde_json::to_value(self).ok()?;
Some(map.get("t")?.as_str()?.to_string())
pub fn name(&self) -> &'static str {
self.into()
}

pub(crate) fn deserialize_and_log(map: JsonMap, original_str: &str) -> Result<Self> {
Expand Down

0 comments on commit e65a416

Please sign in to comment.