Typed Rust calls and a clap command tree, both built from one OpenAPI document, with every write behind a dry-run gate.
What comes out is api.get_voucher(5)?.send(&client)? returning your own
Voucher, and toy vouchers get --id 5 on a command line nobody wrote. What
goes in is the vendor's OpenAPI 3.0 document plus OpenAPI Overlay 1.1
documents holding your corrections to it. This is not a typed model of an
OpenAPI document — for that, use openapiv3.
$ toy vouchers create --total 12.50 --currency EUR --status open # operations at the root
POST /vouchers HTTP/1.1
host: localhost:9999
content-type: application/json
{"total":"12.50","currency":"EUR","status":"open"}
dry run: nothing was sent. Add --commit to send it.$ cargo add typed-openapiThat adds the library with its default clap feature; the table under
Features lists the rest.
Before 1.0 any release can break the API. CHANGELOG.md names each
break and the repair it asks for.
The Rust caller gets owned types and one method per operation, with a
newtype wherever the document names a rule — Voucher.currency is a Currency
and cannot be built out of something that is not an ISO 4217 code — and a type
of your own wherever it cannot: Voucher.total is a fixed-point Money,
which no OpenAPI document has a way to describe.
The CLI consumer gets a two-level tree — one subcommand per resource the
document's paths name, one per operation under it, so PUT /vouchers/{id} is
vouchers update whatever the vendor called it — with flags from the parameters
and the request body, and dynamic shell completion. Mounting it under a name of
your own, or as the whole CLI, is two calls either way: docs/cli.md.
Both go through the same request builder, so the CLI and the typed caller cannot
disagree about what an operation is — and both run the document's pattern on
the same regex engine, so they cannot disagree about what a value is either:
$ toy vouchers create --total 1,50 --currency EUR --status open
error: invalid value '1,50' for '--total <STRING>': `1,50` is not a match for ^-?[0-9]+(\.[0-9]{1,2})?$The scalar rules retained from a parameter are enforced in the document's
own numbers — pattern, the lengths, the bounds, multipleOf, an enum of
numbers or booleans, and a list's minItems, maxItems and uniqueItems — on
the command line and in a typed call alike. A body field's flag runs the same
rules, and its generated type runs fewer: docs/validation.md says which.
A string enum keeps its choices and drops pattern and length rules.
A regex engine is linked in every feature set.
A read runs on sight. A write prints the exact bytes it would have sent and
stops, until --commit. Which operations write is the document's answer rather
than a guess from the HTTP method: a GET that stores a PDF is marked
x-cli-writes in your Overlay and is gated like any POST. The gate is
default-closed, and an operation the document does not describe has no
subcommand at all.
One word is not always enough. An operation that cannot be undone, or that
reaches a third party, names its own gates in the document: x-cli-gates: [enshrine] grows a required --enshrine demanded beside --commit, so the
hazard is typed out before the request is built.
This matters most when the CLI's user is an agent, which has to learn caution from the tool rather than bring it.
One command turns the vendor's document and your Overlays into four committed files: the corrected document, the schemas as Rust types, one typed wrapper per operation, and the document reduced to what a command line needs. A shipped binary reads that reduction — it parses no YAML and links no OpenAPI object model. A fifth is optional: a page counting what that reduction did, for docs that quote a number.
Corrections come in layers, applied in the order you name them, so the repairs
stay a document worth handing back to the vendor and the command-line markings
sit above them. The generator ships inside this crate behind the generate
feature, so your xtask calls it directly — see docs/generating.md.
A vendor revision that moves something you corrected fails the bless step, naming the layer it is in, rather than silently overwriting the correction; one that withdraws an operation or a field your code names fails the compiler. What is not caught is listed in docs/drift.md beside what is.
| feature | default | adds |
|---|---|---|
clap |
yes | tree: the command tree, and ArgMatches back to a sent request |
document |
no | Document::load, the Overlay engine, the $ref resolver |
generate |
no | the code generator a bless step calls. Implies document |
builder |
no | a named-argument builder beside each generated wrapper |
Each feature adds and removes whole items and never changes one, so a match on
one of the crate's errors is the same in every build, _ arm included.
No HTTP client, in any combination. The seam is a two-method trait over
http::Request<Vec<u8>>. The clap feature adds the command tree and
completion dependencies.
Writing an adapter, and the Recorder that answers a test from a script,
are in docs/client.md.
- No authentication. Sign the
http::Requestin your adapter. - No per-field flags for a body that is not flat. A nested,
oneOf,allOforanyOfbody goes out as one--json-body FILE. The adopter callsclient::fitswith the generated type to check its content before sending it (docs/validation.md). The typed wrapper takes that type either way. - No object parameters. An object, a schema that states a keyword the crate
does not read as a value, a schema with no
type, anin: cookie, acontent-described parameter, an unserialisablestyle, a name that will not kebab-case and a name that an earlier parameter of the operation has carry no flag: each is named on its subcommand's help, and refuses the document only where it isrequired. The headersAccept,Content-TypeandAuthorizationare not parameters at all, as OpenAPI says, so theAuthorizationheader is never a flag. A credential that the document declares under another name still is one. - No OpenAPI 3.1. The crate reads OpenAPI 3.0. A document whose
openapisays another version is refused, and the refusal names it: 3.1 spells some rules another way and means others that 3.0 does not have, so a reading as 3.0 would drop them without a word. The version is the vendor's, so an Overlay that changesopenapiis refused too. - No guessing at what a request needs. A document with no
serversis refused, where OpenAPI would send to/, and so is an operation with nooperationId. Each refusal names the repair, an Overlay that adds the server or the id (docs/overlay.md). A server URL with variables uses thedefaultof each. A path template whose variables and path parameters do not match is refused too, so{id}never goes on the wire. - One host for every request. A request goes to the first of the document's
servers, which the caller may replace. Aserverson a path item or on an operation is refused, and an Overlay that removes it is the repair. - No characters outside ASCII in a name. A command is
[a-z0-9-]. A path segment, a gate or a parameter that holds a character outside ASCII is refused or carried with no flag, and never dropped from the word or transliterated:straßeis not--stra-e. A path segment then wantsx-cli-groupandx-cli-commandin an Overlay. - No async CLI. The command tree is sync;
AsyncClientis for the typed caller. - A regex engine in every binary. Enforcing
patternlinksregress, and no feature removes it. - The reduced model is a binary blob, diffable only by regenerating it.
- Stable Rust. CI builds and tests the crate with all features on, on
current stable, and on nothing older.
rust-versionis 1.89, which the YAML reader declares. Only this repository's own recipes want nightly.
typed-openapi/ is the published crate and the only thing on crates.io;
everything under examples/toy/ is one adoption of it, held by the same
just gate CI runs — formatting, clippy, rustdoc, each feature alone and all
together, the tests, a bless that reproduces every committed artefact, the
crate built and tested from its own tarball, and an audit of the dependency
tree for advisories.
| docs/generating.md | the bless step, the Settings interface, the counts page, wiring your own xtask |
| docs/overlay.md | writing corrections as Overlay actions, and the tripwire form |
| docs/adoption.md | taking this to a vendor's document: the judgement calls no check reaches |
| docs/cli.md | mounting the tree, the gate, flag naming, completion |
| docs/client.md | the client seam, writing an adapter, Recorder |
| docs/validation.md | every rule that is enforced, where it runs, and what the engine costs |
| docs/builders.md | the builder feature and what it costs |
| docs/drift.md | every way a vendor revision is caught, and where |
examples/toy |
one adoption end to end, built and tested by CI |
CONTRIBUTING.md says how the project takes changes: for now it takes no outside issues and no outside pull requests. To report a vulnerability, read SECURITY.md: a report goes in privately, never in a public issue.
Licensed under either of Apache-2.0 or MIT, at your option.