Honig generates a typed Rust client and server from an OpenAPI description.
You install two crates:
| Crate | Kind | What it does |
|---|---|---|
cargo-honig |
Binary | The command cargo honig. It generates a Rust module from your OpenAPI description. |
honig |
Library | A dependency of your crate. The generated module calls it for HTTP, routing, authentication and codecs. |
You write the description and your business logic. Honig writes the API layer between them: routing, decoding, parsing and error responses.
When the description changes, the compiler points at every line of yours that has to follow.
Is your description Swagger 2.0, OpenAPI 3.0 or OpenAPI 3.1? That works too. Honig upgrades it to 3.2 first.
Try it without installing anything: the playground generates the module from your description in the browser.
Install Honig as a pre-built binary with cargo-binstall:
cargo binstall cargo-honig-
Tell Honig where your description is.
initadds a small table to yourCargo.toml:cargo honig init path/to/openapi.yaml
initalso prints the commands that your crate still needs. For a description with apatternyou'll see:added [package.metadata.honig.api] to Cargo.toml, run these commands next: cargo add honig@=x.y.z -F client,server,regress cargo add serde -F derive cargo add serde_json cargo honig generate
x.y.zis the version of your generator. -
Run the printed
cargo addlines. Thehonigline already lists every feature that your description needs, such asregressfor apattern. -
Generate the module:
cargo honig generate
You now have the module in
src/api. If the code needs ahonigfeature that is off,generatestops and prints thecargo addline that turns it on. -
Declare the module in your
src/lib.rsorsrc/main.rs:pub mod api;
That's the whole setup. The book's tutorial continues from here: it builds the server and the client of a small honey shop.
Did the description change? Run cargo honig generate again. Every place where your code has to follow is now a compile error. A new operation is a missing method of your server. A new response is a missing match arm in the code that calls the client.
Add cargo honig generate --check to your CI. It fails when the module doesn't match the description or the version of the generator.
Install the new generator, then generate again:
cargo binstall cargo-honig
cargo honig generateThe generator and the honig dependency must have the same version. If they don't, generate stops and prints the cargo add line that sets the version. Run that line and generate again.
The book has a tutorial, guides, recipes and the reference.