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
//! Axum integration for the injectable framework.
//!
//! When the `axum` feature is enabled, `Inject<T>` implements Axum's
//! `FromRequestParts` trait, allowing dependencies to be injected directly
//! into handler parameters.
//!
//! # How It Works
//!
//! 1. Your Axum router state must implement [`InjectableState`]
//! 2. The framework provides [`AxumState`](crate::axum::AxumState) as a
//! convenience wrapper, or you can implement the trait for your own state
//! 3. `Inject<T>` is then usable as an extractor in any handler
//!
//! # Example
//!
//! ```rust,ignore
//! use injectable_rs::{Container, Injectable, Inject};
//! use injectable_rs::axum::AxumState;
//! use axum::{Router, routing::get};
//!
//! #[derive(Injectable, Default)]
//! pub struct Database;
//!
//! async fn handler(db: Inject<Database>) -> &'static str {
//! "OK"
//! }
//!
//! let container = Container::builder().build().await.unwrap();
//! let state = AxumState::new(container);
//! let app = Router::new()
//! .route("/", get(handler))
//! .with_state(state);
//! ```
use FromRequestParts;
use StatusCode;
use Parts;
use ;
use crate::;
/// Trait that the Axum state type must implement to enable `Inject<T>` extraction.
///
/// This trait bridges your Axum application state with the injectable
/// framework's resolution context. Any type that can provide a reference
/// to a [`ResolveContext`] can implement this trait.
///
/// # Provided Implementations
///
/// - `Container` in the public `injectable` crate implements
/// `InjectableState` directly
/// - `injectable_rs::axum::AxumState` wraps `Arc<Container>` for efficient
/// cloning in Axum's state management
///
/// # Custom State
///
/// You can implement `InjectableState` for your own state type to combine
/// the injectable container with other application state:
///
/// ```rust,ignore
/// struct MyAppState {
/// container: Arc<Container>,
/// app_name: String,
/// }
///
/// impl InjectableState for MyAppState {
/// fn resolve_context(&self) -> &ResolveContext {
/// self.container.context()
/// }
/// }
/// ```
/// Rejection type returned when `Inject<T>` extraction fails in an Axum handler.
///
/// This wraps an [`InjectableError`] and implements `IntoResponse`, returning
/// a `500 Internal Server Error` with the error message as the response body.
/// Dependency resolution failures are always server-side issues (missing
/// registrations, circular dependencies, construction errors), so 500 is
/// the appropriate status code.
/// `FromRequestParts` implementation for `Inject<T>`.
///
/// This allows `Inject<T>` to be used as an Axum extractor when the
/// router's state type implements [`InjectableState`]. The implementation
/// resolves `T` from the state's resolve context and wraps it in `Arc<T>`.
///
/// # Type Bounds
///
/// - `S: InjectableState + Send + Sync` — the Axum state must provide a
/// resolve context
/// - `T: Injectable` — the extracted type must be injectable
///
/// # Error Handling
///
/// If resolution fails (missing dependency, circular dependency, construction
/// error), an [`InjectableRejection`] is returned, which produces a 500
/// response with the error message.
///
/// # Example
///
/// ```rust,ignore
/// use injectable_rs::{Injectable, Inject};
///
/// #[derive(Injectable, Default)]
/// pub struct Database;
///
/// // Inject<Database> is automatically extracted from the Axum state
/// async fn get_users(db: Inject<Database>) -> String {
/// format!("Users from {:?}", &*db)
/// }
/// ```