Crates.io | openapi-client-gen |
lib.rs | openapi-client-gen |
version | 0.0.2 |
source | src |
created_at | 2024-10-01 23:45:35.04438 |
updated_at | 2024-10-02 09:26:42.605012 |
description | Experimental OpenApi client generator for Rust with support for C-FFI and WASM |
homepage | |
repository | |
max_upload_size | |
id | 1393511 |
size | 400,363 |
Generate a single rust source file with a http-client based on an OpenApi 3.0 or 3.1 schema. This Project is still experimental and NOT anywhere near ready for any serious use.
The generated code will use the "reqwest" library to make http requests.
The focus of this project is providing a single .rs source file which you can copy (or generate) into your project. This .rs source file should work in a diverse set of development circumstances:
.dll
/.so
with only dependencies to the C-Runtime of the system and NOTHING else..a
archive containing object filesThe "feel" of using the api should be similar as possible for all of these. In addition to that the .rs file should not conflict with multiple other generated clients should an application need more than a single OpenApi api to do its job.
Generate api.rs
openapi-client-gen --help
openapi-client-gen --out src/api.rs schema.json
Toml of your project should contain at these dependencies to compile api.rs
.
I recommend using the latest version of the respective crates.
These are just the versions used at the time of writing this.
Depending on your target you will have to adjust this.
[dependencies]
json = "0.12.4"
either = "1.13.0"
http = "1.1.0"
urlencoding = "2.1"
linked-hash-map = "0.5.6"
#If you do not want to use the blocking feature then reqwest doesn't need to have the blocking feature either!
#Using rustls-tls is optional, I use it because you can more easily link with libc-musl.
#The stream feature is required.
reqwest = { version = "0.12.5", features = ["blocking", "rustls-tls", "stream"], default-features = false}
#Not needed if you don't want async feature or target wasm
#Feel free to reduce the amount of required features. To compile only 'codec' is needed.
tokio-util = {version = "0.7.11", features = ["full"]}
#Not needed if you don't want async feature or target wasm
#Feel free to reduce the amount of required features. To compile no particular features are needed.
tokio = { version = "1.39.2" , features = ["full"]}
[features]
#adjust for your target!
#ffi always requires blocking, or it won't compile. It doesn't care if "async" is present or not.
#wasm targets must use only "async"
#normal rust targets require at least either "async" or "blocking" but can have both.
default = ["async", "blocking", "ffi"]
ffi = []
blocking = []
async = []
Using cbindgen: Assuming your library crate is called "turbo_api" you will have to use cbindgen like this (and you will have to enable the ffi feature.)
cbindgen --config cbindgen.toml --lang c --output turbo_api.h
cbindgen.toml:
[parse.expand]
crates = ["turbo_api"]
As of writing this cbindgen requires using the nightly toolchain for header generation. You can compile the library shared object itself just fine with the stable toolchain.