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
//! Injectable trait for dependencies
use crate::;
/// Injectable trait for dependencies.
///
/// This trait defines how a type can be injected as a dependency.
/// Types implementing this trait can be used with `Depends<T>`.
///
/// # Blanket Implementations
///
/// The following blanket implementations are provided:
///
/// - **`Arc<T>`** where `T: Injectable` — injects the inner `T` and wraps it in `Arc`
/// - **`Depends<T>`** where `T: Send + Sync + 'static` — resolves `T` via the global
/// registry with caching and circular dependency detection
/// - **`Option<T>`** where `T: Injectable` — returns `None` on injection failure
/// instead of propagating the error
///
/// # Custom Implementation
///
/// To make a type injectable, use one of these approaches:
///
/// 1. **`#[injectable]` attribute macro** — generates an `Injectable` impl from
/// a constructor function
/// 2. **`#[injectable_factory]` attribute macro** — generates an `Injectable` impl
/// from a factory function
/// 3. **Manual `impl Injectable`** — implement the trait directly with
/// `#[async_trait]`
///
/// # Example
///
/// ```rust,no_run
/// use reinhardt_di::{Injectable, InjectionContext, DiResult, Depends};
/// use async_trait::async_trait;
///
/// # #[derive(Clone)]
/// # struct DbPool;
/// # impl DbPool {
/// # async fn connect() -> DiResult<Self> { Ok(DbPool) }
/// # }
/// struct Database {
/// pool: DbPool,
/// }
///
/// #[async_trait]
/// impl Injectable for Database {
/// async fn inject(_ctx: &InjectionContext) -> DiResult<Self> {
/// Ok(Database {
/// pool: DbPool::connect().await?,
/// })
/// }
/// }
/// ```
/// Blanket implementation of Injectable for `Arc<T>`
///
/// This allows using `Arc<T>` directly in endpoint handlers with `#[inject]`:
///
/// ```ignore
/// # use reinhardt_di::Injectable;
/// # use std::sync::Arc;
/// # struct DatabaseConnection;
/// # struct Response;
/// # type ViewResult<T> = Result<T, Box<dyn std::error::Error>>;
/// # use reinhardt_core::endpoint;
/// #[endpoint]
/// async fn handler(
/// #[inject] db: Arc<DatabaseConnection>,
/// ) -> ViewResult<Response> {
/// // ...
/// # Ok(Response)
/// }
/// ```
///
/// The implementation injects `T` first, then wraps it in `Arc`.
/// Blanket implementation of Injectable for `Depends<T>`
///
/// This allows using `Depends<T>` directly in endpoint handlers with `#[inject]`:
///
/// ```ignore
/// # use reinhardt_di::{Depends, Injectable};
/// # struct DatabaseConnection;
/// # struct Response;
/// # type ViewResult<T> = Result<T, Box<dyn std::error::Error>>;
/// # use reinhardt_core::endpoint;
/// #[endpoint]
/// async fn handler(
/// #[inject] db: Depends<DatabaseConnection>,
/// ) -> ViewResult<Response> {
/// // ...
/// # Ok(Response)
/// }
/// ```
///
/// The implementation delegates to `Depends::resolve()`, which resolves `T`
/// from the global registry with caching and circular dependency detection.
/// Falls back to `T::inject()` if the type is not in the global registry.
/// Blanket implementation of Injectable for `Option<T>`
///
/// This allows optional injection where failure results in `None`
/// instead of an error. Useful for endpoints that serve both
/// authenticated and anonymous users.
///
/// # Security Note
///
/// `Option<T>` swallows ALL injection errors into `None`.
/// For security-critical endpoints, use `T` directly to ensure
/// errors are surfaced as HTTP 401/500.
///
/// ```ignore
/// # use reinhardt_di::Injectable;
/// # struct AuthInfo;
/// # struct Response;
/// # type ViewResult<T> = Result<T, Box<dyn std::error::Error>>;
/// # use reinhardt_core::endpoint;
/// #[endpoint]
/// async fn handler(
/// #[inject] auth: Option<AuthInfo>,
/// ) -> ViewResult<Response> {
/// // auth is None if not authenticated
/// # Ok(Response)
/// }
/// ```