Skip to content

refactor(cli): deduplicate eleven identical "Failed to load image" closures into one open_image helper #1245

Description

@inureyes

The same image-loading error closure is copy-pasted eleven times across two command files, and the copies have started drifting in formatting style.

Evidence

The closure

image::open(path).map_err(|e| anyhow::anyhow!("Failed to load image {:?}: {}", path, e))

appears at src/commands/generate_vlm.rs lines 587, 634, 777, 913, 1014, 1108, 1234, 1303, 1458, 1685 and at src/commands/generate_diffusion.rs:54. The user-facing message is identical at all eleven sites, but the formatting has drifted: most use the {:?}, {} positional style, while generate_vlm.rs:777 uses the inline style:

.map_err(|error| anyhow::anyhow!("Failed to load image {:?}: {error}", path))

and generate_diffusion.rs:54 uses a bare anyhow! import instead of anyhow::anyhow!. Any future change to the message (adding a hint, changing the path formatting) now needs eleven coordinated edits.

Suggested fix

Add one helper, either in generate_vlm.rs or in a shared commands util:

fn open_image(path: &Path) -> anyhow::Result<image::DynamicImage> {
    image::open(path).map_err(|e| anyhow::anyhow!("Failed to load image {:?}: {}", path, e))
}

and call it from all eleven sites. Mechanical change, roughly 40 lines net.

Acceptance criteria

  • A single open_image helper exists; grep -rn "Failed to load image" src/commands/ matches exactly one definition site
  • All eleven call sites converted to the helper
  • The user-facing error message is byte-identical to the current majority form (Failed to load image {:?}: {} with the path and the underlying error)
  • cargo build and existing tests pass unchanged

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:cliCommand-line interface / CLI flagsgood first issueGood for newcomerspriority:lowLow prioritystatus:readyReady to be worked ontype:refactorCode restructuring without changing functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions