reinhardt-urls 0.2.2

URL routing and proxy utilities for Reinhardt framework
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! OpenAPI integration for automatic schema generation
//!
//! This module provides utilities for generating OpenAPI schemas from registered routes.
//! It integrates with the `reinhardt-openapi` crate to provide automatic documentation.
//!
//! # Examples
//!
//! ```
//! use reinhardt_urls::routers::openapi_integration::{OpenApiBuilder, PathItem};
//! use hyper::Method;
//!
//! let mut builder = OpenApiBuilder::new("My API", "1.0.0");
//!
//! // Add routes
//! builder.add_path(
//!     "/api/users/",
//!     PathItem::new()
//!         .with_method(Method::GET, "List all users", Some("api:users:list"))
//! );
//!
//! // Generate OpenAPI spec
//! let spec = builder.build();
//! assert_eq!(spec.info.title, "My API");
//! ```

use crate::routers::introspection::{RouteInfo, RouteInspector};
use hyper::Method;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// OpenAPI specification version
const OPENAPI_VERSION: &str = "3.0.3";

/// OpenAPI specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenApiSpec {
	/// OpenAPI version
	pub openapi: String,

	/// API information
	pub info: InfoObject,

	/// Server information
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub servers: Vec<ServerObject>,

	/// API paths
	pub paths: HashMap<String, PathItemObject>,

	/// Reusable components
	#[serde(skip_serializing_if = "Option::is_none")]
	pub components: Option<ComponentsObject>,

	/// Security requirements
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub security: Vec<HashMap<String, Vec<String>>>,

	/// Tags for grouping operations
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub tags: Vec<TagObject>,
}

/// API information object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InfoObject {
	/// API title
	pub title: String,

	/// API version
	pub version: String,

	/// API description
	#[serde(skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,

	/// Terms of service URL
	#[serde(skip_serializing_if = "Option::is_none")]
	#[serde(rename = "termsOfService")]
	pub terms_of_service: Option<String>,

	/// Contact information
	#[serde(skip_serializing_if = "Option::is_none")]
	pub contact: Option<ContactObject>,

	/// License information
	#[serde(skip_serializing_if = "Option::is_none")]
	pub license: Option<LicenseObject>,
}

/// Contact information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContactObject {
	/// Contact name.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub name: Option<String>,

	/// Contact URL.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub url: Option<String>,

	/// Contact email address.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub email: Option<String>,
}

/// License information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LicenseObject {
	/// License name (e.g., `"MIT"`, `"Apache-2.0"`).
	pub name: String,

	/// URL to the full license text.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub url: Option<String>,
}

/// Server object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerObject {
	/// Base URL of the server.
	pub url: String,

	/// Human-readable description of the server.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
}

/// Path item object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathItemObject {
	/// GET operation for this path.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub get: Option<OperationObject>,

	/// POST operation for this path.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub post: Option<OperationObject>,

	/// PUT operation for this path.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub put: Option<OperationObject>,

	/// PATCH operation for this path.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub patch: Option<OperationObject>,

	/// DELETE operation for this path.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub delete: Option<OperationObject>,

	/// Path-level parameters shared by all operations.
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub parameters: Vec<ParameterObject>,
}

/// Operation object (HTTP method on a path)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OperationObject {
	/// Short summary of the operation.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub summary: Option<String>,

	/// Detailed description of the operation.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,

	/// Unique identifier for the operation.
	#[serde(skip_serializing_if = "Option::is_none")]
	#[serde(rename = "operationId")]
	pub operation_id: Option<String>,

	/// Tags for grouping this operation.
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub tags: Vec<String>,

	/// Operation-specific parameters.
	#[serde(skip_serializing_if = "Vec::is_empty")]
	pub parameters: Vec<ParameterObject>,

	/// Map of HTTP status codes to response descriptions.
	pub responses: HashMap<String, ResponseObject>,
}

/// Parameter object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterObject {
	/// Parameter name.
	pub name: String,

	/// Location of the parameter (`"path"`, `"query"`, `"header"`, or `"cookie"`).
	#[serde(rename = "in")]
	pub location: String,

	/// Human-readable description of the parameter.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,

	/// Whether the parameter is required.
	pub required: bool,

	/// Schema describing the parameter type.
	pub schema: SchemaObject,
}

/// Response object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseObject {
	/// Human-readable description of the response.
	pub description: String,

	/// Map of media types to their schemas.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub content: Option<HashMap<String, MediaTypeObject>>,
}

/// Media type object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaTypeObject {
	/// Schema describing the media type content.
	pub schema: SchemaObject,
}

/// Schema object (simplified)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaObject {
	/// The JSON Schema type (e.g., `"string"`, `"integer"`, `"object"`).
	#[serde(rename = "type")]
	pub schema_type: String,

	/// Additional format hint (e.g., `"int64"`, `"date-time"`).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub format: Option<String>,
}

/// Components object (reusable schemas)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentsObject {
	/// Map of schema names to their definitions.
	#[serde(skip_serializing_if = "HashMap::is_empty")]
	pub schemas: HashMap<String, SchemaObject>,
}

/// Tag object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TagObject {
	/// Tag name used for grouping operations.
	pub name: String,

	/// Human-readable description of the tag.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
}

/// Builder for creating PathItem objects
#[derive(Debug, Clone, Default)]
pub struct PathItem {
	operations: HashMap<Method, OperationObject>,
	parameters: Vec<ParameterObject>,
}

impl PathItem {
	/// Create a new PathItem builder
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::openapi_integration::PathItem;
	///
	/// let path_item = PathItem::new();
	/// ```
	pub fn new() -> Self {
		Self::default()
	}

	/// Add an operation for an HTTP method
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::openapi_integration::PathItem;
	/// use hyper::Method;
	///
	/// let path_item = PathItem::new()
	///     .with_method(Method::GET, "Get user", Some("users:detail"));
	/// ```
	pub fn with_method(
		mut self,
		method: Method,
		summary: impl Into<String>,
		operation_id: Option<impl Into<String>>,
	) -> Self {
		let operation = OperationObject {
			summary: Some(summary.into()),
			description: None,
			operation_id: operation_id.map(|s| s.into()),
			tags: Vec::new(),
			parameters: Vec::new(),
			responses: HashMap::new(),
		};

		self.operations.insert(method, operation);
		self
	}

	/// Add a path parameter
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::openapi_integration::PathItem;
	///
	/// let path_item = PathItem::new()
	///     .with_parameter("id", "User ID", "string");
	/// ```
	pub fn with_parameter(
		mut self,
		name: impl Into<String>,
		description: impl Into<String>,
		schema_type: impl Into<String>,
	) -> Self {
		let param = ParameterObject {
			name: name.into(),
			location: "path".to_string(),
			description: Some(description.into()),
			required: true,
			schema: SchemaObject {
				schema_type: schema_type.into(),
				format: None,
			},
		};

		self.parameters.push(param);
		self
	}

	/// Build the PathItemObject
	fn build(self) -> PathItemObject {
		let mut path_item = PathItemObject {
			get: None,
			post: None,
			put: None,
			patch: None,
			delete: None,
			parameters: self.parameters,
		};

		for (method, operation) in self.operations {
			match method {
				Method::GET => path_item.get = Some(operation),
				Method::POST => path_item.post = Some(operation),
				Method::PUT => path_item.put = Some(operation),
				Method::PATCH => path_item.patch = Some(operation),
				Method::DELETE => path_item.delete = Some(operation),
				_ => {}
			}
		}

		path_item
	}
}

/// OpenAPI specification builder
///
/// # Examples
///
/// ```
/// use reinhardt_urls::routers::openapi_integration::{OpenApiBuilder, PathItem};
/// use hyper::Method;
///
/// let mut builder = OpenApiBuilder::new("My API", "1.0.0");
/// builder.description("A sample API");
/// builder.add_server("https://api.example.com", None);
///
/// builder.add_path(
///     "/users/",
///     PathItem::new().with_method(Method::GET, "List users", Some("users:list"))
/// );
///
/// let spec = builder.build();
/// assert_eq!(spec.info.title, "My API");
/// ```
pub struct OpenApiBuilder {
	info: InfoObject,
	servers: Vec<ServerObject>,
	paths: HashMap<String, PathItemObject>,
	tags: Vec<TagObject>,
}

impl OpenApiBuilder {
	/// Create a new OpenAPI builder
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::openapi_integration::OpenApiBuilder;
	///
	/// let builder = OpenApiBuilder::new("My API", "1.0.0");
	/// ```
	pub fn new(title: impl Into<String>, version: impl Into<String>) -> Self {
		Self {
			info: InfoObject {
				title: title.into(),
				version: version.into(),
				description: None,
				terms_of_service: None,
				contact: None,
				license: None,
			},
			servers: Vec::new(),
			paths: HashMap::new(),
			tags: Vec::new(),
		}
	}

