|
| 1 | +/// Returns `true` if `s` looks like a JSON object or array. |
| 2 | +/// Uses a fast prefix check before attempting a full parse. |
| 3 | +pub fn is_json(s: &str) -> bool { |
| 4 | + let s = s.trim(); |
| 5 | + (s.starts_with('{') || s.starts_with('[')) |
| 6 | + && serde_json::from_str::<serde_json::Value>(s).is_ok() |
| 7 | +} |
| 8 | + |
| 9 | +/// Parse `s` as a JSON object and return a compact `key=value` string |
| 10 | +/// suitable for single-line TUI display. |
| 11 | +/// |
| 12 | +/// - String values are shown unquoted: `msg=request` |
| 13 | +/// - Numbers/bools are shown as-is: `status=200 ok=true` |
| 14 | +/// - Nested objects/arrays are inlined as compact JSON: `meta={"host":"x"}` |
| 15 | +/// - Returns `None` if `s` is not a valid JSON object (arrays included as |
| 16 | +/// pretty JSON). |
| 17 | +pub fn flatten_json(s: &str) -> Option<String> { |
| 18 | + let s = s.trim(); |
| 19 | + if !s.starts_with('{') && !s.starts_with('[') { |
| 20 | + return None; |
| 21 | + } |
| 22 | + let v: serde_json::Value = serde_json::from_str(s).ok()?; |
| 23 | + match v { |
| 24 | + serde_json::Value::Object(map) => { |
| 25 | + let parts: Vec<String> = map |
| 26 | + .iter() |
| 27 | + .map(|(k, val)| { |
| 28 | + let formatted = match val { |
| 29 | + serde_json::Value::String(s) => s.clone(), |
| 30 | + serde_json::Value::Null => "null".to_string(), |
| 31 | + other => other.to_string(), |
| 32 | + }; |
| 33 | + format!("{k}={formatted}") |
| 34 | + }) |
| 35 | + .collect(); |
| 36 | + Some(parts.join(" ")) |
| 37 | + } |
| 38 | + // For arrays, fall back to compact single-line JSON |
| 39 | + other => serde_json::to_string(&other).ok(), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod tests { |
| 45 | + use super::*; |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn is_json_detects_object() { |
| 49 | + assert!(is_json(r#"{"level":"info","msg":"ok"}"#)); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn is_json_detects_array() { |
| 54 | + assert!(is_json(r#"[1,2,3]"#)); |
| 55 | + } |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn is_json_rejects_plain_text() { |
| 59 | + assert!(!is_json("server started on port 3000")); |
| 60 | + assert!(!is_json("ERROR: connection refused")); |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn is_json_rejects_invalid_json() { |
| 65 | + assert!(!is_json("{not valid}")); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn flatten_json_produces_key_value_pairs() { |
| 70 | + let s = r#"{"level":"info","status":200,"ok":true}"#; |
| 71 | + let out = flatten_json(s).unwrap(); |
| 72 | + assert!(out.contains("level=info")); |
| 73 | + assert!(out.contains("status=200")); |
| 74 | + assert!(out.contains("ok=true")); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn flatten_json_unquotes_string_values() { |
| 79 | + let out = flatten_json(r#"{"msg":"hello world"}"#).unwrap(); |
| 80 | + assert_eq!(out, "msg=hello world"); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn flatten_json_inlines_nested_objects() { |
| 85 | + let out = flatten_json(r#"{"meta":{"host":"x"}}"#).unwrap(); |
| 86 | + assert!(out.starts_with("meta=")); |
| 87 | + assert!(out.contains("host")); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn flatten_json_returns_none_for_plain_text() { |
| 92 | + assert!(flatten_json("not json").is_none()); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn flatten_json_handles_array() { |
| 97 | + let out = flatten_json(r#"[1,2,3]"#).unwrap(); |
| 98 | + assert_eq!(out, "[1,2,3]"); |
| 99 | + } |
| 100 | +} |
0 commit comments