Skip to main content

reinhardt_core/
endpoint.rs

1#![cfg(not(target_arch = "wasm32"))]
2
3//! Endpoint metadata trait for HTTP Method Macros
4//!
5//! This module provides the `EndpointInfo` trait that HTTP Method Macros
6//! (`#[get]`, `#[post]`, etc.) implement to provide route metadata.
7
8use hyper::Method;
9
10/// Endpoint metadata for OpenAPI generation
11///
12/// This struct is automatically submitted to the global inventory
13/// by HTTP method decorator macros (`#[get]`, `#[post]`, etc.) at compile time.
14/// It can be collected at runtime using `inventory::iter::<EndpointMetadata>()`.
15///
16/// # Example
17///
18/// ```rust,no_run
19/// use reinhardt_core::endpoint::EndpointMetadata;
20///
21/// // Collect all registered endpoints
22/// for metadata in inventory::iter::<EndpointMetadata>() {
23///     println!("{} {}", metadata.method, metadata.path);
24/// }
25/// ```
26// NOTE: #[non_exhaustive] is intentionally omitted in the pre-1.0 phase.
27// EndpointMetadata is constructed via struct literals in both proc-macro codegen
28// (reinhardt-core-macros) and test code across multiple crates. Adding
29// #[non_exhaustive] would require a builder or constructor, which adds complexity
30// without benefit before the public API stabilizes at 1.0.
31#[derive(Debug, Clone)]
32pub struct EndpointMetadata {
33	/// URL path pattern for this endpoint (e.g., "/users/{id}/").
34	pub path: &'static str,
35	/// HTTP method name (e.g., "GET", "POST").
36	pub method: &'static str,
37	/// Optional route name for URL reversal.
38	pub name: Option<&'static str>,
39	/// Name of the handler function.
40	pub function_name: &'static str,
41	/// Module path where the handler is defined.
42	pub module_path: &'static str,
43
44	/// Type name of the request body (e.g., "CreateUserRequest")
45	/// Extracted from parameter extractors like `Json<T>`, `Form<T>`, `Body<T>`
46	pub request_body_type: Option<&'static str>,
47
48	/// Content-Type of the request body (e.g., "application/json", "application/x-www-form-urlencoded")
49	pub request_content_type: Option<&'static str>,
50
51	/// Additional response definitions beyond the default 200
52	/// Each entry: (status_code, description)
53	pub responses: &'static [EndpointResponse],
54
55	/// Response headers
56	/// Each entry: (header_name, description)
57	pub headers: &'static [EndpointHeader],
58
59	/// Security requirements (e.g., "bearer", "api_key")
60	pub security: &'static [&'static str],
61}
62
63/// A response definition for an endpoint
64#[derive(Debug, Clone, Copy)]
65pub struct EndpointResponse {
66	/// HTTP status code (e.g., 201, 404)
67	pub status: u16,
68	/// Description of the response
69	pub description: &'static str,
70}
71
72/// A response header definition for an endpoint
73#[derive(Debug, Clone, Copy)]
74pub struct EndpointHeader {
75	/// Header name (e.g., "X-Request-Id")
76	pub name: &'static str,
77	/// Description of the header
78	pub description: &'static str,
79}
80
81// Register EndpointMetadata as a collectible type with inventory
82inventory::collect!(EndpointMetadata);
83
84/// Trait for endpoint metadata used by HTTP Method Macros
85///
86/// This trait is automatically implemented by HTTP Method Macros (`#[get]`, `#[post]`, etc.)
87/// for the generated View types. It provides the route path, HTTP method, and name
88/// for URL reversal.
89///
90/// # Examples
91///
92/// The HTTP Method Macro generates a View type that implements this trait:
93///
94/// ```rust,ignore
95/// # use reinhardt_core_macros::get;
96/// # use reinhardt_http::Response;
97/// # use reinhardt_core::endpoint::EndpointInfo;
98/// # use hyper::Method;
99/// #[get("/users/{id}/", name = "get_user")]
100/// pub async fn get_user(id: i64) -> Result<Response, Box<dyn std::error::Error>> {
101///     Ok(Response::ok())
102/// }
103///
104/// // Generates:
105/// // pub struct GetUserView;
106/// //
107/// // impl EndpointInfo for GetUserView {
108/// //     fn path() -> &'static str { "/users/{id}/" }
109/// //     fn method() -> Method { Method::GET }
110/// //     fn name() -> &'static str { "get_user" }
111/// // }
112/// ```
113pub trait EndpointInfo: Send + Sync {
114	/// Returns the route path pattern
115	///
116	/// Example: "/users/{id}/"
117	fn path() -> &'static str;
118
119	/// Returns the HTTP method for this endpoint
120	///
121	/// Example: Method::GET
122	fn method() -> Method;
123
124	/// Returns the route name for URL reversal
125	///
126	/// Example: "get_user"
127	fn name() -> &'static str;
128}