Skip to content

Commit 1c699fb

Browse files
committed
fix(hu-meter): escape --json topic output and document --raw in echo usage
1 parent efdd06e commit 1c699fb

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

  • crates/hiroz-union/plugins/hu-meter/src

crates/hiroz-union/plugins/hu-meter/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl HuMeter {
182182
fn cmd_echo(&mut self, args: &[String]) {
183183
let Some(topic) = args.first().cloned() else {
184184
render::println(
185-
"Usage: hu meter echo <topic> [--count <n>] [--field <path>] [--timeout <s>]",
185+
"Usage: hu meter echo <topic> [--count <n>] [--field <path>] [--timeout <s>] [--raw]",
186186
);
187187
render::exit(1);
188188
self.mode = Mode::Done;
@@ -295,8 +295,9 @@ impl HuMeter {
295295
topics
296296
.iter()
297297
.map(|t| format!(
298-
"{{\"name\":\"{}\",\"type\":\"{}\"}}",
299-
t.name, t.type_name
298+
"{{\"name\":{},\"type\":{}}}",
299+
json_str(&t.name),
300+
json_str(&t.type_name)
300301
))
301302
.collect::<Vec<_>>()
302303
.join(",")
@@ -1236,6 +1237,13 @@ fn infer_param_value(s: &str) -> (u8, String) {
12361237
(4, format!(r#""string_value":{quoted}"#))
12371238
}
12381239

1240+
/// Serialize a string as a JSON string literal (with surrounding quotes),
1241+
/// escaping quotes, backslashes, and control characters so the emitted
1242+
/// `--json` output stays valid JSON regardless of topic/type contents.
1243+
fn json_str(s: &str) -> String {
1244+
serde_json::to_string(s).unwrap_or_else(|_| "\"\"".to_string())
1245+
}
1246+
12391247
/// Same as `infer_param_value` but from an already-typed JSON value (as
12401248
/// produced by the host's YAML-file parser for `param load`), so a numeric
12411249
/// YAML scalar like `55` isn't round-tripped through string parsing.

0 commit comments

Comments
 (0)