openapi_struct_gen/
lib.rs1#[cfg(feature = "build")]
2pub mod error;
3#[cfg(feature = "build")]
4mod generate;
5#[cfg(feature = "build")]
6mod parse;
7
8#[cfg(feature = "build")]
9use crate::error::GenError;
10#[cfg(feature = "build")]
11use openapiv3::OpenAPI;
12
13#[cfg(feature = "build")]
14pub fn generate<P1: AsRef<std::path::Path>, P2: AsRef<std::path::Path>>(
15 schema_filename: P1,
16 output_filename: P2,
17 derivatives: Option<&[&str]>,
18 imports: Option<&[(&str, &str)]>,
19 annotations_before: Option<&[(&str, Option<&[&str]>)]>,
20 annotations_after: Option<&[(&str, Option<&[&str]>)]>,
21) -> Result<(), GenError> {
22 let schema_filename = schema_filename.as_ref();
23 let data = std::fs::read_to_string(schema_filename)?;
24 let oapi: OpenAPI = match schema_filename.extension().map(|s| s.to_str().unwrap()) {
25 Some("json") => serde_json::from_str(&data)?,
26 Some("yaml") | Some("yml") => serde_yaml::from_str(&data)?,
27 o => return Err(GenError::WrongFileExtension(o.map(|s| s.to_owned()))),
28 };
29 let schemas_map = parse::parse_schema(oapi);
30 let resp = generate::generate(
31 schemas_map,
32 derivatives,
33 imports,
34 annotations_before,
35 annotations_after,
36 );
37 std::fs::write(output_filename, resp)?;
38 Ok(())
39}
40
41#[macro_export]
42macro_rules! include {
43 ($package: tt) => {
44 include!(concat!(env!("OUT_DIR"), concat!("/", $package, ".rs")));
45 };
46}