ensure_parent_dir

Function ensure_parent_dir 

Source
pub fn ensure_parent_dir(path: &Path) -> Result<()>
Expand description

Ensures that the parent directory of a file path exists.

This is a convenience function for creating the directory structure needed for a file before writing to it. It extracts the parent directory from the file path and ensures it exists.

§Arguments

  • path - The file path whose parent directory should exist

§Returns

  • Ok(()) if the parent directory exists or was created successfully
  • Err if directory creation fails
  • Ok(()) if the path has no parent (e.g., root level files)

§Examples

use agpm_cli::utils::fs::ensure_parent_dir;
use std::path::Path;

// Ensure directory structure exists before writing file
ensure_parent_dir(Path::new("output/agents/example.md"))?;
std::fs::write("output/agents/example.md", "# Example Agent")?;

§Use Cases

  • Preparing directory structure before file operations
  • Ensuring atomic writes have proper directory structure
  • Setting up output paths in batch processing

§See Also