Skip to content

Commit

Permalink
Add conversion to Enum from EnumItem for rbx_xml serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethloeffler committed Nov 1, 2024
1 parent 4147ab9 commit 28a187c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion rbx_xml/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::borrow::{Borrow, Cow};
use std::convert::TryInto;

use rbx_dom_weak::types::{
Attributes, BrickColor, Color3uint8, MaterialColors, Tags, Variant, VariantType,
Attributes, BrickColor, Color3uint8, Enum, MaterialColors, Tags, Variant, VariantType,
};

pub trait ConvertVariant: Clone + Sized {
Expand Down Expand Up @@ -69,6 +69,9 @@ impl ConvertVariant for Variant {
.map_err(|_| "invalid MaterialColors value")?
.into(),
)),
(Variant::EnumItem(enum_item), VariantType::Enum) => {
Ok(Cow::Owned(Enum::from_u32(enum_item.value).into()))
}
(_, _) => Ok(value),
}
}
Expand Down
33 changes: 29 additions & 4 deletions rbx_xml/src/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
use rbx_dom_weak::types::{
Attributes, BinaryString, BrickColor, Color3, Color3uint8, ColorSequence,
ColorSequenceKeypoint, Enum, Font, MaterialColors, NumberRange, NumberSequence,
NumberSequenceKeypoint, Rect, Tags, TerrainMaterials, UDim, UDim2, UniqueId, Variant, Vector2,
Vector3,
ColorSequenceKeypoint, Enum, EnumItem, Font, MaterialColors, NumberRange, NumberSequence,
NumberSequenceKeypoint, Rect, Tags, TerrainMaterials, UDim, UDim2, UniqueId, Variant,
VariantType, Vector2, Vector3,
};
use rbx_dom_weak::{InstanceBuilder, WeakDom};
use rbx_dom_weak::{ustr, InstanceBuilder, WeakDom};

#[test]
fn with_bool() {
Expand Down Expand Up @@ -352,3 +352,28 @@ fn bad_migrated_property() {
crate::to_writer_default(&mut encoded, &tree, &[tree.root_ref()]).unwrap();
insta::assert_snapshot!(std::str::from_utf8(&encoded).unwrap());
}

#[test]
fn enum_item_to_enum() {
let tree = WeakDom::new(InstanceBuilder::new("Part").with_property(
"Material",
EnumItem {
ty: "Material".into(),
value: 256,
},
));

let mut encoded = Vec::new();
crate::to_writer_default(&mut encoded, &tree, &[tree.root_ref()]).unwrap();

let decoded = crate::from_reader_default(encoded.as_slice()).unwrap();
let prop_type = decoded
.get_by_ref(*decoded.root().children().first().unwrap())
.unwrap()
.properties
.get(&ustr("Material"))
.unwrap()
.ty();

assert_eq!(prop_type, VariantType::Enum);
}

0 comments on commit 28a187c

Please sign in to comment.