kynos_openapi/model/paths/
item.rs1use serde::{Deserialize, Serialize};
4
5use crate::model::{
6 extensions::Extensions,
7 parameter::Parameter,
8 paths::{method::Method, operation::Operation},
9 reference::RefOr,
10 server::Server,
11};
12
13#[cfg(feature = "openapi32")]
16use crate::Map;
17
18#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
27pub struct PathItem {
28 #[serde(rename = "$ref", default, skip_serializing_if = "Option::is_none")]
33 pub reference: Option<String>,
34
35 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub summary: Option<String>,
38
39 #[serde(default, skip_serializing_if = "Option::is_none")]
41 pub description: Option<String>,
42
43 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub get: Option<Box<Operation>>,
46
47 #[serde(default, skip_serializing_if = "Option::is_none")]
49 pub put: Option<Box<Operation>>,
50
51 #[serde(default, skip_serializing_if = "Option::is_none")]
53 pub post: Option<Box<Operation>>,
54
55 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub delete: Option<Box<Operation>>,
58
59 #[serde(default, skip_serializing_if = "Option::is_none")]
61 pub options: Option<Box<Operation>>,
62
63 #[serde(default, skip_serializing_if = "Option::is_none")]
65 pub head: Option<Box<Operation>>,
66
67 #[serde(default, skip_serializing_if = "Option::is_none")]
69 pub patch: Option<Box<Operation>>,
70
71 #[serde(default, skip_serializing_if = "Option::is_none")]
73 pub trace: Option<Box<Operation>>,
74
75 #[cfg(feature = "openapi32")]
79 #[serde(default, skip_serializing_if = "Option::is_none")]
80 pub query: Option<Box<Operation>>,
81
82 #[cfg(feature = "openapi32")]
88 #[serde(
89 rename = "additionalOperations",
90 default,
91 skip_serializing_if = "Map::is_empty"
92 )]
93 pub additional_operations: Map<Box<Operation>>,
94
95 #[serde(default, skip_serializing_if = "Vec::is_empty")]
97 pub servers: Vec<Server>,
98
99 #[serde(default, skip_serializing_if = "Vec::is_empty")]
104 pub parameters: Vec<RefOr<Parameter>>,
105
106 #[serde(flatten)]
108 pub extensions: Extensions,
109}
110
111impl PathItem {
112 #[must_use]
114 pub fn new() -> Self {
115 Self::default()
116 }
117
118 #[must_use]
120 pub fn operation(&self, method: Method) -> Option<&Operation> {
121 match method {
122 Method::Get => self.get.as_deref(),
123 Method::Put => self.put.as_deref(),
124 Method::Post => self.post.as_deref(),
125 Method::Delete => self.delete.as_deref(),
126 Method::Options => self.options.as_deref(),
127 Method::Head => self.head.as_deref(),
128 Method::Patch => self.patch.as_deref(),
129 Method::Trace => self.trace.as_deref(),
130 #[cfg(feature = "openapi32")]
131 Method::Query => self.query.as_deref(),
132 }
133 }
134
135 pub fn set_operation(&mut self, method: Method, operation: Operation) -> Option<Operation> {
137 let slot = match method {
138 Method::Get => &mut self.get,
139 Method::Put => &mut self.put,
140 Method::Post => &mut self.post,
141 Method::Delete => &mut self.delete,
142 Method::Options => &mut self.options,
143 Method::Head => &mut self.head,
144 Method::Patch => &mut self.patch,
145 Method::Trace => &mut self.trace,
146 #[cfg(feature = "openapi32")]
147 Method::Query => &mut self.query,
148 };
149 slot.replace(Box::new(operation)).map(|boxed| *boxed)
150 }
151
152 pub fn operations(&self) -> impl Iterator<Item = (Method, &Operation)> {
154 Method::all()
155 .iter()
156 .filter_map(move |&method| self.operation(method).map(|op| (method, op)))
157 }
158
159 #[must_use]
161 pub fn with_operation(mut self, method: Method, operation: Operation) -> Self {
162 self.set_operation(method, operation);
163 self
164 }
165}