From 65cac04e582958747ccb6a2f6be57bee44eccb7a Mon Sep 17 00:00:00 2001 From: Dervex <78505208+DervexDev@users.noreply.github.com> Date: Tue, 7 May 2024 00:39:46 +0200 Subject: [PATCH] Add option to serialize human-readable MessagePack in `rbx_reflector` (#410) --- rbx_reflector/src/cli/generate.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rbx_reflector/src/cli/generate.rs b/rbx_reflector/src/cli/generate.rs index 8264936a..20e9af7f 100644 --- a/rbx_reflector/src/cli/generate.rs +++ b/rbx_reflector/src/cli/generate.rs @@ -13,6 +13,8 @@ use rbx_reflection::{ PropertySerialization, PropertyTag, ReflectionDatabase, Scriptability, }; use rbx_types::VariantType; +use rmp_serde::Serializer; +use serde::Serialize; use tempfile::tempdir; use crate::{ @@ -35,6 +37,9 @@ pub struct GenerateSubcommand { /// Whether to pretty-print the JSON output. This has no effect on MessagePack. #[clap(long)] pub no_pretty: bool, + /// Whether to serialize MessagePack in a human readable format. This has no effect on JSON. + #[clap(long)] + pub human_readable: bool, } impl GenerateSubcommand { @@ -86,8 +91,21 @@ impl GenerateSubcommand { result.context("Could not serialize reflection database as JSON")?; } Some("msgpack") => { - let buf = rmp_serde::to_vec(&database) - .context("Could not serialize reflection database as MessagePack")?; + let buf = if self.human_readable { + let mut slice = Vec::with_capacity(128); + let mut serializer = Serializer::new(&mut slice) + .with_human_readable() + .with_struct_map(); + + database.serialize(&mut serializer).context( + "Could not serialize reflection database as human readable MessagePack", + )?; + + slice + } else { + rmp_serde::to_vec(&database) + .context("Could not serialize reflection database as MessagePack")? + }; file.write_all(&buf)?; }