1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#[cfg(feature = "build")]
pub mod error;
#[cfg(feature = "build")]
mod generate;
#[cfg(feature = "build")]
mod parse;

#[cfg(feature = "build")]
use crate::error::GenError;
#[cfg(feature = "build")]
use openapiv3::OpenAPI;

#[cfg(feature = "build")]
pub fn generate<P1: AsRef<std::path::Path>, P2: AsRef<std::path::Path>>(
    schema_filename: P1,
    output_filename: P2,
    derivatives: Option<&[&str]>,
    imports: Option<&[(&str, &str)]>,
    annotations_before: Option<&[(&str, Option<&[&str]>)]>,
    annotations_after: Option<&[(&str, Option<&[&str]>)]>,
) -> Result<(), GenError> {
    let schema_filename = schema_filename.as_ref();
    let data = std::fs::read_to_string(schema_filename)?;
    let oapi: OpenAPI = match schema_filename.extension().map(|s| s.to_str().unwrap()) {
        Some("json") => serde_json::from_str(&data)?,
        Some("yaml") | Some("yml") => serde_yaml::from_str(&data)?,
        o => return Err(GenError::WrongFileExtension(o.map(|s| s.to_owned()))),
    };
    let schemas_map = parse::parse_schema(oapi);
    let resp = generate::generate(
        schemas_map,
        derivatives,
        imports,
        annotations_before,
        annotations_after,
    );
    std::fs::write(output_filename, resp)?;
    Ok(())
}

#[macro_export]
macro_rules! include {
    ($package: tt) => {
        include!(concat!(env!("OUT_DIR"), concat!("/", $package, ".rs")));
    };
}