Skip to content
AlphaJackPublic

About

Generates a typed Rust client and server from an OpenAPI description

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Honig

Playground Book

a dripping honeycomb cell

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

Install Honig as a pre-built binary with cargo-binstall:

cargo binstall cargo-honig

Add Honig to your crate

  1. Tell Honig where your description is. init adds a small table to your Cargo.toml:

    cargo honig init path/to/openapi.yaml

    init also prints the commands that your crate still needs. For a description with a pattern you'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.z is the version of your generator.

  2. Run the printed cargo add lines. The honig line already lists every feature that your description needs, such as regress for a pattern.

  3. Generate the module:

    cargo honig generate

    You now have the module in src/api. If the code needs a honig feature that is off, generate stops and prints the cargo add line that turns it on.

  4. Declare the module in your src/lib.rs or src/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.

Update the API

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.

Update Honig

Install the new generator, then generate again:

cargo binstall cargo-honig
cargo honig generate

The 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.

Full documentation

The book has a tutorial, guides, recipes and the reference.

About

Generates a typed Rust client and server from an OpenAPI description

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages