Skip to content

Commit

Permalink
explicit enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
krakow10 committed Feb 23, 2024
1 parent 1d4f34c commit 7b9cf98
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
6 changes: 2 additions & 4 deletions rbx_types/src/attributes/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use crate::{
BinaryString, BrickColor, CFrame, Color3, ColorSequence, ColorSequenceKeypoint, Enum, Font,
BinaryString, BrickColor, CFrame, Color3, ColorSequence, ColorSequenceKeypoint, ExplicitEnum, Font,
FontStyle, FontWeight, Matrix3, NumberRange, NumberSequence, NumberSequenceKeypoint, Rect,
UDim, UDim2, Variant, VariantType, Vector2, Vector3,
};
Expand Down Expand Up @@ -172,9 +172,7 @@ pub(crate) fn read_attributes<R: Read>(
};
let enum_value = read_u32(&mut value)?;

// enum_name is unused

Enum::from_u32(enum_value)
ExplicitEnum::new(enum_name, enum_value)
}
.into(),

Expand Down
27 changes: 27 additions & 0 deletions rbx_types/src/basic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ impl Enum {
}
}

/// Represents any Roblox enum value with an explicitly known name.
///
/// This enums is strongly typed, meaning it can only be assigned to fields with the same enum name.
///
/// A list of all enums and their values are available [on the Roblox Developer
/// Hub](https://developer.roblox.com/en-us/api-reference/enum).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ExplicitEnum {
name: String,
value: u32,
}

impl ExplicitEnum {
pub fn new(name: String, value: u32) -> Self {
Self { name, value }
}

pub fn to_u32(&self) -> u32 {
self.value
}
}

/// The standard 2D vector type used in Roblox.
///
/// ## See Also
Expand Down
7 changes: 4 additions & 3 deletions rbx_types/src/variant.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
Attributes, Axes, BinaryString, BrickColor, CFrame, Color3, Color3uint8, ColorSequence,
Content, Enum, Faces, Font, MaterialColors, NumberRange, NumberSequence, PhysicalProperties,
Ray, Rect, Ref, Region3, Region3int16, SecurityCapabilities, SharedString, Tags, UDim, UDim2,
UniqueId, Vector2, Vector2int16, Vector3, Vector3int16,
Content, Enum, ExplicitEnum, Faces, Font, MaterialColors, NumberRange, NumberSequence,
PhysicalProperties, Ray, Rect, Ref, Region3, Region3int16, SecurityCapabilities, SharedString,
Tags, UDim, UDim2, UniqueId, Vector2, Vector2int16, Vector3, Vector3int16,
};

/// Reduces boilerplate from listing different values of Variant by wrapping
Expand Down Expand Up @@ -101,6 +101,7 @@ make_variant! {
ColorSequence(ColorSequence),
Content(Content),
Enum(Enum),
ExplicitEnum(ExplicitEnum),
Faces(Faces),
Float32(f32),
Float64(f64),
Expand Down

0 comments on commit 7b9cf98

Please sign in to comment.