Skip to content

feat: OCR language via config file / env var, and tessdata-dir selection - #9

Merged
green2grey merged 3 commits into
mainfrom
feat/ocr-config
Jul 6, 2026
Merged

feat: OCR language via config file / env var, and tessdata-dir selection#9
green2grey merged 3 commits into
mainfrom
feat/ocr-config

Conversation

@green2grey

Copy link
Copy Markdown
Member

Completes FR-006 and the tessdata half of Phase 7's OCR accuracy options.

Config module (quickview-core/src/config.rs)

  • ~/.config/quickview/config.toml, schema [ocr] lang / tessdata_dir / max_dimension (max_dimension accepted now, consumed by the upcoming downscale guardrail so configs don't error).
  • Precedence — lang: --lang > QUICKVIEW_LANG > config > eng; tessdata: --tessdata-dir > config. Blank values are unset at each level.
  • Unknown keys are typos: the whole file is warned about and ignored, never fatal (NFR-004). Resolution is pure functions (env passed in), tested headlessly.
  • Resolution happens in the invoking process; with the single-instance app only resolved values cross the instance boundary (the ipc codec grew an optional --tessdata-dir= token — same single-token framing as before).

OcrOptions

OcrOptions { lang, tessdata_dir } replaces the bare lang string end-to-end (LaunchOptions → ipc → ViewerController → cache key → tesseract runner). Arg construction is now a pure, unit-tested tesseract_args(); --tessdata-dir is emitted before the tsv quiet config names, which tesseract requires. Tessdata is a plain directory path rather than a fast/best enum — those sets have no conventional install location, users clone them anywhere (documented in templates/config.example.toml).

Cache key (ADR-0009)

The tessdata dir joins the blake3 key with a presence marker (NoneSome("")). One-time full cache invalidation: existing entries miss once and repopulate (no-eviction v1, nothing to clean). ProjectDirs triple deduplicated into config::project_dirs(), shared with cache_dir().

Testing

  • 36 tests green (new: lang precedence matrix incl. blank values, TOML load missing/partial/garbage/typo, tesseract_args ±tessdata, cache-key tessdata variants, ipc round-trip with/without tessdata-dir); clippy -D warnings clean.
  • Smoke: QUICKVIEW_LANG=deu reaches tesseract (fails cleanly without tesseract-data-deu, image still displays); --tessdata-dir /nonexistent ditto (/nonexistent/eng.traineddata in the error proves the flag flows); second open after the key-format change is a cache hit again.

FR-006 completion plus the tessdata half of Phase 7's accuracy options:

- New quickview-core config module: ~/.config/quickview/config.toml
  ([ocr] lang / tessdata_dir / max_dimension), loaded once in the invoking
  process. Precedence lang: --lang > QUICKVIEW_LANG > config > eng;
  tessdata: --tessdata-dir > config. Blank values are unset; unknown keys
  are treated as typos (whole file warned about and ignored, never fatal —
  NFR-004). max_dimension is accepted but only consumed by the upcoming
  downscale guardrail.
- OcrOptions {lang, tessdata_dir} replaces the bare lang string end to
  end (LaunchOptions, ipc argv codec, ViewerController, cache key,
  tesseract runner). Arg construction extracted into a pure, tested
  tesseract_args(); --tessdata-dir must precede the tsv/quiet config
  names.
- Cache key gains the tessdata dir with a presence marker per ADR-0009
  (None != Some("")); existing entries become a one-time miss and
  repopulate. ProjectDirs triple now lives in one place
  (config::project_dirs), shared with cache_dir().
- templates/config.example.toml documents the file, including pointing
  tessdata_dir at a tessdata_fast/tessdata_best checkout.
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 21 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2c7af836-d4d8-4635-bf56-9cbd8f011b66

📥 Commits

Reviewing files that changed from the base of the PR and between 18d936c and f932636.

📒 Files selected for processing (1)
  • crates/quickview/src/main.rs

Walkthrough

This PR adds a config.toml-based OCR configuration system with CLI/env/config precedence resolution, extends OcrOptions with a tessdata_dir field, and threads these options through tesseract invocation, cache key generation, IPC argv encoding, UI controller state, and CLI resolution, alongside documentation and example config updates.

Changes

OCR configuration and options plumbing

Layer / File(s) Summary
Config module and language resolution
crates/quickview-core/src/config.rs, crates/quickview-core/src/lib.rs, crates/quickview-core/Cargo.toml
New Config/OcrSection types, config_path(), load(), and resolve_lang() precedence logic with tests; module registered and toml dependency added.
OcrOptions and tesseract argv
crates/quickview-core/src/ocr/tesseract.rs
Adds OcrOptions struct with tessdata_dir; run_tesseract_tsv and new tesseract_args helper build argv with optional --tessdata-dir.
OCR cache key update
crates/quickview-core/src/cache.rs
ocr_cache_path now takes &OcrOptions; cache key hashes tessdata_dir with a presence marker; cache_dir uses config::project_dirs.
IPC argv codec
crates/quickview-ui/src/ipc.rs
to_argv/from_argv emit/parse --tessdata-dir; LaunchOptions built with OcrOptions instead of flat ocr_lang.
UI LaunchOptions and ViewerController
crates/quickview-ui/src/lib.rs, crates/quickview-ui/src/windows/shared.rs, .../full_viewer.rs, .../quick_preview.rs
LaunchOptions.ocr_lang replaced by ocr: OcrOptions; ViewerController stores/applies OcrOptions via set_ocr_options; OCR run/cache calls updated.
CLI resolution and wiring
crates/quickview/src/main.rs, crates/quickview/Cargo.toml
CLI lang becomes optional, adds tessdata_dir flag; new resolve_ocr_options applies CLI/env/config precedence; adds quickview-core dependency.
Docs and example config
.claude/CLAUDE.md, adrs/ADR-0009-Caching.md, docs/PHASED_PLAN.md, templates/config.example.toml
Documents precedence rules and cache key composition; adds example config.toml template.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI as main.rs
  participant ConfigModule as config.rs
  participant LaunchOptions
  participant ViewerController
  participant Cache as cache.rs
  participant Tesseract as tesseract.rs

  CLI->>ConfigModule: load(config_path)
  ConfigModule-->>CLI: Config
  CLI->>CLI: resolve_ocr_options(cli, env, config)
  CLI->>LaunchOptions: ocr: OcrOptions{lang, tessdata_dir}
  LaunchOptions->>ViewerController: set_ocr_options(ocr)
  ViewerController->>Cache: ocr_cache_path(file, &ocr_opts)
  ViewerController->>Tesseract: run_tesseract_tsv(&path, &ocr_opts)
Loading

Poem

A rabbit hops through configs deep,
tessdata paths it likes to keep,
lang and dir now hashed as one,
the cache key knows when work is done. 🐇
Hop, precedence, hop away—
CLI wins, then env, then file, then "eng" today!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the feature but does not follow the required template, missing the Summary heading and checklist items. Add a ## Summary section and a ## Checklist with the four required items (cargo fmt, cargo clippy, tests, docs).
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the OCR config/env and tessdata-dir changes and matches the PR scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/ocr-config

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 18d936c36d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/quickview/src/main.rs Outdated
let env_lang = std::env::var("QUICKVIEW_LANG").ok();
quickview_core::ocr::tesseract::OcrOptions {
lang: config::resolve_lang(cli_lang.as_deref(), env_lang.as_deref(), &cfg),
tessdata_dir: cli_tessdata_dir.or_else(|| cfg.ocr.tessdata_dir.clone()),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Resolve tessdata_dir before forwarding it

When --tessdata-dir or the config value is relative and a QuickView primary instance is already running, this stores and forwards the relative path unchanged; OCR later runs in the primary process, so the path is interpreted relative to that process's cwd rather than the invoking process's cwd. The image path is canonicalized at the app boundary for this same single-instance issue, but the tessdata path is not, so invocations from a different directory can make Tesseract look in the wrong tessdata directory and fail OCR despite a valid CLI/config path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid — same single-instance cwd trap the image path was canonicalized for in PR #4, and it also made the cache key spelling-dependent for one directory. Fixed in b70f343: the tessdata dir is canonicalized (or made absolute when missing, via std::path::absolute) in the invoking process before it crosses the instance boundary, with a unit test covering existing/missing/already-absolute cases.

A relative --tessdata-dir (or config value) forwarded to a running
primary instance resolved against the primary's cwd, not the invoking
process's — same single-instance cwd trap the image path is
canonicalized for. Canonicalize (or absolutize when missing) before the
value crosses the instance boundary; also makes the cache key stable
across spellings of the same directory. Found by Codex on PR #9.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/quickview-core/src/config.rs`:
- Around line 40-49: The OcrSection config currently treats blank tessdata_dir
values as set, so empty strings become Some(PathBuf::new()) instead of unset.
Update the config normalization in OcrSection to apply the same blank-as-unset
handling already used for lang, so an empty [ocr] tessdata_dir falls back to
None rather than being passed through to the OCR CLI. Locate the fix in the
OcrSection parsing/normalization logic in config.rs and ensure tessdata_dir is
cleared when the input string is blank.

In `@crates/quickview/src/main.rs`:
- Around line 72-92: The `resolve_ocr_options` function treats `tessdata_dir`
differently from `lang`, so blank CLI/config values are not normalized to unset.
Update the `tessdata_dir` resolution in `resolve_ocr_options` to trim and ignore
empty values before falling back to the next source, matching the behavior of
`config::resolve_lang`. Keep the change localized to the `OcrOptions`
construction path so empty `--tessdata-dir` or empty config values do not become
`Some(PathBuf::new())`.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c27feac4-0b58-4db5-9b06-65ea1e41facc

📥 Commits

Reviewing files that changed from the base of the PR and between 60e26bc and 18d936c.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (16)
  • .claude/CLAUDE.md
  • adrs/ADR-0009-Caching.md
  • crates/quickview-core/Cargo.toml
  • crates/quickview-core/src/cache.rs
  • crates/quickview-core/src/config.rs
  • crates/quickview-core/src/lib.rs
  • crates/quickview-core/src/ocr/tesseract.rs
  • crates/quickview-ui/src/ipc.rs
  • crates/quickview-ui/src/lib.rs
  • crates/quickview-ui/src/windows/full_viewer.rs
  • crates/quickview-ui/src/windows/quick_preview.rs
  • crates/quickview-ui/src/windows/shared.rs
  • crates/quickview/Cargo.toml
  • crates/quickview/src/main.rs
  • docs/PHASED_PLAN.md
  • templates/config.example.toml
📜 Review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: build-test
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Use rustfmt and clippy for code formatting and linting, as tracked in rust-toolchain.toml
Run clippy with all targets and features enabled with -D warnings flag to treat warnings as errors

Files:

  • crates/quickview-core/src/ocr/tesseract.rs
  • crates/quickview-ui/src/windows/quick_preview.rs
  • crates/quickview-core/src/lib.rs
  • crates/quickview-ui/src/windows/full_viewer.rs
  • crates/quickview-ui/src/lib.rs
  • crates/quickview/src/main.rs
  • crates/quickview-core/src/config.rs
  • crates/quickview-ui/src/ipc.rs
  • crates/quickview-ui/src/windows/shared.rs
  • crates/quickview-core/src/cache.rs
🔇 Additional comments (16)
crates/quickview-core/src/lib.rs (1)

6-6: LGTM!

crates/quickview-core/src/config.rs (1)

1-37: LGTM!

Also applies to: 51-64, 80-151

crates/quickview-core/src/ocr/tesseract.rs (1)

1-92: LGTM!

crates/quickview-core/src/cache.rs (1)

14-21: LGTM!

Also applies to: 23-59, 146-231, 232-286

crates/quickview-core/Cargo.toml (1)

17-17: 🚀 Performance & Scalability

No action needed for toml 0.8. The crate is only used for toml::from_str on owned config types, so the current pin is fine unless you want to take on the 1.x API changes.

crates/quickview-ui/src/ipc.rs (1)

9-10: LGTM!

Also applies to: 29-30, 41-56, 72-73, 83-86, 99-102, 135-148

crates/quickview-ui/src/lib.rs (1)

17-17: LGTM!

Also applies to: 30-32, 105-106

crates/quickview-ui/src/windows/shared.rs (1)

12-12: LGTM!

Also applies to: 42-42, 53-73, 87-93, 223-223, 244-249

crates/quickview-ui/src/windows/full_viewer.rs (1)

23-23: LGTM!

crates/quickview-ui/src/windows/quick_preview.rs (1)

30-30: LGTM!

crates/quickview/Cargo.toml (1)

19-19: LGTM!

crates/quickview/src/main.rs (1)

15-32: LGTM!

.claude/CLAUDE.md (1)

60-63: LGTM!

adrs/ADR-0009-Caching.md (1)

55-59: LGTM!

docs/PHASED_PLAN.md (1)

175-181: LGTM!

templates/config.example.toml (1)

1-24: LGTM!

Comment thread crates/quickview-core/src/config.rs
Comment thread crates/quickview/src/main.rs
An empty --tessdata-dir or config tessdata_dir = "" flowed through as
Some(PathBuf::new()), reaching tesseract as --tessdata-dir "" and
hashing as a distinct cache key — inconsistent with lang's
blank-as-unset rule. Filter blanks at each precedence level before
absolutizing. Found by CodeRabbit on PR #9.
@green2grey
green2grey merged commit e28c10f into main Jul 6, 2026
3 checks passed
@green2grey
green2grey deleted the feat/ocr-config branch July 6, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant