1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct OpenAPISpec {
9 pub openapi: String,
11 pub info: Info,
13 pub paths: HashMap<String, PathItem>,
15 pub components: Option<Components>,
17}
18
19impl OpenAPISpec {
20 pub fn new(info: Info) -> Self {
22 Self {
23 openapi: "3.0.0".to_string(),
24 info,
25 paths: HashMap::new(),
26 components: None,
27 }
28 }
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct Info {
34 pub title: String,
36 pub version: String,
38 pub description: Option<String>,
40}
41
42impl Info {
43 pub fn new(title: String, version: String) -> Self {
45 Self {
46 title,
47 version,
48 description: None,
49 }
50 }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct PathItem {
56 pub get: Option<Operation>,
58 pub post: Option<Operation>,
60 pub put: Option<Operation>,
62 pub delete: Option<Operation>,
64 pub patch: Option<Operation>,
66}
67
68impl PathItem {
69 pub fn new() -> Self {
71 Self {
72 get: None,
73 post: None,
74 put: None,
75 delete: None,
76 patch: None,
77 }
78 }
79}
80
81impl Default for PathItem {
82 fn default() -> Self {
83 Self::new()
84 }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct Operation {
90 pub summary: Option<String>,
92 pub description: Option<String>,
94 pub parameters: Vec<Parameter>,
96 pub responses: HashMap<String, Response>,
98}
99
100impl Operation {
101 pub fn new() -> Self {
103 Self {
104 summary: None,
105 description: None,
106 parameters: Vec::new(),
107 responses: HashMap::new(),
108 }
109 }
110}
111
112impl Default for Operation {
113 fn default() -> Self {
114 Self::new()
115 }
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct Parameter {
121 pub name: String,
123 pub location: ParameterLocation,
125 pub required: bool,
127 pub schema: Schema,
129}
130
131impl Parameter {
132 pub fn new(name: String, location: ParameterLocation) -> Self {
134 Self {
135 name,
136 location,
137 required: false,
138 schema: Schema::new("string".to_string()),
139 }
140 }
141}
142
143#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
145#[serde(rename_all = "lowercase")]
146pub enum ParameterLocation {
147 Query,
149 Header,
151 Path,
153 Cookie,
155}
156
157#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct Response {
160 pub description: String,
162 pub content: Option<HashMap<String, MediaType>>,
164}
165
166impl Response {
167 pub fn new(description: String) -> Self {
169 Self {
170 description,
171 content: None,
172 }
173 }
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct MediaType {
179 pub schema: Schema,
181}
182
183impl MediaType {
184 pub fn new(schema: Schema) -> Self {
186 Self { schema }
187 }
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct Schema {
193 #[serde(rename = "type")]
195 pub schema_type: String,
196 pub properties: Option<HashMap<String, Schema>>,
198}
199
200impl Schema {
201 pub fn new(schema_type: String) -> Self {
203 Self {
204 schema_type,
205 properties: None,
206 }
207 }
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct Components {
213 pub schemas: HashMap<String, Schema>,
215}
216
217impl Components {
218 pub fn new() -> Self {
220 Self {
221 schemas: HashMap::new(),
222 }
223 }
224}
225
226impl Default for Components {
227 fn default() -> Self {
228 Self::new()
229 }
230}
231
232pub struct SchemaGenerator;
234
235impl SchemaGenerator {
236 pub fn new() -> Self {
238 Self
239 }
240
241 pub fn generate(&self) -> Schema {
243 Schema::new("object".to_string())
244 }
245}
246
247impl Default for SchemaGenerator {
248 fn default() -> Self {
249 Self::new()
250 }
251}
252
253#[derive(Debug, Clone)]
255pub struct EndpointInfo {
256 pub path: String,
258 pub method: String,
260 pub operation: Operation,
262}
263
264impl EndpointInfo {
265 pub fn new(path: String, method: String) -> Self {
267 Self {
268 path,
269 method,
270 operation: Operation::new(),
271 }
272 }
273}