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
//! Salvo integration for Injectium dependency injection.
//!
//! This crate provides seamless integration between the [Injectium](https://crates.io/crates/injectium)
//! dependency injection container and the [Salvo](https://salvo.rs) web framework.
//!
//! # Quick Start
//!
//! ## 1. Define your services with `#[derive(Injectable)]`
//!
//! ```ignore
//! use injectium::Injectable;
//!
//! #[derive(Clone)]
//! struct Database {
//! connection_string: String,
//! }
//!
//! #[derive(Clone, Injectable)]
//! struct UserService {
//! db: Database,
//! }
//! ```
//!
//! ## 2. Build the container
//!
//! ```ignore
//! use injectium::{cloned, container};
//! use std::sync::Arc;
//!
//! let container = Arc::new(container! {
//! providers: [
//! cloned(Database { connection_string: "postgres://localhost".into() }),
//! ],
//! });
//! ```
//!
//! ## 3. Configure Salvo router
//!
//! ```ignore
//! use salvo::prelude::*;
//! use injectium_salvo::{inject_container, Injected};
//!
//! #[handler]
//! async fn get_users(users: Injected<UserService>) -> String {
//! format!("Users DB: {}", users.db.connection_string)
//! }
//!
//! let router = Router::new()
//! .hoop(inject_container(container))
//! .push(Router::with_path("/users").get(get_users));
//! ```
//!
//! # Key Types
//!
//! - [`Injected<T>`] - Automatically resolves `T` from the container in your
//! handlers
//! - [`inject_container`] - Registers the container into Salvo's request scope
//!
//! # Features
//!
//! - `oapi` - Enable OpenAPI support for `Injected<T>` types
//!
//! # Error Handling
//!
//! If the container is not registered or a dependency is missing, the handler
//! will panic with a helpful message. Use [`Container::validate`] at startup to
//! catch missing dependencies early.
//!
//! # Thread Safety
//!
//! All injected types must implement `Send + Sync + 'static` to be safely
//! shared across async tasks.
//!
//! # Example
//!
//! See the [injectium examples](https://github.com/so1ve/injectium/tree/main/examples) for a complete working example.
use Deref;
use Arc;
use cfg_block;
use ;
use ;
use ;
use *;
/// A wrapper type for injecting dependencies from an Injectium container into
/// Salvo handlers.
///
/// `Injected<T>` automatically resolves `T` from the [`Container`] stored in
/// the Salvo [`Depot`]. The type `T` must implement [`injectium::Injectable`]
/// and `Send + Sync + 'static`.
///
/// # Example
///
/// ```ignore
/// use injectium::{Injectable, cloned, container};
/// use injectium_salvo::{Injected, inject_container};
/// use salvo::prelude::*;
/// use std::sync::Arc;
///
/// #[derive(Clone, Injectable)]
/// struct DbService {
/// conn: String,
/// }
///
/// #[handler]
/// async fn hello(user: Injected<DbService>) -> String {
/// format!("DB: {}", user.conn)
/// }
///
/// let container = Arc::new(container! {
/// providers: [cloned(DbService { conn: "localhost".into() })],
/// });
///
/// let router = Router::new()
/// .hoop(inject_container(container))
/// .push(Router::with_path("/hello").get(hello));
/// ```
;
cfg_block!
/// Register a [`Container`] into the Salvo [`Depot`] so that [`Injected<T>`]
/// can resolve dependencies from it.
///
/// Call this in your router setup, e.g.:
///
/// ```ignore
/// Router::new()
/// .hoop(inject_container(Arc::new(container)))
/// ...
/// ```