From 0efb09ea6902e5c6cd5259605cd259291ae0c91a Mon Sep 17 00:00:00 2001 From: Dervex Date: Wed, 8 May 2024 23:03:29 +0200 Subject: [PATCH] Add pretty print serialization for floats --- src/resolution.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/resolution.rs b/src/resolution.rs index b649ea7..b7f94ff 100644 --- a/src/resolution.rs +++ b/src/resolution.rs @@ -6,7 +6,7 @@ use rbx_dom_weak::types::{ Vector3, }; use rbx_reflection::{DataType, PropertyDescriptor}; -use serde::{Deserialize, Serialize}; +use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer}; use std::{borrow::Borrow, collections::HashMap, fmt::Write}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -108,11 +108,16 @@ pub enum AmbiguousValue { Bool(bool), String(String), StringArray(Vec), + #[serde(serialize_with = "serialize_number")] Number(f64), Object(HashMap), + #[serde(serialize_with = "serialize_array")] Array2([f64; 2]), + #[serde(serialize_with = "serialize_array")] Array3([f64; 3]), + #[serde(serialize_with = "serialize_array")] Array4([f64; 4]), + #[serde(serialize_with = "serialize_array")] Array12([f64; 12]), Attributes(Attributes), Font(Font), @@ -296,3 +301,35 @@ fn truncate_float(float: f64) -> f64 { float } } + +fn serialize_number(number: &f64, serializer: S) -> Result +where + S: Serializer, +{ + let number = (*number * 1_000_000.0).trunc() / 1_000_000.0; + + if number.fract() == 0.0 { + serializer.serialize_i64(number as i64) + } else { + serializer.serialize_f64(number) + } +} + +fn serialize_array(array: &[f64], serializer: S) -> Result +where + S: Serializer, +{ + let mut seq = serializer.serialize_seq(Some(array.len()))?; + + for number in array { + let number = (*number * 1_000_000.0).trunc() / 1_000_000.0; + + if number.fract() == 0.0 { + seq.serialize_element(&(number as i64))?; + } else { + seq.serialize_element(&number)?; + } + } + + seq.end() +}