Skip to content

refactor(cli): deduplicate eleven identical image-loading closures into open_image - #1252

Closed
DebayanSen96 wants to merge 1 commit into
lablup:mainfrom
DebayanSen96:refactor/1245-open-image-helper
Closed

DebayanSen96 wants to merge 1 commit into
lablup:mainfrom
DebayanSen96:refactor/1245-open-image-helper

Conversation

@DebayanSen96

Copy link
Copy Markdown
Contributor

Fixes #1245.

The same image::open error closure was copy-pasted eleven times across generate_vlm.rs (10x) and generate_diffusion.rs (1x), and the copies had started drifting in formatting (one inline-capture variant at generate_vlm.rs:777, a bare anyhow! import in generate_diffusion.rs).

Changes:

  • Added one shared pub(crate) fn open_image(path: &Path) -> anyhow::Result<image::DynamicImage> in src/commands/mod.rs with the canonical message.
  • All eleven call sites now use open_image(path); the closure bodies are gone.
  • The user-facing error text is byte-identical to the pre-existing majority variant, so nothing observable changes.

No Rust toolchain on this machine, so I couldn't run cargo fmt/cargo test locally — I formatted the call sites to what rustfmt would produce (single-line .map(|path| open_image(path))) and the helper mirrors the exact code it replaces.

…to open_image

The same image::open error closure was copy-pasted eleven times across
generate_vlm.rs (10x) and generate_diffusion.rs (1x), and the copies
had drifted in formatting (inline capture style, bare anyhow! import).
Add one shared open_image(&Path) helper in commands/mod.rs and call it
from all eleven sites. User-facing error text is byte-identical, so
nothing observable changes.

Fixes lablup#1245
@DebayanSen96

Copy link
Copy Markdown
Contributor Author

Closing — contribution needs local build/test verification before review.

inureyes pushed a commit that referenced this pull request Sep 2, 2026
## Summary

Replaces the fourteen copy-pasted `image::open(path).map_err(...)` closures in `generate_vlm.rs` and `generate_diffusion.rs` with a single `open_image` helper. The error text is byte-identical to the previous majority form; the drifted variants (inline `{error}` capture, bare `anyhow!` import, mixed `|e|`/`|error|` binders) disappear.

## Related issues

Closes #1245

## Type of change

- [x] `refactor`: internal restructuring without behavior change

## Test plan

- [x] `cargo fmt --all -- --check`
- [x] `cargo check --workspace --all-targets --features metal,accelerate` (initially on a CPU-only MLX build via `MLXCEL_BUILD_METAL=OFF`; superseded by the Metal gate below)
- [x] `cargo clippy -p mlxcel --bin mlxcel --features metal,accelerate -- -D warnings` (clean; in particular no `redundant_closure` on the `.map(|path| open_image(path))` sites, since the closure performs the `&PathBuf` to `&Path` coercion)
- [x] `RUST_MIN_STACK=16777216 cargo test --features metal,accelerate -p mlxcel --bin mlxcel commands::` (169 passed)
- [x] `grep -rn "Failed to load image" src/commands/` matches exactly one line, the helper body
- [ ] Real-checkpoint validation not run: the change does not touch inference, only the error path when `image::open` fails, and the success path returns the same `DynamicImage` as before
- [x] Full local gate from CONTRIBUTING.md on a Metal MLX build, run on a local branch that merges all eight re-implementation PRs (#1583, #1584, #1585, #1586, #1587, #1589, #1590, #1591) onto `main`: `cargo clippy --workspace --all-targets --features metal,accelerate -- -D warnings` (clean) and `cargo test --workspace --profile test-fast --features metal,accelerate --no-fail-fast -- --test-threads=1` (114 test binaries, 10385 passed, 0 failed, 326 ignored)

## Notes for reviewers

This re-implements the closed PR #1252 against current `main`. The issue counted eleven sites; `main` has since grown to thirteen in `generate_vlm.rs` plus the one in `generate_diffusion.rs`, all converted here. The helper lives in `generate_vlm.rs` next to its heaviest users and is `pub(crate)` so `generate_diffusion.rs` can reach it as `super::generate_vlm::open_image`; a separate `commands/util.rs` felt heavy for a one-function module, but it is a trivial move if you would rather have it there.
@inureyes inureyes added type:refactor Code restructuring without changing functionality priority:low Low priority labels Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:low Low priority type:refactor Code restructuring without changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants