From e059e19192254c3679f98e9e542d2bf16090d6f2 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 20 Aug 2026 15:13:31 -0700 Subject: [PATCH 1/2] Make `imdl torrent dump` output valid JSON --- src/subcommand/torrent/dump.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/subcommand/torrent/dump.rs b/src/subcommand/torrent/dump.rs index ddbc1b2..aae413b 100644 --- a/src/subcommand/torrent/dump.rs +++ b/src/subcommand/torrent/dump.rs @@ -35,12 +35,13 @@ struct Fmt<'a>(&'a Value<'a>); fn fmt_string(f: &mut Formatter, string: &[u8]) -> fmt::Result { if let Ok(string) = str::from_utf8(string) { - write!(f, "\"{string}\"")?; + write!(f, "{}", serde_json::to_string(string).unwrap())?; } else { - write!(f, "0x")?; + write!(f, "\"0x")?; for byte in string { write!(f, "{byte:02x}")?; } + write!(f, "\"")?; } Ok(()) @@ -138,7 +139,7 @@ mod tests { fn hex_string() { assert_eq!( Fmt(&Value::Bytes(b"\x80\x81".to_vec().into())).to_string(), - "0x8081", + "\"0x8081\"", ); } @@ -155,10 +156,15 @@ mod tests { env.assert_ok(); assert_eq!(env.out(), output); + + serde_json::from_str::(&env.out()).unwrap(); } case("0:", "\"\"\n"); case("1:x", "\"x\"\n"); + case("1:\"", "\"\\\"\"\n"); + case("1:\\", "\"\\\\\"\n"); + case("1:\n", "\"\\n\"\n"); case("i-123e", "-123\n"); case("i-1e", "-1\n"); From 913cae6c4fec2fe0389f8b1d213d1920d3fce693 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 20 Aug 2026 15:34:21 -0700 Subject: [PATCH 2/2] Reform --- src/subcommand/torrent/dump.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/subcommand/torrent/dump.rs b/src/subcommand/torrent/dump.rs index aae413b..92e86fd 100644 --- a/src/subcommand/torrent/dump.rs +++ b/src/subcommand/torrent/dump.rs @@ -163,8 +163,6 @@ mod tests { case("0:", "\"\"\n"); case("1:x", "\"x\"\n"); case("1:\"", "\"\\\"\"\n"); - case("1:\\", "\"\\\\\"\n"); - case("1:\n", "\"\\n\"\n"); case("i-123e", "-123\n"); case("i-1e", "-1\n");