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
//! Dependency Injection system for Spikard
//!
//! This module provides a comprehensive dependency injection system with:
//!
//! - **Type-safe dependency resolution**: Dependencies are stored as `Arc<dyn Any>` but
//! can be retrieved with type safety using `ResolvedDependencies::get<T>()`
//! - **Async resolution**: All dependencies can perform async operations during resolution
//! - **Batched parallel resolution**: Dependencies with no interdependencies are resolved
//! in parallel using topological sorting
//! - **Multiple caching strategies**:
//! - Singleton: Resolved once globally, cached forever
//! - Per-request cacheable: Resolved once per request
//! - Non-cacheable: Resolved every time
//! - **Cycle detection**: Circular dependencies are detected at registration time
//! - **Cleanup support**: Generator-pattern dependencies can register cleanup tasks
//!
//! # Architecture
//!
//! The DI system is built on several core components:
//!
//! - [`Dependency`] trait: The core abstraction that all dependencies implement
//! - [`DependencyContainer`]: Manages registration and resolution
//! - [`ResolvedDependencies`]: Stores resolved dependencies with type-safe access
//! - [`DependencyGraph`]: Handles topological sorting and cycle detection
//! - [`ValueDependency<T>`]: Simple static value dependencies
//! - [`FactoryDependency`]: Dynamic factory-based dependencies
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```ignore
//! use spikard_core::di::{DependencyContainer, ValueDependency, FactoryDependency};
//! use std::sync::Arc;
//!
//! # tokio_test::block_on(async {
//! let mut container = DependencyContainer::new();
//!
//! // Register a simple value dependency
//! let config = ValueDependency::new("database_url", "postgresql://localhost/mydb");
//! container.register("database_url".to_string(), Arc::new(config)).unwrap();
//!
//! // Register a factory dependency that depends on the config
//! let pool = FactoryDependency::builder("db_pool")
//! .depends_on(vec!["database_url".to_string()])
//! .factory(|_req, _data, resolved| {
//! Box::pin(async move {
//! let url: Arc<String> = resolved.get("database_url").unwrap();
//! let pool = format!("Pool connected to {}", *url);
//! Ok(Arc::new(pool) as Arc<dyn std::any::Any + Send + Sync>)
//! })
//! })
//! .singleton(true) // Share across all requests
//! .build().unwrap();
//! container.register("db_pool".to_string(), Arc::new(pool)).unwrap();
//!
//! // Resolve for a handler
//! use http::Request;
//! use crate::request_data::RequestData;
//! use std::collections::HashMap;
//!
//! let request = Request::builder().body(()).unwrap();
//! let request_data = RequestData {
//! path_params: Arc::new(HashMap::new()),
//! query_params: serde_json::Value::Null,
//! validated_params: None,
//! raw_query_params: Arc::new(HashMap::new()),
//! body: serde_json::Value::Null,
//! raw_body: None,
//! headers: Arc::new(HashMap::new()),
//! cookies: Arc::new(HashMap::new()),
//! method: "GET".to_string(),
//! path: "/".to_string(),
//! };
//!
//! let resolved = container
//! .resolve_for_handler(&["db_pool".to_string()], &request, &request_data)
//! .await
//! .unwrap();
//!
//! let pool: Option<Arc<String>> = resolved.get("db_pool");
//! assert!(pool.is_some());
//! # });
//! ```
//!
//! ## Request-Scoped Dependencies
//!
//! ```ignore
//! use spikard_core::di::{DependencyContainer, FactoryDependency};
//! use std::sync::Arc;
//!
//! # tokio_test::block_on(async {
//! let mut container = DependencyContainer::new();
//!
//! // Create a request-scoped dependency (e.g., request ID)
//! let request_id = FactoryDependency::builder("request_id")
//! .factory(|_req, _data, _resolved| {
//! Box::pin(async {
//! let id = uuid::Uuid::new_v4().to_string();
//! Ok(Arc::new(id) as Arc<dyn std::any::Any + Send + Sync>)
//! })
//! })
//! .cacheable(true) // Same ID throughout the request
//! .build().unwrap();
//!
//! container.register("request_id".to_string(), Arc::new(request_id)).unwrap();
//! # });
//! ```
//!
//! ## Accessing Request Data
//!
//! ```ignore
//! use spikard_core::di::{DependencyContainer, FactoryDependency};
//! use std::sync::Arc;
//!
//! # tokio_test::block_on(async {
//! let mut container = DependencyContainer::new();
//!
//! // Access headers, query params, etc.
//! let user_agent = FactoryDependency::builder("user_agent")
//! .factory(|_req, request_data, _resolved| {
//! let ua = request_data.headers
//! .get("user-agent")
//! .cloned()
//! .unwrap_or_else(|| "unknown".to_string());
//!
//! Box::pin(async move {
//! Ok(Arc::new(ua) as Arc<dyn std::any::Any + Send + Sync>)
//! })
//! })
//! .build().unwrap();
//!
//! container.register("user_agent".to_string(), Arc::new(user_agent)).unwrap();
//! # });
//! ```
//!
//! ## Cleanup Tasks
//!
//! ```ignore
//! use spikard_core::di::ResolvedDependencies;
//! use std::sync::Arc;
//!
//! # tokio_test::block_on(async {
//! let mut resolved = ResolvedDependencies::new();
//!
//! // Add a dependency with cleanup
//! resolved.insert("connection".to_string(), Arc::new("DB Connection"));
//!
//! // Register cleanup task
//! resolved.add_cleanup_task(Box::new(|| {
//! Box::pin(async {
//! println!("Closing database connection");
//! })
//! }));
//!
//! // Cleanup runs when resolved is dropped (or explicitly)
//! resolved.cleanup().await;
//! # });
//! ```
//!
//! # Performance
//!
//! The DI system is designed for high performance:
//!
//! - **Parallel resolution**: Independent dependencies are resolved concurrently
//! - **Efficient caching**: Singleton and per-request caching minimize redundant work
//! - **Arc-based sharing**: Values are reference-counted, not cloned
//! - **Zero-cost abstractions**: Type erasure has minimal overhead
//!
//! # Thread Safety
//!
//! All components are thread-safe:
//!
//! - `DependencyContainer` can be shared with `Arc<DependencyContainer>`
//! - Singleton cache uses `RwLock` for concurrent access
//! - All dependencies must be `Send + Sync`
pub use DependencyContainer;
pub use Dependency;
pub use DependencyError;
pub use ;
pub use ResolvedDependencies;
pub use ValueDependency;