	/// Set API description
	pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
		self.info.description = Some(description.into());
		self
	}

	/// Add a server
	pub fn add_server(&mut self, url: impl Into<String>, description: Option<String>) -> &mut Self {
		self.servers.push(ServerObject {
			url: url.into(),
			description,
		});
		self
	}

	/// Add a tag
	pub fn add_tag(&mut self, name: impl Into<String>, description: Option<String>) -> &mut Self {
		self.tags.push(TagObject {
			name: name.into(),
			description,
		});
		self
	}

	/// Add a path
	pub fn add_path(&mut self, path: impl Into<String>, path_item: PathItem) -> &mut Self {
		self.paths.insert(path.into(), path_item.build());
		self
	}

	/// Build the OpenAPI specification
	pub fn build(self) -> OpenApiSpec {
		OpenApiSpec {
			openapi: OPENAPI_VERSION.to_string(),
			info: self.info,
			servers: self.servers,
			paths: self.paths,
			components: None,
			security: Vec::new(),
			tags: self.tags,
		}
	}

	/// Build from a RouteInspector
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::introspection::RouteInspector;
	/// use reinhardt_urls::routers::openapi_integration::OpenApiBuilder;
	/// use hyper::Method;
	///
	/// let mut inspector = RouteInspector::new();
	/// inspector.add_route("/users/", vec![Method::GET], Some("users:list"), None);
	///
	/// let spec = OpenApiBuilder::from_inspector(
	///     "My API",
	///     "1.0.0",
	///     &inspector
	/// ).build();
	///
	/// assert_eq!(spec.paths.len(), 1);
	/// ```
	pub fn from_inspector(
		title: impl Into<String>,
		version: impl Into<String>,
		inspector: &RouteInspector,
	) -> Self {
		let mut builder = Self::new(title, version);

		// Extract tags from namespaces
		for namespace in inspector.all_namespaces() {
			builder.add_tag(&namespace, None);
		}

		// Add paths
		for route in inspector.all_routes() {
			builder.add_route_info(route);
		}

		builder
	}

	/// Add a route from RouteInfo
	fn add_route_info(&mut self, route: &RouteInfo) {
		let mut path_item = PathItem::new();

		// Add parameters from path
		for param in &route.params {
			path_item = path_item.with_parameter(param, format!("{} parameter", param), "string");
		}

		// Add operations for each method
		for method_str in &route.methods {
			// Parse string back to Method
			if let Ok(method) = method_str.parse::<Method>() {
				let operation_id = route.name.clone();
				let summary = route
					.metadata
					.get("summary")
					.cloned()
					.unwrap_or_else(|| format!("{} {}", method.as_str(), route.path));

				path_item = path_item.with_method(method, summary, operation_id);
			}
		}

		self.paths.insert(route.path.clone(), path_item.build());
	}
}

impl OpenApiSpec {
	/// Export as JSON
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::openapi_integration::OpenApiBuilder;
	///
	/// let spec = OpenApiBuilder::new("My API", "1.0.0").build();
	/// let json = spec.to_json().unwrap();
	/// assert!(json.contains("My API"));
	/// ```
	pub fn to_json(&self) -> Result<String, serde_json::Error> {
		serde_json::to_string_pretty(self)
	}

	/// Export as YAML
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_urls::routers::openapi_integration::OpenApiBuilder;
	///
	/// let spec = OpenApiBuilder::new("My API", "1.0.0").build();
	/// let yaml = spec.to_yaml().unwrap();
	/// assert!(yaml.contains("My API"));
	/// ```
	pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
		serde_yaml::to_string(self)
	}
}

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

	#[test]
	fn test_openapi_builder_basic() {
		let mut builder = OpenApiBuilder::new("Test API", "1.0.0");
		builder.description("A test API");

		let spec = builder.build();
		assert_eq!(spec.info.title, "Test API");
		assert_eq!(spec.info.version, "1.0.0");
		assert_eq!(spec.info.description, Some("A test API".to_string()));
	}

	#[test]
	fn test_openapi_builder_with_server() {
		let mut builder = OpenApiBuilder::new("Test API", "1.0.0");
		builder.add_server(
			"https://api.example.com",
			Some("Production server".to_string()),
		);

		let spec = builder.build();
		assert_eq!(spec.servers.len(), 1);
		assert_eq!(spec.servers[0].url, "https://api.example.com");
	}

	#[test]
	fn test_path_item_builder() {
		let path_item = PathItem::new()
			.with_method(Method::GET, "Get user", Some("users:detail"))
			.with_parameter("id", "User ID", "string");

		let path_item_obj = path_item.build();
		assert!(path_item_obj.get.is_some());
		assert_eq!(path_item_obj.parameters.len(), 1);
	}

	#[test]
	fn test_openapi_builder_from_inspector() {
		let mut inspector = RouteInspector::new();
		inspector.add_route("/users/", vec![Method::GET], Some("users:list"), None);
		inspector.add_route(
			"/users/{id}/",
			vec![Method::GET],
			Some("users:detail"),
			None,
		);

		let spec = OpenApiBuilder::from_inspector("Test API", "1.0.0", &inspector).build();

		assert_eq!(spec.paths.len(), 2);
		assert!(spec.paths.contains_key("/users/"));
		assert!(spec.paths.contains_key("/users/{id}/"));
	}

	#[test]
	fn test_openapi_spec_to_json() {
		let spec = OpenApiBuilder::new("Test API", "1.0.0").build();
		let json = spec.to_json().unwrap();
		assert!(json.contains("Test API"));
		assert!(json.contains("3.0.3"));
	}
}