C API for ocrs lib#257
Merged
Merged
Conversation
A small, stable `extern "C"` interface. `ocrs-capi` wraps `ocrs::OcrEngine` and builds as both a `cdylib` and a `staticlib`. Called it `ocrs_capi` (instead of `ocrs`) to avoid a collision with the `cdylib` from the `ocrs` crate itself. Included a README.md that explains things, and a Zig example, and unit tests.
Now the Rust source is the single source of truth for the C ABI header so it can no longer drift from the `extern "C"` signatures and `#[repr(C)]` types. Way better. - Replace hand-written `include/ocrs.h` with a cbindgen-generated header and add `cbindgen.toml` describing the output (C, OCRS_H guard, C++ extern "C" compat, `usize` to `size_t`, doc comments preserved). - Add a `header` test that regenerates the header in memory and fails if it differs from the committed file, so CI catches an API change made without regenerating. `OCRS_UPDATE_HEADER=1` rewrites it instead of comparing. The comparison tolerates CRLF and trailing whitespace for Windows checkouts. - Add a `make capi-header` target. cbindgen is a pinned, default-features-disabled dev-dependency, so compiled only for `cargo test`, so downstram consumers don't need it to build the library.
Adding tests that actually try to detect, layout, recognize path, using `ocrs_engine_get_text` and `ocrs_engine_get_text_lines` on a test image, assert substrings and non-degenerate bounding boxes. The model files are too big to commit, so they're `#[ignore]`d and using `OCRS_DETECTION_MODEL` / `OCRS_RECOGNITION_MODEL` from the env instead, skipping when unset. `make test-capi` downloads the models and runs them in release mode. The image is decoded with a new `image` dev-dependency. `lib` is added to the crate-type so the integration tests can link the crate as an rlib. This produces `libocrs_capi.rlib` and does not collide with the `ocrs` crate.
Fix warnings about safety docs reported by `make lint`. Add `# Safety` headings and regenerate the C header.
robertknight
approved these changes
Jun 29, 2026
robertknight
left a comment
Owner
There was a problem hiding this comment.
This looks great, thank-you very much. I was able to build and run the Zig example and run the tests via make test-capi. I pushed a couple of commits to fix warnings from make lint (add # Safety headings) and a few typos.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I wanted to play with ocrs in Zig and I noticed #2, so I went ahead and implemented it.
There's:
cbindgen(The name on the account, "p5p", is a bug by GitHub that I keep trying to get their support to resolve, but alas, no response. I can explain in more details if this is a concern. I would be happy if you can validate the name on the commits is "Sawyer X" after merging. Thank you.)