openapiv3_resolve/
component.rs1use crate::Section;
2use indexmap::IndexMap;
3use openapiv3::{
4 Callback, Example, Header, Link, OpenAPI, Parameter, PathItem, ReferenceOr, RequestBody,
5 Response, Schema, SecurityScheme,
6};
7
8pub trait Component: sealed::Sealed + Sized {
26 const SECTION: Section;
28
29 fn section(openapi: &OpenAPI) -> Option<&IndexMap<String, ReferenceOr<Self>>>;
31}
32
33mod sealed {
34 pub trait Sealed {}
35}
36
37macro_rules! component {
38 ($ty:ty, $section:ident, $field:ident) => {
39 impl sealed::Sealed for $ty {}
40
41 impl Component for $ty {
42 const SECTION: Section = Section::$section;
43
44 fn section(openapi: &OpenAPI) -> Option<&IndexMap<String, ReferenceOr<Self>>> {
45 Some(&openapi.components.as_ref()?.$field)
46 }
47 }
48 };
49}
50
51component!(Callback, Callbacks, callbacks);
55component!(Example, Examples, examples);
56component!(Header, Headers, headers);
57component!(Link, Links, links);
58component!(Parameter, Parameters, parameters);
59component!(RequestBody, RequestBodies, request_bodies);
60component!(Response, Responses, responses);
61component!(Schema, Schemas, schemas);
62component!(SecurityScheme, SecuritySchemes, security_schemes);
63
64impl sealed::Sealed for PathItem {}
65
66impl Component for PathItem {
67 const SECTION: Section = Section::Paths;
68
69 fn section(openapi: &OpenAPI) -> Option<&IndexMap<String, ReferenceOr<Self>>> {
70 Some(&openapi.paths.paths)
71 }
72}