Skip to content

Add int/float widening support to rbx_xml #303

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

Merged
merged 3 commits into from
Jul 1, 2023
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
8 changes: 8 additions & 0 deletions rbx_xml/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ impl ConvertVariant for Variant {
target_type: VariantType,
) -> Result<Cow<'_, Self>, String> {
match (value.borrow(), target_type) {
// Older files may not have their number types moved to 64-bit yet,
// which can cause problems. See issue #301.
(Variant::Int32(value), VariantType::Int64) => {
Ok(Cow::Owned((i64::from(*value)).into()))
}
(Variant::Float32(value), VariantType::Float64) => {
Ok(Cow::Owned((f64::from(*value)).into()))
}
(Variant::Int32(value), VariantType::BrickColor) => {
let narrowed: u16 = (*value).try_into().map_err(|_| {
format!("Value {} is not in the range of a valid BrickColor", value)
Expand Down
1 change: 1 addition & 0 deletions rbx_xml/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ fn deserialize_properties<R: Read>(
DataType::Enum(_enum_name) => VariantType::Enum,
_ => unimplemented!(),
};
log::trace!("property's read type: {xml_ty:?}, canonical type: {expected_type:?}");

let value = match value.try_convert(expected_type) {
Ok(value) => value,
Expand Down
33 changes: 33 additions & 0 deletions rbx_xml/src/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,36 @@ fn read_unique_id() {
)))
);
}

#[test]
fn number_widening() {
let _ = env_logger::try_init();
let document = r#"
<roblox version="4">
<Item class="IntValue" referent="Test">
<Properties>
<int name="Value">194</int>
</Properties>
</Item>
<Item class="NumberValue" referent="Test">
<Properties>
<float name="Value">1337</float>
</Properties>
</Item>
</roblox>
"#;
let tree = crate::from_str_default(document).unwrap();

let int_value = tree.get_by_ref(tree.root().children()[0]).unwrap();
assert_eq!(int_value.class, "IntValue");
assert_eq!(
int_value.properties.get("Value"),
Some(&Variant::Int64(194))
);
let float_value = tree.get_by_ref(tree.root().children()[1]).unwrap();
assert_eq!(float_value.class, "NumberValue");
assert_eq!(
float_value.properties.get("Value"),
Some(&Variant::Float64(1337.0))
);
}