From 28a187ce52c1439fdaf89bad2c8dfe2ab70516dd Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Fri, 1 Nov 2024 18:27:06 +0000 Subject: [PATCH] Add conversion to Enum from EnumItem for rbx_xml serializer --- rbx_xml/src/conversion.rs | 5 ++++- rbx_xml/src/tests/basic.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/rbx_xml/src/conversion.rs b/rbx_xml/src/conversion.rs index a878d578a..56fb9c768 100644 --- a/rbx_xml/src/conversion.rs +++ b/rbx_xml/src/conversion.rs @@ -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 { @@ -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), } } diff --git a/rbx_xml/src/tests/basic.rs b/rbx_xml/src/tests/basic.rs index e4540eb38..7169844f4 100644 --- a/rbx_xml/src/tests/basic.rs +++ b/rbx_xml/src/tests/basic.rs @@ -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() { @@ -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); +}