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
//! Handler traits: IRequestHandler and IEventHandler.
//!
//! ## IRequestHandler<T, R>
//!
//! Dual-type-parameter handler: `T` is the request type, `R` is the response type.
//! The constraint `T: IRequest<R>` ensures type safety between request and response.
//!
//! `handle` takes `&mut self` so handlers that own a `DbContext` (resolved via
//! `get_owned` — EFCore-style per-request unit-of-work) can call `ctx.set::<T>()`
//! and `ctx.save_changes()` which require `&mut self`.
//!
//! ```ignore
//! #[async_trait]
//! impl IRequestHandler<GetUserRequest, UserModel> for GetUserHandler {
//! async fn handle(&mut self, req: GetUserRequest) -> Result<UserModel> { ... }
//! }
//! ```
use crateIClaims;
use crateResult;
use crate;
/// Handles a single `IRequest<R>`, producing its associated response `R`.
///
/// Authentication claims are NOT passed as a method parameter — this trait stays
/// free of auth concerns. Instead, requests that need claims implement the
/// inherent `set_claims` method (see `IClaimsCarrier`); the dispatcher injects
/// claims into the request *before* calling `handle`.
///
/// Register via `#[handler]` proc macro for compile-time collection,
/// or use `register_handlers!` for manual DI registration.
///
/// `handle(&mut self, ...)` enables the EFCore-style owned-DbContext pattern:
/// handlers declare a bare `ctx: DbContext` field, resolved per-request via
/// `IServiceResolver::get_owned`, and mutate it directly without `Arc<Mutex>`.
///
/// ```ignore
/// #[async_trait]
/// impl IRequestHandler<GetUserRequest, UserModel> for GetUserHandler {
/// async fn handle(&mut self, req: GetUserRequest) -> Result<UserModel> { ... }
/// }
/// ```
/// Blanket trait that enables claims injection on request structs.
///
/// The default implementation is a **no-op**, so every `T: Send` satisfies it
/// without any boilerplate. Requests that actually carry claims shadow the
/// trait method with an **inherent** `set_claims(&mut self, …)` method; Rust's
/// method resolution picks the inherent method over the trait default at
/// compile time, with zero runtime cost.
///
/// # Why not specialization?
///
/// Stable Rust has no specialization. The inherent-method-shadows-trait-default
/// pattern achieves the same "override per-type" behavior without nightly
/// features.
///
/// # Usage in contracts
///
/// Use the `#[claims]` attribute macro to automatically inject the `claims`
/// field and generate the inherent `set_claims` method:
///
/// ```ignore
/// use rust_webx::*;
/// use serde::Deserialize;
///
/// #[claims]
/// #[derive(Default, Deserialize)]
/// pub struct CreateBlogPostRequest {
/// pub slug: String,
/// // ...
/// }
/// ```
///
/// The macro expands to (conceptually):
///
/// ```ignore
/// pub struct CreateBlogPostRequest {
/// pub slug: String,
/// // ...
/// #[serde(skip)]
/// pub claims: Option<Box<dyn IClaims>>,
/// }
///
/// impl CreateBlogPostRequest {
/// pub fn set_claims(&mut self, claims: Option<Box<dyn IClaims>>) {
/// self.claims = claims;
/// }
/// }
/// ```
///
/// # Usage in handlers
///
/// ```ignore
/// async fn handle(&mut self, req: CreateBlogPostRequest) -> Result<BlogPostModel> {
/// let uid = req.claims.as_ref()
/// .and_then(|c| c.subject().parse().ok())
/// .ok_or(Error::Unauthorized)?;
/// // ...
/// }
/// ```
/// Blanket no-op implementation — every `Send` type is a carrier by default.
/// Handles a single `IEventRequest`, performing side effects.
///
/// ```ignore
/// #[async_trait]
/// impl IEventHandler<UserCreatedEvent> for SendWelcomeEmailHandler {
/// async fn handle(&self, event: UserCreatedEvent) -> Result<()> { ... }
/// }
/// ```
/// Background service that is started when the host starts and
/// stopped when the host performs a graceful shutdown.
///
/// Analogous to ASP.NET Core's IHostedService.
///
/// Use this for:
/// - Data initialization / seeding at application startup
/// - Background polling loops
/// - Queue consumers
/// - Connection pool warmup
///
/// # Example
///
/// ```ignore
/// #[derive(Default)]
/// struct DbInitService;
///
/// #[async_trait]
/// impl IHostedService for DbInitService {
/// async fn start(&self) -> Result<()> {
/// tracing::info!("[DbInitService] Running migrations...");
/// run_migrations().await?;
/// tracing::info!("[DbInitService] Seeding data...");
/// seed_data().await?;
/// Ok(())
/// }
///
/// async fn stop(&self) -> Result<()> {
/// tracing::info!("[DbInitService] Shutting down...");
/// Ok(())
/// }
/// }
/// ```