Skip to content

Commit

Permalink
Simplify ArgumentFormat serialized form (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottanderson authored Oct 21, 2024
1 parent f8d5d93 commit af2e190
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
49 changes: 9 additions & 40 deletions src/properties/text_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub enum FTextHistory {
/// Source format
source_format: Box<FText>,
/// Arguments
arguments: Vec<FormatArgumentData>,
arguments: HashableIndexMap<String, FormatArgumentValue>,
},
/// Convert to number
AsNumber {
Expand Down Expand Up @@ -363,10 +363,12 @@ impl FTextHistory {
TextHistoryType::ArgumentFormat => {
let source_format = Box::new(FText::read(cursor, options)?);
let count = cursor.read_i32::<LittleEndian>()?;
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 {
Expand Down Expand Up @@ -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::<LittleEndian>(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)
}
Expand Down Expand Up @@ -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<R: Read + Seek>(cursor: &mut R, options: &PropertyOptions) -> Result<Self, Error> {
let name = cursor.read_string()?;
let value = FormatArgumentValue::read(cursor, options)?;

Ok(FormatArgumentData { name, value })
}

/// Write [`FormatArgumentData`] to a cursor
#[inline]
pub fn write<W: Write>(
&self,
cursor: &mut W,
options: &PropertyOptions,
) -> Result<usize, Error> {
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))]
Expand Down
23 changes: 10 additions & 13 deletions tests/serde_tests/serde_json_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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#"{
Expand All @@ -1784,14 +1784,11 @@ fn text_argumentformat() {
"flags": 1,
"history": "None"
},
"arguments": [
{
"name": "key",
"value": {
"UInt": 2
}
"arguments": {
"key": {
"UInt": 2
}
]
}
}"#,
);
}
Expand Down

0 comments on commit af2e190

Please sign in to comment.