Crates.io | openapi-struct-gen |
lib.rs | openapi-struct-gen |
version | 0.1.13 |
source | src |
created_at | 2022-08-03 13:04:38.386285 |
updated_at | 2023-05-05 10:41:11.341214 |
description | Generate rust structures from an openapi 3.0 definition |
homepage | |
repository | https://gitlab.com/amentis/openapi-struct-gen |
max_upload_size | |
id | 638036 |
size | 94,306 |
This crate generates Rust structures from OpenAPI 3.0 definitions.
[dependencies]
serde = "1.0.142"
openapi-struct-gen = "*"
[build-dependencies]
openapi-struct-gen = { version = "*", features = ["build"] }
use openapi_struct_gen::generate;
fn main() {
generate(
format!(
"{}/{}",
std::env::var("CARGO_MANIFEST_DIR").unwrap(),
"api.yaml"
),
format!("{}/{}", std::env::var("OUT_DIR").unwrap(), "oapi.rs"),
Some(&["Clone", "Serialize", "Deserialize"]),
Some(&[("serde", "Serialize"), ("serde", "Deserialize")]),
Some(&[(r#"#[skip_serializing_none]"#, None)]),
Some(&[(r#"#[serde(rename_all = "camelCase")]"#, Some(&["Struct"]))]),
).unwrap();
}
The first aparameter is path to oapi schema. The second is the target output rust file. The third is derive statements. The fourth is use statements, being tuples of the path to an object and the object the fifth is annotations that are to be put before the derive statement. Sometimes such are required, like serde_with. Each annotation consists of a tuple - the annotation itself and optional list of structs that are not to have this annotation The sixth is annotations that are to be put after the derive statement. Most annotations would be applied like that. Each annotation consists of a tuple - the annotation itself and optional list of structs that are not to have this annotation
openapi_struct_gen::include!("oapi");