Skip to content

Commit 835a7d3

Browse files
authored
Add int/float widening support to rbx_xml (#303)
1 parent 58c0124 commit 835a7d3

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

rbx_xml/src/conversion.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ impl ConvertVariant for Variant {
2727
target_type: VariantType,
2828
) -> Result<Cow<'_, Self>, String> {
2929
match (value.borrow(), target_type) {
30+
// Older files may not have their number types moved to 64-bit yet,
31+
// which can cause problems. See issue #301.
32+
(Variant::Int32(value), VariantType::Int64) => {
33+
Ok(Cow::Owned((i64::from(*value)).into()))
34+
}
35+
(Variant::Float32(value), VariantType::Float64) => {
36+
Ok(Cow::Owned((f64::from(*value)).into()))
37+
}
3038
(Variant::Int32(value), VariantType::BrickColor) => {
3139
let narrowed: u16 = (*value).try_into().map_err(|_| {
3240
format!("Value {} is not in the range of a valid BrickColor", value)

rbx_xml/src/deserializer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ fn deserialize_properties<R: Read>(
588588
DataType::Enum(_enum_name) => VariantType::Enum,
589589
_ => unimplemented!(),
590590
};
591+
log::trace!("property's read type: {xml_ty:?}, canonical type: {expected_type:?}");
591592

592593
let value = match value.try_convert(expected_type) {
593594
Ok(value) => value,

rbx_xml/src/tests/basic.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,36 @@ fn read_unique_id() {
230230
)))
231231
);
232232
}
233+
234+
#[test]
235+
fn number_widening() {
236+
let _ = env_logger::try_init();
237+
let document = r#"
238+
<roblox version="4">
239+
<Item class="IntValue" referent="Test">
240+
<Properties>
241+
<int name="Value">194</int>
242+
</Properties>
243+
</Item>
244+
<Item class="NumberValue" referent="Test">
245+
<Properties>
246+
<float name="Value">1337</float>
247+
</Properties>
248+
</Item>
249+
</roblox>
250+
"#;
251+
let tree = crate::from_str_default(document).unwrap();
252+
253+
let int_value = tree.get_by_ref(tree.root().children()[0]).unwrap();
254+
assert_eq!(int_value.class, "IntValue");
255+
assert_eq!(
256+
int_value.properties.get("Value"),
257+
Some(&Variant::Int64(194))
258+
);
259+
let float_value = tree.get_by_ref(tree.root().children()[1]).unwrap();
260+
assert_eq!(float_value.class, "NumberValue");
261+
assert_eq!(
262+
float_value.properties.get("Value"),
263+
Some(&Variant::Float64(1337.0))
264+
);
265+
}

0 commit comments

Comments
 (0)