From af2e19026dccb291187a31566a99521cb0d0b6d0 Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Mon, 21 Oct 2024 06:41:55 -0400 Subject: [PATCH] Simplify ArgumentFormat serialized form (#115) --- src/properties/text_property.rs | 49 +++++------------------- tests/serde_tests/serde_json_template.rs | 23 +++++------ 2 files changed, 19 insertions(+), 53 deletions(-) diff --git a/src/properties/text_property.rs b/src/properties/text_property.rs index 9df498a..dd0b4c8 100644 --- a/src/properties/text_property.rs +++ b/src/properties/text_property.rs @@ -211,7 +211,7 @@ pub enum FTextHistory { /// Source format source_format: Box, /// Arguments - arguments: Vec, + arguments: HashableIndexMap, }, /// Convert to number AsNumber { @@ -363,10 +363,12 @@ impl FTextHistory { TextHistoryType::ArgumentFormat => { let source_format = Box::new(FText::read(cursor, options)?); let count = cursor.read_i32::()?; - let mut arguments = Vec::with_capacity(count as usize); + let mut arguments = HashableIndexMap::with_capacity(count as usize); for _ in 0..count { - arguments.push(FormatArgumentData::read(cursor, options)?); + let key = cursor.read_string()?; + let value = FormatArgumentValue::read(cursor, options)?; + arguments.insert(key, value); } FTextHistory::ArgumentFormat { @@ -576,15 +578,16 @@ impl FTextHistory { FTextHistory::ArgumentFormat { source_format, - arguments, + arguments: HashableIndexMap(arguments), } => { let mut len = 1; cursor.write_enum(TextHistoryType::ArgumentFormat)?; len += source_format.write(cursor, options)?; len += 4; cursor.write_i32::(arguments.len() as i32)?; - for argument in arguments { - len += argument.write(cursor, options)?; + for (key, value) in arguments { + len += cursor.write_string(key)?; + len += value.write(cursor, options)?; } Ok(len) } @@ -725,40 +728,6 @@ pub enum FormatArgumentType { Gender, } -/// Format argument data -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct FormatArgumentData { - /// Argument name - pub name: String, - /// Argument value - pub value: FormatArgumentValue, -} - -impl FormatArgumentData { - /// Read [`FormatArgumentData`] from a cursor - #[inline] - pub fn read(cursor: &mut R, options: &PropertyOptions) -> Result { - let name = cursor.read_string()?; - let value = FormatArgumentValue::read(cursor, options)?; - - Ok(FormatArgumentData { name, value }) - } - - /// Write [`FormatArgumentData`] to a cursor - #[inline] - pub fn write( - &self, - cursor: &mut W, - options: &PropertyOptions, - ) -> Result { - let mut len = 0; - len += cursor.write_string(&self.name)?; - len += self.value.write(cursor, options)?; - Ok(len) - } -} - /// Format argument value #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] diff --git a/tests/serde_tests/serde_json_template.rs b/tests/serde_tests/serde_json_template.rs index 318c06f..083bd1e 100644 --- a/tests/serde_tests/serde_json_template.rs +++ b/tests/serde_tests/serde_json_template.rs @@ -21,8 +21,8 @@ use gvas::{ DateTime, IntPoint, LinearColor, QuatD, QuatF, RotatorD, RotatorF, VectorD, VectorF, }, text_property::{ - DateTimeStyle, FText, FTextHistory, FormatArgumentData, FormatArgumentValue, - NumberFormattingOptions, RoundingMode, TextProperty, TransformType, + DateTimeStyle, FText, FTextHistory, FormatArgumentValue, NumberFormattingOptions, + RoundingMode, TextProperty, TransformType, }, unknown_property::UnknownProperty, Property, @@ -1771,10 +1771,10 @@ fn text_argumentformat() { culture_invariant_string: None, }, }), - arguments: vec![FormatArgumentData { - name: String::from("key"), - value: FormatArgumentValue::UInt(2), - }], + arguments: HashableIndexMap::from([( + String::from("key"), + FormatArgumentValue::UInt(2), + )]), }, })), r#"{ @@ -1784,14 +1784,11 @@ fn text_argumentformat() { "flags": 1, "history": "None" }, - "arguments": [ - { - "name": "key", - "value": { - "UInt": 2 - } + "arguments": { + "key": { + "UInt": 2 } - ] + } }"#, ); }