Skip to main content

cmd_edit

Function cmd_edit 

Source
pub fn cmd_edit(beans_dir: &Path, id: &str) -> Result<()>
Expand description

Orchestrate the complete bn edit workflow for a bean.

The full edit workflow:

  1. Validate the bean ID format
  2. Find the bean file using discovery
  3. Load the current bean content as a backup
  4. Open the file in the user’s configured editor
  5. Load the edited content
  6. Validate and save with schema validation (updates timestamp)
  7. Rebuild the index to reflect changes

If validation fails, prompts user to retry, rollback, or abort. If editor subprocess fails, handles the error gracefully.

§Arguments

  • beans_dir - Path to the .beans directory
  • id - Bean ID to edit (e.g., “1”, “1.1”)

§Returns

  • Ok(()) if edit is successful and saved
  • Err if:
    • Bean ID not found
    • $EDITOR not set or editor not found
    • Editor exits with non-zero status
    • Validation fails and user chooses abort
    • I/O or index rebuild fails

§Examples

cmd_edit(Path::new(".beans"), "1")?;