| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | pub fn ron_to_lua(ron_content: &str) -> Result<String, String> { |
| 4 | let mut lua_output = String::new(); |
| 5 | let defines = extract_defines(ron_content); |
| 6 | |
| 7 | lua_output.push_str("-- OXWM Configuration File (Lua)\n"); |
| 8 | lua_output.push_str("-- Migrated from config.ron\n"); |
| 9 | lua_output.push_str("-- Edit this file and reload with Mod+Shift+R (no compilation needed!)\n\n"); |
| 10 | |
| 11 | let terminal = resolve_value(&defines.get("$terminal").cloned().unwrap_or_else(|| "\"st\"".to_string()), &defines); |
| 12 | let modkey = resolve_value(&defines.get("$modkey").cloned().unwrap_or_else(|| "Mod4".to_string()), &defines); |
| 13 | let secondary_modkey = defines.get("$secondary_modkey").map(|v| resolve_value(v, &defines)); |
| 14 | |
| 15 | lua_output.push_str(&format!("local terminal = {}\n", terminal)); |
| 16 | lua_output.push_str(&format!("local modkey = \"{}\"\n", modkey.trim_matches('"'))); |
| 17 | if let Some(sec_mod) = secondary_modkey { |
| 18 | lua_output.push_str(&format!("local secondary_modkey = \"{}\"\n", sec_mod.trim_matches('"'))); |
| 19 | } |
| 20 | lua_output.push_str("\n"); |
| 21 | |
| 22 | lua_output.push_str("-- Color palette\n"); |
| 23 | lua_output.push_str("local colors = {\n"); |
| 24 | for (key, value) in &defines { |
| 25 | if key.starts_with("$color_") { |
| 26 | let color_name = &key[7..]; |
| 27 | let color_value = if value.starts_with("0x") { |
| 28 | format!("\"#{}\"", &value[2..]) |
| 29 | } else { |
| 30 | value.clone() |
| 31 | }; |
| 32 | lua_output.push_str(&format!(" {} = {},\n", color_name, color_value)); |
| 33 | } |
| 34 | } |
| 35 | lua_output.push_str("}\n\n"); |
| 36 | |
| 37 | lua_output.push_str("-- Main configuration table\n"); |
| 38 | lua_output.push_str("return {\n"); |
| 39 | |
| 40 | if let Some(config_start) = ron_content.find('(') { |
| 41 | let config_content = &ron_content[config_start + 1..]; |
| 42 | |
| 43 | lua_output.push_str(" -- Appearance\n"); |
| 44 | if let Some(val) = extract_field(config_content, "border_width") { |
| 45 | lua_output.push_str(&format!(" border_width = {},\n", val)); |
| 46 | } |
| 47 | if let Some(val) = extract_field(config_content, "border_focused") { |
| 48 | lua_output.push_str(&format!(" border_focused = {},\n", resolve_color_value(&val, &defines))); |
| 49 | } |
| 50 | if let Some(val) = extract_field(config_content, "border_unfocused") { |
| 51 | lua_output.push_str(&format!(" border_unfocused = {},\n", resolve_color_value(&val, &defines))); |
| 52 | } |
| 53 | if let Some(val) = extract_field(config_content, "font") { |
| 54 | lua_output.push_str(&format!(" font = {},\n", val)); |
| 55 | } |
| 56 | |
| 57 | lua_output.push_str("\n -- Window gaps\n"); |
| 58 | if let Some(val) = extract_field(config_content, "gaps_enabled") { |
| 59 | lua_output.push_str(&format!(" gaps_enabled = {},\n", val)); |
| 60 | } |
| 61 | if let Some(val) = extract_field(config_content, "gap_inner_horizontal") { |
| 62 | lua_output.push_str(&format!(" gap_inner_horizontal = {},\n", val)); |
| 63 | } |
| 64 | if let Some(val) = extract_field(config_content, "gap_inner_vertical") { |
| 65 | lua_output.push_str(&format!(" gap_inner_vertical = {},\n", val)); |
| 66 | } |
| 67 | if let Some(val) = extract_field(config_content, "gap_outer_horizontal") { |
| 68 | lua_output.push_str(&format!(" gap_outer_horizontal = {},\n", val)); |
| 69 | } |
| 70 | if let Some(val) = extract_field(config_content, "gap_outer_vertical") { |
| 71 | lua_output.push_str(&format!(" gap_outer_vertical = {},\n", val)); |
| 72 | } |
| 73 | |
| 74 | lua_output.push_str("\n -- Basics\n"); |
| 75 | if let Some(val) = extract_field(config_content, "modkey") { |
| 76 | let resolved = resolve_value(&val, &defines).trim_matches('"').to_string(); |
| 77 | if resolved == "modkey" { |
| 78 | lua_output.push_str(" modkey = modkey,\n"); |
| 79 | } else { |
| 80 | lua_output.push_str(&format!(" modkey = \"{}\",\n", resolved)); |
| 81 | } |
| 82 | } |
| 83 | if let Some(val) = extract_field(config_content, "terminal") { |
| 84 | let resolved = resolve_value(&val, &defines); |
| 85 | if resolved == "terminal" { |
| 86 | lua_output.push_str(" terminal = terminal,\n"); |
| 87 | } else { |
| 88 | lua_output.push_str(&format!(" terminal = {},\n", resolved)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | lua_output.push_str("\n -- Workspace tags\n"); |
| 93 | if let Some(val) = extract_field(config_content, "tags") { |
| 94 | lua_output.push_str(&format!(" tags = {},\n", convert_array_to_lua(&val))); |
| 95 | } |
| 96 | |
| 97 | lua_output.push_str("\n -- Layout symbol overrides\n"); |
| 98 | if let Some(val) = extract_field(config_content, "layout_symbols") { |
| 99 | lua_output.push_str(" layout_symbols = "); |
| 100 | lua_output.push_str(&convert_layout_symbols(&val)); |
| 101 | lua_output.push_str(",\n"); |
| 102 | } |
| 103 | |
| 104 | lua_output.push_str("\n -- Keybindings\n"); |
| 105 | if let Some(val) = extract_field(config_content, "keybindings") { |
| 106 | lua_output.push_str(" keybindings = "); |
| 107 | lua_output.push_str(&convert_keybindings(&val, &defines)); |
| 108 | lua_output.push_str(",\n"); |
| 109 | } |
| 110 | |
| 111 | lua_output.push_str("\n -- Status bar blocks\n"); |
| 112 | if let Some(val) = extract_field(config_content, "status_blocks") { |
| 113 | lua_output.push_str(" status_blocks = "); |
| 114 | lua_output.push_str(&convert_status_blocks(&val, &defines)); |
| 115 | lua_output.push_str(",\n"); |
| 116 | } |
| 117 | |
| 118 | lua_output.push_str("\n -- Color schemes for bar\n"); |
| 119 | if let Some(val) = extract_field(config_content, "scheme_normal") { |
| 120 | lua_output.push_str(" scheme_normal = "); |
| 121 | lua_output.push_str(&convert_color_scheme(&val, &defines)); |
| 122 | lua_output.push_str(",\n"); |
| 123 | } |
| 124 | if let Some(val) = extract_field(config_content, "scheme_occupied") { |
| 125 | lua_output.push_str(" scheme_occupied = "); |
| 126 | lua_output.push_str(&convert_color_scheme(&val, &defines)); |
| 127 | lua_output.push_str(",\n"); |
| 128 | } |
| 129 | if let Some(val) = extract_field(config_content, "scheme_selected") { |
| 130 | lua_output.push_str(" scheme_selected = "); |
| 131 | lua_output.push_str(&convert_color_scheme(&val, &defines)); |
| 132 | lua_output.push_str(",\n"); |
| 133 | } |
| 134 | |
| 135 | lua_output.push_str("\n -- Autostart commands\n"); |
| 136 | if let Some(val) = extract_field(config_content, "autostart") { |
| 137 | let converted = convert_array_to_lua(&val); |
| 138 | lua_output.push_str(" autostart = "); |
| 139 | lua_output.push_str(&converted); |
| 140 | lua_output.push_str(",\n"); |
| 141 | } else { |
| 142 | lua_output.push_str(" autostart = {},\n"); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | lua_output.push_str("}\n"); |
| 147 | |
| 148 | Ok(lua_output) |
| 149 | } |
| 150 | |
| 151 | fn extract_defines(content: &str) -> HashMap<String, String> { |
| 152 | let mut defines = HashMap::new(); |
| 153 | for line in content.lines() { |
| 154 | let trimmed = line.trim(); |
| 155 | if trimmed.starts_with("#DEFINE") { |
| 156 | if let Some(rest) = trimmed.strip_prefix("#DEFINE") { |
| 157 | if let Some(eq_pos) = rest.find('=') { |
| 158 | let var_name = rest[..eq_pos].trim().to_string(); |
| 159 | let value = rest[eq_pos + 1..].trim().trim_end_matches(',').to_string(); |
| 160 | defines.insert(var_name, value); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | defines |
| 166 | } |
| 167 | |
| 168 | fn resolve_value(value: &str, defines: &HashMap<String, String>) -> String { |
| 169 | if let Some(resolved) = defines.get(value) { |
| 170 | resolved.clone() |
| 171 | } else { |
| 172 | value.to_string() |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | fn resolve_color_value(value: &str, defines: &HashMap<String, String>) -> String { |
| 177 | let resolved = resolve_value(value, defines); |
| 178 | if resolved.starts_with("$color_") { |
| 179 | format!("colors.{}", &resolved[7..]) |
| 180 | } else if value.starts_with("$color_") { |
| 181 | format!("colors.{}", &value[7..]) |
| 182 | } else if resolved.starts_with("0x") { |
| 183 | format!("\"#{}\"", &resolved[2..]) |
| 184 | } else { |
| 185 | resolved |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn extract_field(content: &str, field_name: &str) -> Option<String> { |
| 190 | let pattern = format!("{}:", field_name); |
| 191 | let cleaned_content = remove_comments(content); |
| 192 | |
| 193 | if let Some(start) = cleaned_content.find(&pattern) { |
| 194 | let after_colon = &cleaned_content[start + pattern.len()..]; |
| 195 | let value_start = after_colon.trim_start(); |
| 196 | |
| 197 | if value_start.starts_with('[') { |
| 198 | extract_bracketed(value_start, '[', ']') |
| 199 | } else if value_start.starts_with('(') { |
| 200 | extract_bracketed(value_start, '(', ')') |
| 201 | } else if value_start.starts_with('"') { |
| 202 | if let Some(end) = value_start[1..].find('"') { |
| 203 | Some(value_start[..end + 2].to_string()) |
| 204 | } else { |
| 205 | None |
| 206 | } |
| 207 | } else { |
| 208 | let end = value_start |
| 209 | .find(|c: char| c == ',' || c == '\n' || c == ')') |
| 210 | .unwrap_or(value_start.len()); |
| 211 | Some(value_start[..end].trim().to_string()) |
| 212 | } |
| 213 | } else { |
| 214 | None |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | fn extract_bracketed(s: &str, open: char, close: char) -> Option<String> { |
| 219 | if !s.starts_with(open) { |
| 220 | return None; |
| 221 | } |
| 222 | let mut depth = 0; |
| 223 | let mut end = 0; |
| 224 | for (i, c) in s.char_indices() { |
| 225 | if c == open { |
| 226 | depth += 1; |
| 227 | } else if c == close { |
| 228 | depth -= 1; |
| 229 | if depth == 0 { |
| 230 | end = i + 1; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | if end > 0 { |
| 236 | Some(s[..end].to_string()) |
| 237 | } else { |
| 238 | None |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | fn convert_array_to_lua(ron_array: &str) -> String { |
| 243 | let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); |
| 244 | let items: Vec<&str> = inner.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).collect(); |
| 245 | format!("{{ {} }}", items.join(", ")) |
| 246 | } |
| 247 | |
| 248 | fn convert_layout_symbols(ron_array: &str) -> String { |
| 249 | let mut result = String::from("{\n"); |
| 250 | let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); |
| 251 | |
| 252 | let items = extract_all_bracketed(inner, '(', ')'); |
| 253 | for item in items { |
| 254 | let item_inner = item.trim_start_matches('(').trim_end_matches(')'); |
| 255 | if let (Some(name), Some(symbol)) = (extract_quoted_value(item_inner, "name"), extract_quoted_value(item_inner, "symbol")) { |
| 256 | result.push_str(&format!(" {{ name = \"{}\", symbol = \"{}\" }},\n", name, symbol)); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | result.push_str(" }"); |
| 261 | result |
| 262 | } |
| 263 | |
| 264 | fn convert_keybindings(ron_array: &str, defines: &HashMap<String, String>) -> String { |
| 265 | let mut result = String::from("{\n"); |
| 266 | let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); |
| 267 | |
| 268 | let items = extract_all_bracketed(inner, '(', ')'); |
| 269 | for item in items { |
| 270 | let binding = convert_keybinding(&item, defines); |
| 271 | result.push_str(&binding); |
| 272 | result.push_str(",\n"); |
| 273 | } |
| 274 | |
| 275 | result.push_str(" }"); |
| 276 | result |
| 277 | } |
| 278 | |
| 279 | fn convert_keybinding(ron_binding: &str, defines: &HashMap<String, String>) -> String { |
| 280 | let inner = ron_binding.trim_start_matches('(').trim_end_matches(')'); |
| 281 | |
| 282 | if inner.contains("keys:") { |
| 283 | convert_keychord(inner, defines) |
| 284 | } else { |
| 285 | convert_single_key(inner, defines) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn convert_keychord(inner: &str, defines: &HashMap<String, String>) -> String { |
| 290 | let mut result = String::from(" {\n keys = {\n"); |
| 291 | |
| 292 | if let Some(keys_str) = extract_field(inner, "keys") { |
| 293 | let keys = extract_all_bracketed(&keys_str, '(', ')'); |
| 294 | for key in keys { |
| 295 | let key_inner = key.trim_start_matches('(').trim_end_matches(')'); |
| 296 | let mods = extract_modifiers(key_inner, defines); |
| 297 | let key_name = extract_key(key_inner); |
| 298 | result.push_str(&format!(" {{ modifiers = {{ {} }}, key = \"{}\" }},\n", mods, key_name)); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | result.push_str(" },\n"); |
| 303 | |
| 304 | if let Some(action) = extract_identifier(inner, "action") { |
| 305 | result.push_str(&format!(" action = \"{}\",\n", action)); |
| 306 | } |
| 307 | |
| 308 | if let Some(arg) = extract_arg(inner, defines) { |
| 309 | result.push_str(&format!(" arg = {}\n", arg)); |
| 310 | } |
| 311 | |
| 312 | result.push_str(" }"); |
| 313 | result |
| 314 | } |
| 315 | |
| 316 | fn convert_single_key(inner: &str, defines: &HashMap<String, String>) -> String { |
| 317 | let mods = extract_modifiers(inner, defines); |
| 318 | let key = extract_key(inner); |
| 319 | let action = extract_identifier(inner, "action").unwrap_or_default(); |
| 320 | |
| 321 | let mut result = format!(" {{ modifiers = {{ {} }}, key = \"{}\", action = \"{}\"", mods, key, action); |
| 322 | |
| 323 | if let Some(arg) = extract_arg(inner, defines) { |
| 324 | result.push_str(&format!(", arg = {}", arg)); |
| 325 | } |
| 326 | |
| 327 | result.push_str(" }"); |
| 328 | result |
| 329 | } |
| 330 | |
| 331 | fn extract_modifiers(content: &str, defines: &HashMap<String, String>) -> String { |
| 332 | if let Some(mods_str) = extract_field(content, "modifiers") { |
| 333 | let inner = mods_str.trim_start_matches('[').trim_end_matches(']').trim(); |
| 334 | if inner.is_empty() { |
| 335 | return String::new(); |
| 336 | } |
| 337 | let mods: Vec<String> = inner |
| 338 | .split(',') |
| 339 | .map(|s| { |
| 340 | let trimmed = s.trim(); |
| 341 | if !trimmed.is_empty() { |
| 342 | let resolved = resolve_value(trimmed, defines); |
| 343 | format!("\"{}\"", resolved) |
| 344 | } else { |
| 345 | String::new() |
| 346 | } |
| 347 | }) |
| 348 | .filter(|s| !s.is_empty()) |
| 349 | .collect(); |
| 350 | mods.join(", ") |
| 351 | } else { |
| 352 | String::new() |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | fn extract_key(content: &str) -> String { |
| 357 | if let Some(key_str) = extract_identifier(content, "key") { |
| 358 | if key_str.starts_with("Key") && key_str.len() == 4 { |
| 359 | if let Some(digit) = key_str.chars().nth(3) { |
| 360 | if digit.is_ascii_digit() { |
| 361 | return digit.to_string(); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | key_str |
| 366 | } else { |
| 367 | String::from("Return") |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | fn extract_identifier(content: &str, field_name: &str) -> Option<String> { |
| 372 | let pattern = format!("{}:", field_name); |
| 373 | if let Some(start) = content.find(&pattern) { |
| 374 | let after_colon = &content[start + pattern.len()..]; |
| 375 | let value_start = after_colon.trim_start(); |
| 376 | let end = value_start |
| 377 | .find(|c: char| c == ',' || c == ')' || c == '\n') |
| 378 | .unwrap_or(value_start.len()); |
| 379 | Some(value_start[..end].trim().to_string()) |
| 380 | } else { |
| 381 | None |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | fn extract_arg(content: &str, defines: &HashMap<String, String>) -> Option<String> { |
| 386 | if let Some(arg_str) = extract_field(content, "arg") { |
| 387 | let resolved = resolve_value(&arg_str, defines); |
| 388 | if resolved.starts_with('[') { |
| 389 | Some(convert_array_to_lua(&resolved)) |
| 390 | } else if resolved.starts_with('"') || resolved.parse::<i32>().is_ok() || resolved.starts_with("0x") { |
| 391 | Some(resolved) |
| 392 | } else { |
| 393 | Some(format!("\"{}\"", resolved)) |
| 394 | } |
| 395 | } else { |
| 396 | None |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | fn convert_status_blocks(ron_array: &str, defines: &HashMap<String, String>) -> String { |
| 401 | let mut result = String::from("{\n"); |
| 402 | let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); |
| 403 | |
| 404 | let items = extract_all_bracketed(inner, '(', ')'); |
| 405 | for item in items { |
| 406 | let block = convert_status_block(&item, defines); |
| 407 | if !block.trim().ends_with("{\n }") { |
| 408 | result.push_str(&block); |
| 409 | result.push_str(",\n"); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | result.push_str(" }"); |
| 414 | result |
| 415 | } |
| 416 | |
| 417 | fn convert_status_block(ron_block: &str, defines: &HashMap<String, String>) -> String { |
| 418 | let mut result = String::from(" {\n"); |
| 419 | let inner = ron_block.trim_start_matches('(').trim_end_matches(')'); |
| 420 | |
| 421 | if let Some(format) = extract_field(inner, "format") { |
| 422 | result.push_str(&format!(" format = {},\n", format)); |
| 423 | } |
| 424 | if let Some(command) = extract_field(inner, "command") { |
| 425 | result.push_str(&format!(" command = {},\n", command)); |
| 426 | } |
| 427 | if let Some(command_arg) = extract_field(inner, "command_arg") { |
| 428 | result.push_str(&format!(" command_arg = {},\n", command_arg)); |
| 429 | } |
| 430 | if inner.contains("battery_formats:") { |
| 431 | if let Some(battery_str) = extract_field(inner, "battery_formats") { |
| 432 | result.push_str(" battery_formats = {\n"); |
| 433 | let battery_inner = battery_str.trim_start_matches('(').trim_end_matches(')'); |
| 434 | if let Some(charging) = extract_quoted_value(battery_inner, "charging") { |
| 435 | result.push_str(&format!(" charging = \"{}\",\n", charging)); |
| 436 | } |
| 437 | if let Some(discharging) = extract_quoted_value(battery_inner, "discharging") { |
| 438 | result.push_str(&format!(" discharging = \"{}\",\n", discharging)); |
| 439 | } |
| 440 | if let Some(full) = extract_quoted_value(battery_inner, "full") { |
| 441 | result.push_str(&format!(" full = \"{}\"\n", full)); |
| 442 | } |
| 443 | result.push_str(" },\n"); |
| 444 | } |
| 445 | } |
| 446 | if let Some(interval) = extract_field(inner, "interval_secs") { |
| 447 | let interval_val = if interval.len() > 10 { |
| 448 | "999999999".to_string() |
| 449 | } else { |
| 450 | interval |
| 451 | }; |
| 452 | result.push_str(&format!(" interval_secs = {},\n", interval_val)); |
| 453 | } |
| 454 | if let Some(color) = extract_field(inner, "color") { |
| 455 | let resolved = resolve_color_value(&color, defines); |
| 456 | result.push_str(&format!(" color = {},\n", resolved)); |
| 457 | } |
| 458 | if let Some(underline) = extract_field(inner, "underline") { |
| 459 | result.push_str(&format!(" underline = {}\n", underline)); |
| 460 | } |
| 461 | |
| 462 | result.push_str(" }"); |
| 463 | result |
| 464 | } |
| 465 | |
| 466 | fn convert_color_scheme(ron_scheme: &str, defines: &HashMap<String, String>) -> String { |
| 467 | let mut result = String::from("{\n"); |
| 468 | let inner = ron_scheme.trim_start_matches('(').trim_end_matches(')'); |
| 469 | |
| 470 | if let Some(fg) = extract_field(inner, "foreground") { |
| 471 | let resolved = resolve_color_value(&fg, defines); |
| 472 | result.push_str(&format!(" foreground = {},\n", resolved)); |
| 473 | } |
| 474 | if let Some(bg) = extract_field(inner, "background") { |
| 475 | let resolved = resolve_color_value(&bg, defines); |
| 476 | result.push_str(&format!(" background = {},\n", resolved)); |
| 477 | } |
| 478 | if let Some(ul) = extract_field(inner, "underline") { |
| 479 | let resolved = resolve_color_value(&ul, defines); |
| 480 | result.push_str(&format!(" underline = {}\n", resolved)); |
| 481 | } |
| 482 | |
| 483 | result.push_str(" }"); |
| 484 | result |
| 485 | } |
| 486 | |
| 487 | fn extract_all_bracketed(s: &str, open: char, close: char) -> Vec<String> { |
| 488 | let mut results = Vec::new(); |
| 489 | let mut depth = 0; |
| 490 | let mut start = None; |
| 491 | |
| 492 | let cleaned = remove_comments(s); |
| 493 | |
| 494 | for (i, c) in cleaned.char_indices() { |
| 495 | if c == open { |
| 496 | if depth == 0 { |
| 497 | start = Some(i); |
| 498 | } |
| 499 | depth += 1; |
| 500 | } else if c == close { |
| 501 | depth -= 1; |
| 502 | if depth == 0 { |
| 503 | if let Some(start_idx) = start { |
| 504 | results.push(cleaned[start_idx..=i].to_string()); |
| 505 | start = None; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | results |
| 512 | } |
| 513 | |
| 514 | fn remove_comments(s: &str) -> String { |
| 515 | let mut result = String::new(); |
| 516 | for line in s.lines() { |
| 517 | let mut in_string = false; |
| 518 | let mut comment_start = None; |
| 519 | |
| 520 | for (i, c) in line.char_indices() { |
| 521 | if c == '"' && (i == 0 || line.chars().nth(i - 1) != Some('\\')) { |
| 522 | in_string = !in_string; |
| 523 | } |
| 524 | if !in_string && i + 1 < line.len() && &line[i..i + 2] == "//" { |
| 525 | comment_start = Some(i); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if let Some(pos) = comment_start { |
| 531 | result.push_str(&line[..pos]); |
| 532 | } else { |
| 533 | result.push_str(line); |
| 534 | } |
| 535 | result.push('\n'); |
| 536 | } |
| 537 | result |
| 538 | } |
| 539 | |
| 540 | fn extract_quoted_value(content: &str, field_name: &str) -> Option<String> { |
| 541 | let pattern = format!("{}:", field_name); |
| 542 | if let Some(start) = content.find(&pattern) { |
| 543 | let after_colon = &content[start + pattern.len()..]; |
| 544 | let trimmed = after_colon.trim_start(); |
| 545 | if trimmed.starts_with('"') { |
| 546 | if let Some(end) = trimmed[1..].find('"') { |
| 547 | return Some(trimmed[1..end + 1].to_string()); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | None |
| 552 | } |