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
//! # diode
//!
//! A dependency injection framework for Rust applications that provides type-safe,
//! async-compatible dependency management with plugin-based architecture.
//!
//! ## Core Concepts
//!
//! - **App**: The main container that holds all registered components and services
//! - **Service**: A trait for defining injectable services with async initialization
//! - **Plugin**: A trait for modular components that can register services and dependencies
//! - **Components**: Raw objects stored in the app container
//! - **Dependencies**: Type-safe dependency declarations between services and plugins
//!
//! ## Basic Usage
//!
//! Simple service registration and retrieval:
//!
//! ```rust
//! use diode::{App, Service, StdError, AddServiceExt};
//! use std::sync::Arc;
//!
//! struct DatabaseService {
//! connection_string: String,
//! }
//!
//! impl Service for DatabaseService {
//! type Handle = Arc<Self>;
//!
//! async fn build(_app: &diode::AppContext) -> Result<Self::Handle, StdError> {
//! Ok(Arc::new(Self {
//! connection_string: "sqlite::memory:".to_string(),
//! }))
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let app = App::builder()
//! .add_service::<DatabaseService>()
//! .build()
//! .await?;
//!
//! let db = app.get_component::<Arc<DatabaseService>>().unwrap();
//! println!("Database connected: {}", db.connection_string);
//! Ok(())
//! }
//! ```
//!
//! ## Service Dependencies
//!
//! Services can depend on other services, with automatic dependency resolution:
//!
//! ```rust
//! use diode::{App, Service, StdError, Dependencies, ServiceDependencyExt, AddServiceExt};
//! use std::sync::Arc;
//!
//! struct ConfigService {
//! database_url: String,
//! }
//!
//! struct DatabaseService {
//! config: Arc<ConfigService>,
//! }
//!
//! struct ApiService {
//! database: Arc<DatabaseService>,
//! }
//!
//! impl Service for ConfigService {
//! type Handle = Arc<Self>;
//!
//! async fn build(_app: &diode::AppContext) -> Result<Self::Handle, StdError> {
//! Ok(Arc::new(Self {
//! database_url: "postgresql://localhost:5432/mydb".to_string(),
//! }))
//! }
//! }
//!
//! impl Service for DatabaseService {
//! type Handle = Arc<Self>;
//!
//! async fn build(app: &diode::AppContext) -> Result<Self::Handle, StdError> {
//! let config = app.get_component::<Arc<ConfigService>>()
//! .ok_or("ConfigService not found")?;
//!
//! Ok(Arc::new(Self { config }))
//! }
//!
//! fn dependencies() -> Dependencies {
//! Dependencies::new().service::<ConfigService>()
//! }
//! }
//!
//! impl Service for ApiService {
//! type Handle = Arc<Self>;
//!
//! async fn build(app: &diode::AppContext) -> Result<Self::Handle, StdError> {
//! let database = app.get_component::<Arc<DatabaseService>>()
//! .ok_or("DatabaseService not found")?;
//!
//! Ok(Arc::new(Self { database }))
//! }
//!
//! fn dependencies() -> Dependencies {
//! Dependencies::new().service::<DatabaseService>()
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let app = App::builder()
//! .add_service::<ConfigService>()
//! .add_service::<DatabaseService>()
//! .add_service::<ApiService>()
//! .build()
//! .await?;
//!
//! let api = app.get_component::<Arc<ApiService>>().unwrap();
//! println!("API service initialized with database URL: {}",
//! api.database.config.database_url);
//! Ok(())
//! }
//! ```
//!
//! ## Plugin System
//!
//! For more complex initialization logic, use plugins:
//!
//! ```rust
//! use diode::{App, Plugin, Dependencies, StdError, AppContext};
//!
//! struct DatabasePlugin {
//! connection_string: String,
//! }
//!
//! impl Plugin for DatabasePlugin {
//! async fn build(&self, ctx: &AppContext) -> Result<(), StdError> {
//! // Register components or perform complex initialization
//! ctx.add_component(self.connection_string.clone());
//! Ok(())
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let app = App::builder()
//! .add_plugin(DatabasePlugin {
//! connection_string: "postgresql://localhost:5432/mydb".to_string(),
//! })
//! .build()
//! .await?;
//!
//! let connection_string = app.get_component::<String>().unwrap();
//! println!("Database connection: {}", connection_string);
//! Ok(())
//! }
//! ```
//!
//! ## Using Macros
//!
//! With the `macros` feature enabled, service definition becomes much simpler:
//!
//! ```rust
//! use diode::{App, AddServiceExt, Component, Service};
//! use std::sync::Arc;
//!
//! #[derive(Service)]
//! struct DatabaseService {
//! #[inject(Component)]
//! connection_string: String,
//! }
//!
//! #[derive(Service)]
//! struct ApiService {
//! database: Arc<DatabaseService>,
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let app = App::builder()
//! .add_component("postgresql://localhost:5432/mydb".to_string())
//! .add_service::<DatabaseService>()
//! .add_service::<ApiService>()
//! .build()
//! .await?;
//!
//! let api = app.get_component::<Arc<ApiService>>().unwrap();
//! println!("Services initialized successfully");
//! Ok(())
//! }
//! ```
//!
//! ## Features
//!
//! - `macros` (default): Enables procedural macros for simplified service definitions
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;