Load a JSON file for **editing**. Unlike [`load_json_file`], this returns an error if the file exists but cannot be parsed, preventing silent data loss when the modified value is written back. # Error conditions - File exists but is not readable (permissions, I/O error). - File exists and has content but contains invalid JSON. Returns `Ok(json!({}))` only when the file does not exist or is empty
(path: &Path)
| 462 | /// Returns `Ok(json!({}))` only when the file does not exist or is empty, |
| 463 | /// which is safe for creating a new config from scratch. |
| 464 | pub fn load_json_file_strict(path: &Path) -> Result<serde_json::Value> { |
| 465 | if !path.exists() { |
| 466 | return Ok(serde_json::json!({})); |
| 467 | } |
| 468 | let contents = std::fs::read_to_string(path).map_err(|e| TraceDecayError::Config { |
| 469 | message: format!("cannot read {}: {e}", path.display()), |
| 470 | })?; |
| 471 | if contents.trim().is_empty() { |
| 472 | return Ok(serde_json::json!({})); |
| 473 | } |
| 474 | serde_json::from_str(&contents).map_err(|e| TraceDecayError::Config { |
| 475 | message: format!( |
| 476 | "cannot parse {} as JSON: {e}\n \ |
| 477 | Hint: fix the JSON syntax manually and re-run the command,\n \ |
| 478 | or delete the file to start fresh", |
| 479 | path.display() |
| 480 | ), |
| 481 | }) |
| 482 | } |
| 483 | |
| 484 | /// Create a backup copy of a config file before modifying it. |
| 485 | /// |