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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! REST
//! provides server implementations for REST API

#[macro_use]
pub mod macros;
pub mod api;
pub mod server;
pub mod structs;

use api::*;

use utoipa::OpenApi;

use svc_storage_client_grpc::prelude::*;

#[derive(OpenApi)]
#[openapi(
    paths(
        api::get_operator,
        api::get_all_aircraft,
        api::get_all_vertiports,
        api::get_all_vertipads,
        api::get_all_assets_by_operator,
        api::get_all_grouped_assets,
        api::get_all_grouped_assets_delegated_to,
        api::get_all_grouped_assets_delegated_from,
        api::get_aircraft_by_id,
        api::get_vertipad_by_id,
        api::get_vertiport_by_id,
        api::get_asset_group_by_id,
        api::register_aircraft,
        api::register_vertiport,
        api::register_vertipad,
        api::register_asset_group,
        api::update_aircraft,
        api::update_vertiport,
        api::update_vertipad,
        api::update_asset_group,
        api::remove_aircraft,
        api::remove_vertiport,
        api::remove_vertipad,
        api::remove_asset_group,
    ),
    components(
        schemas(
            vehicle::Data,
            vertiport::Data,
            vertipad::Data,
            GeoPoint,
            GeoPolygon,
            GeoLineString,
            RegisterAssetGroupPayload,
            UpdateAircraftPayload,
            UpdateVertiportPayload,
            UpdateVertipadPayload,
            structs::Operator,
            structs::Aircraft,
            structs::Vertiport,
            structs::Vertipad,
            structs::AssetGroup,
            structs::AssetStatus,
            structs::Basics,
        )
    ),
    tags(
        (name = "svc-assets", description = "svc-assets API")
    )
)]
struct ApiDoc;

/// Create OpenAPI3 Specification File
pub fn generate_openapi_spec(target: &str) -> Result<(), Box<dyn std::error::Error>> {
    let output = ApiDoc::openapi()
        .to_pretty_json()
        .expect("(ERROR) unable to write openapi specification to json.");

    std::fs::write(target, output).expect("(ERROR) unable to write json string to file.");

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_openapi_spec_generation() {
        assert!(generate_openapi_spec("/tmp/generate_openapi_spec.out").is_ok());
    }
}