agentmux_common/
settings_jsonc.rs1use std::io::Read;
9
10pub fn parse_jsonc(text: &str) -> Option<serde_json::Value> {
12 let text = text.strip_prefix('\u{feff}').unwrap_or(text);
14 let mut bytes = Vec::new();
15 std::io::BufReader::new(json_comments::StripComments::new(text.as_bytes()))
16 .read_to_end(&mut bytes)
17 .ok()?;
18 serde_json::from_str(&strip_trailing_commas(&String::from_utf8_lossy(&bytes))).ok()
19}
20
21pub fn strip_trailing_commas(input: &str) -> String {
24 let mut result = String::with_capacity(input.len());
25 let mut chars = input.chars().peekable();
26 let mut in_string = false;
27 let mut last_comma_pos: Option<usize> = None;
28
29 while let Some(ch) = chars.next() {
30 if in_string {
31 result.push(ch);
32 if ch == '\\' {
33 if let Some(&next) = chars.peek() {
34 result.push(next);
35 chars.next();
36 }
37 } else if ch == '"' {
38 in_string = false;
39 }
40 } else {
41 match ch {
42 '"' => {
43 in_string = true;
44 last_comma_pos = None;
45 result.push(ch);
46 }
47 ',' => {
48 last_comma_pos = Some(result.len());
49 result.push(ch);
50 }
51 '}' | ']' => {
52 if let Some(pos) = last_comma_pos {
53 result.replace_range(pos..pos + 1, " ");
54 }
55 last_comma_pos = None;
56 result.push(ch);
57 }
58 c if c.is_whitespace() => {
59 result.push(ch);
60 }
61 _ => {
62 last_comma_pos = None;
63 result.push(ch);
64 }
65 }
66 }
67 }
68 result
69}
70
71pub fn dev_mode_from(text: Option<&str>, dev_build: bool) -> bool {
76 text.and_then(parse_jsonc)
77 .and_then(|v| v.get("app:devmode").and_then(|b| b.as_bool()))
78 .unwrap_or(dev_build)
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn dev_mode_is_the_setting_when_it_is_set_and_the_build_when_it_is_not() {
87 for text in [None, Some(""), Some("{}"), Some("{ // nothing set
88}"), Some("{nope"), Some(r#"{"app:devmode": "yes"}"#)] {
89 assert!(dev_mode_from(text, true), "a dev build, {text:?}");
90 assert!(!dev_mode_from(text, false), "a release build, {text:?}");
91 }
92 let on = Some("{
93 // on
94 \"app:devmode\": true,
95}
96");
97 assert!(dev_mode_from(on, false), "on in a release build");
98 assert!(dev_mode_from(on, true));
99 let off = Some(r#"{"app:devmode": false, "other": 1}"#);
100 assert!(!dev_mode_from(off, true), "off in a dev build");
101 assert!(!dev_mode_from(off, false));
102 }
103
104 #[test]
105 fn a_byte_order_mark_is_not_part_of_the_json() {
106 let text = "\u{feff}{\"a\": 1,}";
107 assert_eq!(parse_jsonc(text).unwrap()["a"], 1);
108 }
109
110 #[test]
111 fn reads_comments_and_trailing_commas() {
112 let text = "{
113 // The splash screen.
114 \"splash:disabled\": true,
115 /* tray */ \"app:runinbackground\": false,
116 \"list\": [1, 2,],
117}
118";
119 let v = parse_jsonc(text).unwrap();
120 assert_eq!(v["splash:disabled"], true);
121 assert_eq!(v["app:runinbackground"], false);
122 assert_eq!(v["list"], serde_json::json!([1, 2]));
123 }
124
125 #[test]
126 fn leaves_strings_alone_and_refuses_what_isnt_json() {
127 assert_eq!(parse_jsonc(r#"{"a": "b,} // not a comment"}"#).unwrap()["a"], "b,} // not a comment");
128 assert!(parse_jsonc("{nope").is_none());
129 }
130
131 #[test]
132 fn strips_only_trailing_commas() {
133 assert_eq!(strip_trailing_commas(r#"{"a": 1,}"#), r#"{"a": 1 }"#);
134 assert_eq!(strip_trailing_commas(r#"[1, 2,]"#), r#"[1, 2 ]"#);
135 assert_eq!(strip_trailing_commas(r#"{"a": "b,}"}"#), r#"{"a": "b,}"}"#);
136 assert_eq!(strip_trailing_commas(r#"{"a": 1, "b": 2}"#), r#"{"a": 1, "b": 2}"#);
137 }
138}