From 2b716351c12dd5c5fd878e92535d765da2a2a7bd Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 22 Jan 2026 22:22:03 -0600 Subject: [PATCH] Refactor JSON handling and improve code clarity - Updated error messages in Http.jsonDecode for better context. - Enhanced serialization error handling in CompactJson5Serializer. - Reformatted struct definitions in tests for improved readability. - Adjusted assertions in tests for better clarity and consistency. - Refactored project file name checks for improved readability. --- plugin/http/init.lua | 2 +- src/json.rs | 47 +++++++++++++++++++++++++++++++++----------- src/project.rs | 12 +++++++++-- src/web/api.rs | 2 +- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/plugin/http/init.lua b/plugin/http/init.lua index 8c6b215f..04287c7c 100644 --- a/plugin/http/init.lua +++ b/plugin/http/init.lua @@ -72,7 +72,7 @@ function Http.jsonDecode(source) if success then return result end - print("[HTTP Response:json()] Native decode failed, using JSON5 for size:", #self.body) + print("[Http.jsonDecode] Native decode failed, using JSON5 for size:", #source) return JSON5Decoder.Decode(source) end diff --git a/src/json.rs b/src/json.rs index 4891c7ba..ce3d2aae 100644 --- a/src/json.rs +++ b/src/json.rs @@ -165,8 +165,12 @@ impl serde::ser::Error for CompactJson5Error { /// Returns an error if the value cannot be serialized to JSON5. pub fn to_string_compact(value: &T) -> anyhow::Result { let mut output = String::new(); - let mut serializer = CompactJson5Serializer { output: &mut output }; - value.serialize(&mut serializer).map_err(|e| anyhow::anyhow!("{}", e))?; + let mut serializer = CompactJson5Serializer { + output: &mut output, + }; + value + .serialize(&mut serializer) + .map_err(|e| anyhow::anyhow!("{}", e))?; Ok(output) } @@ -764,7 +768,9 @@ mod tests { #[test] fn test_deserialize_infinity() { #[derive(Debug, Deserialize)] - struct TestFloat { x: f64 } + struct TestFloat { + x: f64, + } let result: TestFloat = from_str(r#"{ "x": Infinity }"#).unwrap(); assert!(result.x.is_infinite() && result.x.is_sign_positive()); } @@ -772,7 +778,9 @@ mod tests { #[test] fn test_deserialize_negative_infinity() { #[derive(Debug, Deserialize)] - struct TestFloat { x: f64 } + struct TestFloat { + x: f64, + } let result: TestFloat = from_str(r#"{ "x": -Infinity }"#).unwrap(); assert!(result.x.is_infinite() && result.x.is_sign_negative()); } @@ -780,7 +788,9 @@ mod tests { #[test] fn test_deserialize_nan() { #[derive(Debug, Deserialize)] - struct TestFloat { x: f64 } + struct TestFloat { + x: f64, + } let result: TestFloat = from_str(r#"{ "x": NaN }"#).unwrap(); assert!(result.x.is_nan()); } @@ -956,7 +966,9 @@ mod tests { assert_eq!(result.normal, 42.5); assert!(result.infinity.is_infinite() && result.infinity.is_sign_positive()); - assert!(result.negative_infinity.is_infinite() && result.negative_infinity.is_sign_negative()); + assert!( + result.negative_infinity.is_infinite() && result.negative_infinity.is_sign_negative() + ); assert!(result.nan_value.is_nan()); } @@ -994,7 +1006,9 @@ mod tests { assert_eq!(deserialized.normal, original.normal); assert!(deserialized.infinity.is_infinite() && deserialized.infinity.is_sign_positive()); - assert!(deserialized.neg_infinity.is_infinite() && deserialized.neg_infinity.is_sign_negative()); + assert!( + deserialized.neg_infinity.is_infinite() && deserialized.neg_infinity.is_sign_negative() + ); } #[test] @@ -1047,15 +1061,22 @@ mod tests { let deserialized: CFrameData = from_str(&json_str).unwrap(); // Verify position - assert!(deserialized.position[0].is_infinite() && deserialized.position[0].is_sign_positive()); + assert!( + deserialized.position[0].is_infinite() && deserialized.position[0].is_sign_positive() + ); assert!(deserialized.position[1].is_nan()); - assert!(deserialized.position[2].is_infinite() && deserialized.position[2].is_sign_negative()); + assert!( + deserialized.position[2].is_infinite() && deserialized.position[2].is_sign_negative() + ); // Verify orientation assert_eq!(deserialized.orientation[0][0], 1.0); assert!(deserialized.orientation[0][2].is_nan()); assert!(deserialized.orientation[1][1].is_infinite()); - assert!(deserialized.orientation[2][0].is_infinite() && deserialized.orientation[2][0].is_sign_negative()); + assert!( + deserialized.orientation[2][0].is_infinite() + && deserialized.orientation[2][0].is_sign_negative() + ); } #[test] @@ -1093,7 +1114,11 @@ mod tests { let deserialized: MetaFile = from_str(&json_str).unwrap(); assert_eq!(deserialized.class_name, "Humanoid"); - assert!(deserialized.properties.get("Transparency").unwrap().is_nan()); + assert!(deserialized + .properties + .get("Transparency") + .unwrap() + .is_nan()); assert!(deserialized.properties.get("Health").unwrap().is_infinite()); assert_eq!(*deserialized.properties.get("WalkSpeed").unwrap(), 16.0); } diff --git a/src/project.rs b/src/project.rs index ba7538c7..b968e47c 100644 --- a/src/project.rs +++ b/src/project.rs @@ -16,7 +16,11 @@ use crate::{ }; /// Represents 'default' project names that act as `init` files -pub static DEFAULT_PROJECT_NAMES: [&str; 3] = ["default.project.json", "default.project.jsonc", "default.project.json5"]; +pub static DEFAULT_PROJECT_NAMES: [&str; 3] = [ + "default.project.json", + "default.project.jsonc", + "default.project.json5", +]; /// Error type returned by any function that handles projects. #[derive(Debug, Error)] @@ -146,7 +150,11 @@ impl Project { pub fn is_project_file(path: &Path) -> bool { path.file_name() .and_then(|name| name.to_str()) - .map(|name| name.ends_with(".project.json") || name.ends_with(".project.jsonc") || name.ends_with(".project.json5")) + .map(|name| { + name.ends_with(".project.json") + || name.ends_with(".project.jsonc") + || name.ends_with(".project.json5") + }) .unwrap_or(false) } diff --git a/src/web/api.rs b/src/web/api.rs index d3bf2ddd..09bc11db 100644 --- a/src/web/api.rs +++ b/src/web/api.rs @@ -28,7 +28,7 @@ use crate::{ ServerInfoResponse, SocketPacket, SocketPacketBody, SocketPacketType, SubscribeMessage, WriteRequest, WriteResponse, PROTOCOL_VERSION, SERVER_VERSION, }, - util::{json_response, json_ok}, + util::{json_ok, json_response}, }, web_api::{ BufferEncode, InstanceUpdate, RefPatchRequest, RefPatchResponse, SerializeRequest,