openapi_types/v3_0/
document.rs

1use crate::Output;
2use crate::v3_0::{ComponentsObject, InfoObject, OpenApiVersion, PathsObject};
3use crate::yaml::ToOpenApi;
4use gesha_collections::yaml::{YamlMap, YamlMapExt};
5
6/// OpenAPI Object
7/// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.4.md#openapi-object
8#[derive(Debug)]
9pub struct Document {
10    /// > REQUIRED. This string MUST be the version number of the OpenAPI Specification that the OpenAPI Document uses.
11    pub openapi: OpenApiVersion,
12
13    /// > REQUIRED. Provides metadata about the API.
14    pub info: InfoObject,
15
16    /// > REQUIRED. The available paths and operations for the API.
17    pub paths: PathsObject,
18
19    pub components: Option<ComponentsObject>,
20}
21
22impl ToOpenApi for Document {
23    fn apply(mut map: YamlMap) -> Output<Self> {
24        let (openapi, errors_of_openapi) = map
25            .extract_with_default("openapi", OpenApiVersion::from_string)
26            .into_tuple();
27
28        let (info, errors_of_info) = map
29            .extract_with_default("info", InfoObject::from_yaml_map)
30            .into_tuple();
31
32        let (paths, errors_of_paths) = map
33            .extract_with_default("paths", PathsObject::from_yaml_map)
34            .into_tuple();
35
36        let (components, errors_of_components) = map
37            .extract_if_exists("components", ToOpenApi::apply)
38            .into_tuple();
39
40        let document = Document {
41            openapi,
42            info,
43            paths,
44            components,
45        };
46
47        Output::ok(document)
48            .append(errors_of_openapi)
49            .append(errors_of_info)
50            .append(errors_of_paths)
51            .append(errors_of_components)
52    }
53}