Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert enum number into wrapper struct to save type sizes #2746

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum Opcode {
/// Dispatches an event.
Expand Down
41 changes: 18 additions & 23 deletions src/internal/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,39 @@ macro_rules! status {
macro_rules! enum_number {
(
$(#[$outer:meta])*
$(#[<default> = $default:literal])?
$vis:vis enum $Enum:ident {
$(
$(#[doc = $doc:literal])*
$(#[cfg $($cfg:tt)*])?
$(#[default $($dummy:tt)?])?
$Variant:ident = $value:literal,
)*
_ => Unknown($T:ty),
}
) => {
$(#[$outer])*
$vis enum $Enum {
$vis struct $Enum (pub $T);

$(
impl Default for $Enum {
fn default() -> Self {
Self($default)
}
}
)?

#[allow(non_snake_case, non_upper_case_globals)]
impl $Enum {
$(
$(#[doc = $doc])*
$(#[cfg $($cfg)*])?
$(#[default $($dummy:tt)?])?
$Variant,
$vis const $Variant: Self = Self($value);
)*
/// Variant value is unknown.
Unknown($T),
}

impl From<$T> for $Enum {
fn from(value: $T) -> Self {
match value {
$($(#[cfg $($cfg)*])? $value => Self::$Variant,)*
unknown => Self::Unknown(unknown),
}
}
}

impl From<$Enum> for $T {
fn from(value: $Enum) -> Self {
match value {
$($(#[cfg $($cfg)*])? $Enum::$Variant => $value,)*
$Enum::Unknown(unknown) => unknown,
}
/// Variant value is unknown.
#[must_use]
$vis const fn Unknown(val: $T) -> Self {
Self(val)
}
}
};
Expand Down Expand Up @@ -184,7 +180,6 @@ mod tests {
fn enum_number() {
enum_number! {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
pub enum T {
/// AAA
A = 1,
Expand Down
3 changes: 0 additions & 3 deletions src/model/application/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum CommandType {
ChatInput = 1,
Expand Down Expand Up @@ -288,7 +287,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-type).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum CommandOptionType {
SubCommand = 1,
Expand Down Expand Up @@ -361,7 +359,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-application-command-permission-type).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum CommandPermissionType {
Role = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/model/application/command_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ fn option_from_raw(raw: RawCommandDataOption) -> Result<CommandDataOption> {
CommandOptionType::Mentionable => CommandDataOptionValue::Mentionable(value!()),
CommandOptionType::Role => CommandDataOptionValue::Role(value!()),
CommandOptionType::User => CommandDataOptionValue::User(value!()),
CommandOptionType::Unknown(unknown) => CommandDataOptionValue::Unknown(unknown),
CommandOptionType(unknown) => CommandDataOptionValue::Unknown(unknown),
};

Ok(CommandDataOption {
Expand Down
7 changes: 2 additions & 5 deletions src/model/application/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ enum_number! {
/// The type of a component
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ComponentType {
ActionRow = 1,
Expand Down Expand Up @@ -70,7 +69,7 @@ impl<'de> Deserialize<'de> for ActionRowComponent {
ComponentType::ActionRow => {
return Err(DeError::custom("Invalid component type ActionRow"))
},
ComponentType::Unknown(i) => {
ComponentType(i) => {
return Err(DeError::custom(format_args!("Unknown component type {i}")))
},
}
Expand Down Expand Up @@ -134,7 +133,7 @@ impl Serialize for ButtonKind {
custom_id,
style,
} => Helper {
style: (*style).into(),
style: style.0,
url: None,
custom_id: Some(custom_id),
},
Expand Down Expand Up @@ -171,7 +170,6 @@ enum_number! {
/// The style of a button.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ButtonStyle {
Primary = 1,
Expand Down Expand Up @@ -285,7 +283,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-styles).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum InputTextStyle {
Short = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/model/application/component_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ impl<'de> Deserialize<'de> for ComponentInteractionDataKind {
ComponentType::ChannelSelect => Self::ChannelSelect {
values: parse_values!(),
},
ComponentType::Unknown(x) => Self::Unknown(x),
x @ (ComponentType::ActionRow | ComponentType::InputText) => {
return Err(D::Error::custom(format_args!(
"invalid message component type in this context: {x:?}",
)));
},
ComponentType(x) => Self::Unknown(x),
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/model/application/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<'de> Deserialize<'de> for Interaction {
InteractionType::Autocomplete => from_value(value).map(Interaction::Autocomplete),
InteractionType::Modal => from_value(value).map(Interaction::Modal),
InteractionType::Ping => from_value(value).map(Interaction::Ping),
InteractionType::Unknown(_) => return Err(DeError::custom("Unknown interaction type")),
InteractionType(_) => return Err(DeError::custom("Unknown interaction type")),
}
.map_err(DeError::custom)
}
Expand All @@ -260,7 +260,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-type).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum InteractionType {
Ping = 1,
Expand Down
1 change: 0 additions & 1 deletion src/model/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ pub struct TeamMember {
enum_number! {
/// [Discord docs](https://discord.com/developers/docs/topics/teams#data-models-membership-state-enum).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum MembershipState {
Invited = 1,
Expand Down
2 changes: 0 additions & 2 deletions src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,9 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#channel-object-forum-layout-types).
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ForumLayoutType {
/// No default has been set for forum channel.
#[default]
NotSet = 0,
/// Display posts as a list.
ListView = 1,
Expand Down
3 changes: 0 additions & 3 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,9 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-object-message-types).
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum MessageType {
/// A regular message.
#[default]
Regular = 0,
/// An indicator that a recipient was added by the author.
GroupRecipientAddition = 1,
Expand Down Expand Up @@ -922,7 +920,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#message-object-message-activity-types).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum MessageActivityKind {
Join = 1,
Expand Down
16 changes: 5 additions & 11 deletions src/model/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,9 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#channel-object-channel-types).
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ChannelType {
/// An indicator that the channel is a text [`GuildChannel`].
#[default]
Text = 0,
/// An indicator that the channel is a [`PrivateChannel`].
Private = 1,
Expand Down Expand Up @@ -250,8 +248,8 @@ enum_number! {

impl ChannelType {
#[must_use]
pub const fn name(&self) -> &str {
match *self {
pub const fn name(self) -> &'static str {
match self {
Self::Private => "private",
Self::Text => "text",
Self::Voice => "voice",
Expand All @@ -264,7 +262,7 @@ impl ChannelType {
Self::Stage => "stage",
Self::Directory => "directory",
Self::Forum => "forum",
Self::Unknown(_) => "unknown",
Self(_) => "unknown",
}
}
}
Expand Down Expand Up @@ -357,7 +355,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#channel-object-video-quality-modes).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum VideoQualityMode {
/// An indicator that the video quality is chosen by Discord for optimal
Expand All @@ -373,15 +370,14 @@ enum_number! {
/// See [`StageInstance::privacy_level`].
///
/// [Discord docs](https://discord.com/developers/docs/resources/stage-instance#stage-instance-object-privacy-level).
#[derive(Clone, Copy, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize, Serialize)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
#[<default> = 2]
pub enum StageInstancePrivacyLevel {
/// The Stage instance is visible publicly. (deprecated)
Public = 1,
/// The Stage instance is visible to only guild members.
#[default]
GuildOnly = 2,
_ => Unknown(u8),
}
Expand All @@ -393,7 +389,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#thread-metadata-object)
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u16", into = "u16")]
#[non_exhaustive]
pub enum AutoArchiveDuration {
None = 0,
Expand Down Expand Up @@ -590,7 +585,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/channel#channel-object-sort-order-types).
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum SortOrder {
/// Sort forum posts by activity.
Expand Down
1 change: 0 additions & 1 deletion src/model/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/resources/user#connection-object-visibility-types).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ConnectionVisibility {
/// Invisible to everyone except the user themselves
Expand Down
2 changes: 0 additions & 2 deletions src/model/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,9 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-types).
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ActivityType {
/// An indicator that the user is playing a game.
#[default]
Playing = 0,
/// An indicator that the user is streaming to a service.
Streaming = 1,
Expand Down
3 changes: 1 addition & 2 deletions src/model/guild/automod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl<'de> Deserialize<'de> for Action {
.duration_seconds
.ok_or_else(|| Error::missing_field("duration_seconds"))?,
)),
ActionType::Unknown(unknown) => Action::Unknown(unknown),
ActionType(unknown) => Action::Unknown(unknown),
})
}
}
Expand Down Expand Up @@ -497,7 +497,6 @@ enum_number! {
///
/// [Discord docs](https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-action-object-action-types).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum ActionType {
BlockMessage = 1,
Expand Down
1 change: 0 additions & 1 deletion src/model/guild/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ enum_number! {
/// [Discord docs](https://discord.com/developers/docs/resources/guild#integration-object-integration-expire-behaviors).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum IntegrationExpireBehaviour {
RemoveRole = 0,
Expand Down
Loading
Loading