Write content to a temporary file in the same directory as `original` so that tool config discovery (which walks up from the file) works. Returns a `NamedTempFile` whose destructor automatically removes the file on drop. The caller must keep it alive until after reading back the formatted content.
(original: &Path, content: &str)
| 480 | /// file on drop. The caller must keep it alive until after reading back |
| 481 | /// the formatted content. |
| 482 | fn write_sibling_temp_file(original: &Path, content: &str) -> Result<NamedTempFile, String> { |
| 483 | let parent = original |
| 484 | .parent() |
| 485 | .ok_or_else(|| "Cannot determine parent directory of file".to_string())?; |
| 486 | |
| 487 | let mut temp = tempfile::Builder::new() |
| 488 | .prefix(".phpantom-fmt-") |
| 489 | .suffix(".php") |
| 490 | .tempfile_in(parent) |
| 491 | .map_err(|e| format!("Failed to create temp file in {}: {}", parent.display(), e))?; |
| 492 | |
| 493 | temp.write_all(content.as_bytes()) |
| 494 | .map_err(|e| format!("Failed to write temp file: {}", e))?; |
| 495 | |
| 496 | temp.flush() |
| 497 | .map_err(|e| format!("Failed to flush temp file: {}", e))?; |
| 498 | |
| 499 | Ok(temp) |
| 500 | } |
| 501 | |
| 502 | /// Result of running an external command. |
| 503 | struct CommandResult { |