injectable-rs-runtime 0.1.0

Runtime types and traits for the injectable-rs DI framework
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Provider registry for dynamically-registered external types.
//!
//! This module provides the [`ProviderRegistry`] — a type-safe store for
//! [`DynProvider`] instances. It uses `TypeId` + a **token string** internally
//! for lookup, allowing multiple providers of the same type to coexist as long
//! as they carry different tokens.
//!
//! # Token-based Registration
//!
//! Every registration is keyed by `(TypeId<T>, token)`. The token is an
//! arbitrary `&str` that disambiguates providers of the same type:
//!
//! ```rust,ignore
//! let mut registry = ProviderRegistry::new();
//!
//! // Two pools of the same type, differentiated by token
//! registry.register("primary",  DynProvider::new(|| async { Ok(PrimaryPool::connect()) }));
//! registry.register("replica",  DynProvider::new(|| async { Ok(ReplicaPool::connect()) }));
//! registry.register("analytics", DynProvider::new(|| async { Ok(AnalyticsPool::connect()) }));
//! ```
//!
//! Use [`DEFAULT_TOKEN`] (`""`) when only one provider of a type is needed —
//! this is the canonical, unnamed registration that [`ResolveContext::resolve_external`]
//! queries without an explicit token.
//!
//! # Lookup Strategy
//!
//! When `ResolveContext::resolve_external_with_token::<T>(token)` is called:
//! 1. Check if `(TypeId<T>, token)` is in the registry
//! 2. If found, invoke its `DynProvider` closure
//! 3. If the token is the default and no DynProvider is found, fall back to
//!    `InjectableArcFactory` inventory entries (for Injectable types)
//! 4. Otherwise, return `MissingDependency` error

use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;

use crate::{DynProvider, InjectableError, InjectableResult, ResolveContext};

/// The default (unnamed) provider token.
///
/// Use this when registering or resolving a provider that does not need to be
/// differentiated from other providers of the same type.  Passing `""` is
/// identical.
///
/// ```rust,ignore
/// builder.register(DEFAULT_TOKEN, DynProvider::sync(|| Ok(Client::new())));
/// let client: Client = container.resolve_external_with_token(DEFAULT_TOKEN).await?;
/// // equivalent:
/// let client: Client = container.resolve_external::<Client>().await?;
/// ```
pub const DEFAULT_TOKEN: &str = "";

pub type ErasedProviderPinnedFuture<'a> = std::pin::Pin<
    Box<dyn std::future::Future<Output = InjectableResult<Box<dyn Any + Send>>> + Send + 'a>,
>;

/// A type-erased dynamic provider stored in the registry.
trait ErasedProvider: Send + Sync + 'static {
    fn provide_as_any(&self, ctx: Arc<ResolveContext>) -> ErasedProviderPinnedFuture<'_>;
}

impl<T: Send + Sync + 'static> ErasedProvider for DynProvider<T> {
    fn provide_as_any(&self, ctx: Arc<ResolveContext>) -> ErasedProviderPinnedFuture<'_> {
        Box::pin(async move {
            let value = self.provide(ctx).await?;
            Ok(Box::new(value) as Box<dyn Any + Send>)
        })
    }
}

/// Registry key: `(TypeId, token)`.
///
/// Two registrations are considered the same if and only if both the type
/// **and** the token match.  This enables multiple providers of the same type
/// (e.g., several `sqlx::Pool` instances for different databases) to coexist.
type RegistryKey = (TypeId, String);

/// A registry of dynamically-registered providers for external types.
///
/// Every registration is keyed by `(TypeId<T>, token)`.  Use [`DEFAULT_TOKEN`]
/// (`""`) when you only need one provider for a type; use distinct token strings
/// when you need multiple providers of the same type.
///
/// # Example
///
/// ```rust,ignore
/// let mut registry = ProviderRegistry::new();
///
/// // Unnamed (default) provider
/// registry.register("", DynProvider::sync(|| Ok(reqwest::Client::new())));
///
/// // Named providers — two pools of the same type
/// registry.register("primary", DynProvider::new(|| async { Ok(primary_pool()) }));
/// registry.register("replica", DynProvider::new(|| async { Ok(replica_pool()) }));
/// ```
pub struct ProviderRegistry {
    providers: HashMap<RegistryKey, Box<dyn ErasedProvider>>,
    /// `"TypeName[token]"` strings recorded when the same `(type, token)` pair is
    /// registered more than once, surfaced as errors at `ContainerBuilder::build` time.
    duplicates: Vec<String>,
}

impl ProviderRegistry {
    /// Create a new empty registry.
    pub fn new() -> Self {
        Self {
            providers: HashMap::new(),
            duplicates: Vec::new(),
        }
    }

    fn make_key<T: 'static>(token: &str) -> RegistryKey {
        (TypeId::of::<T>(), token.to_string())
    }

    fn duplicate_label<T: 'static>(token: &str) -> String {
        if token.is_empty() {
            std::any::type_name::<T>().to_string()
        } else {
            format!("{}[{}]", std::any::type_name::<T>(), token)
        }
    }

    /// Register a dynamic provider for type `T` under the given `token`.
    ///
    /// If the same `(type, token)` pair is registered more than once, the
    /// duplicate is recorded and surfaced as an error when
    /// `ContainerBuilder::build` is called.  Use
    /// [`register_or_replace`](Self::register_or_replace) if you intentionally
    /// want to override an existing registration.
    ///
    /// # Token conventions
    ///
    /// - Use [`DEFAULT_TOKEN`] (`""`) for the canonical, unnamed provider.
    /// - Use a descriptive string (`"primary"`, `"analytics"`) for named variants.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// // Default registration
    /// registry.register("", DynProvider::sync(|| Ok(reqwest::Client::new())));
    ///
    /// // Named registrations of the same type
    /// registry.register("primary",  DynProvider::new(|| async { Ok(primary_pool()) }));
    /// registry.register("replica",  DynProvider::new(|| async { Ok(replica_pool()) }));
    /// ```
    pub fn register<T: Send + Sync + 'static>(
        &mut self,
        token: impl Into<String>,
        provider: DynProvider<T>,
    ) {
        let token = token.into();
        let key = Self::make_key::<T>(&token);
        if self.providers.contains_key(&key) {
            self.duplicates.push(Self::duplicate_label::<T>(&token));
        }
        self.providers.insert(key, Box::new(provider));
    }

    /// Register a dynamic provider for type `T` under the given `token`,
    /// silently replacing any previously registered provider for the same
    /// `(type, token)` pair.
    ///
    /// Use this in tests or layered-config scenarios where intentional override
    /// is expected.
    pub fn register_or_replace<T: Send + Sync + 'static>(
        &mut self,
        token: impl Into<String>,
        provider: DynProvider<T>,
    ) {
        let key = (TypeId::of::<T>(), token.into());
        self.providers.insert(key, Box::new(provider));
    }

    /// Return duplicate `"TypeName[token]"` labels recorded so far.
    pub fn duplicates(&self) -> &[String] {
        &self.duplicates
    }

    /// Check if the registry has a provider for type `T` with the default token.
    ///
    /// Equivalent to `has_with_token::<T>(DEFAULT_TOKEN)`.
    pub fn has<T: 'static>(&self) -> bool {
        self.has_with_token::<T>(DEFAULT_TOKEN)
    }

    /// Check if the registry has a provider for type `T` with the given `token`.
    pub fn has_with_token<T: 'static>(&self, token: &str) -> bool {
        self.providers.contains_key(&Self::make_key::<T>(token))
    }

    /// Resolve a value of type `T` for the given `token`.
    ///
    /// Returns `None` if no provider is registered for `(T, token)`.
    /// Returns `Some(Err(..))` if the provider fails.
    ///
    /// For the default token (`""`), also falls back to `InjectableArcFactory`
    /// inventory entries so that `#[injectable]` types are resolvable via the
    /// external path even without an explicit `DynProvider` registration.
    pub(crate) async fn resolve_with_token<T: Send + Sync + 'static>(
        &self,
        token: &str,
        ctx: Arc<ResolveContext>,
    ) -> Option<InjectableResult<T>> {
        let key = Self::make_key::<T>(token);

        // 1. Check explicitly registered DynProvider<T> for this token
        if let Some(provider) = self.providers.get(&key) {
            let result = provider.provide_as_any(Arc::clone(&ctx)).await;
            return Some(
                result.and_then(|boxed| match boxed.downcast::<T>() {
                    Ok(t) => Ok(*t),
                    Err(_) => Err(InjectableError::ConstructionFailed {
                        type_name: std::any::type_name::<T>(),
                        reason: "downcast failed (this should never happen with correct TypeId)"
                            .to_string(),
                    }),
                }),
            );
        }

        // 2. For the default token only: fall back to InjectableArcFactory entries.
        //    These are submitted at compile time for every #[injectable] type.
        if token == DEFAULT_TOKEN {
            let target_id = TypeId::of::<T>();
            for factory in inventory::iter::<InjectableArcFactory>() {
                if factory.type_id() == target_id {
                    let result = factory.provide(ctx).await;
                    return Some(result.and_then(|boxed| match boxed.downcast::<T>() {
                        Ok(t) => Ok(*t),
                        Err(_) => Err(InjectableError::ConstructionFailed {
                            type_name: std::any::type_name::<T>(),
                            reason: "InjectableArcFactory downcast failed".to_string(),
                        }),
                    }));
                }
            }
        }

        None
    }

    /// Returns the total number of registered providers (across all tokens).
    pub fn len(&self) -> usize {
        self.providers.len()
    }

    /// Returns `true` if no providers are registered.
    pub fn is_empty(&self) -> bool {
        self.providers.is_empty()
    }
}

impl Default for ProviderRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for ProviderRegistry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProviderRegistry")
            .field("count", &self.providers.len())
            .finish()
    }
}

/// Type alias for the type-erased provide function pointer stored in
/// inventory-submitted [`InjectableArcFactory`] entries.
pub type InjectableProvideFnPtr = fn(
    std::sync::Arc<ResolveContext>,
) -> std::pin::Pin<
    Box<
        dyn std::future::Future<Output = InjectableResult<Box<dyn std::any::Any + Send>>>
            + Send
            + 'static,
    >,
>;

/// An entry submitted to the inventory by `#[injectable_impl]` and
/// `#[derive(Injectable)]` macros, allowing Injectable types to be resolved
/// via the same `try_resolve_external` path as DynProvider-registered types.
pub struct InjectableArcFactory {
    /// The type name as a `&'static str`, used for introspection and diagnostics.
    pub type_name: &'static str,
    type_id_fn: fn() -> std::any::TypeId,
    provide_fn: InjectableProvideFnPtr,
}

impl InjectableArcFactory {
    /// Create a new factory entry.
    pub const fn new_const(
        type_name: &'static str,
        type_id_fn: fn() -> std::any::TypeId,
        provide_fn: InjectableProvideFnPtr,
    ) -> Self {
        Self {
            type_name,
            type_id_fn,
            provide_fn,
        }
    }

    /// Return the `TypeId` of the Injectable type this entry was created for.
    pub fn type_id(&self) -> std::any::TypeId {
        (self.type_id_fn)()
    }

    /// Invoke the provider function and return a type-erased result.
    pub fn provide(&self, ctx: std::sync::Arc<ResolveContext>) -> ErasedProviderPinnedFuture<'_> {
        (self.provide_fn)(ctx)
    }
}

inventory::collect!(InjectableArcFactory);

// ─── InjectableHooksEntry ────────────────────────────────────────────────────

/// Function pointer that receives a type-erased `Arc<T>` and calls the
/// `#[injectable(post_construct)]` hook(s) on the instance.
pub type PostConstructFnPtr = fn(
    std::sync::Arc<dyn std::any::Any + std::marker::Send + std::marker::Sync>,
) -> std::pin::Pin<
    Box<dyn std::future::Future<Output = crate::HookResult> + std::marker::Send + 'static>,
>;

/// Function pointer that receives a type-erased `Arc<T>` and returns an
/// `Arc<dyn PreDestruct>` adapter suitable for registering with the context.
pub type MakePreDestructFnPtr = fn(
    std::sync::Arc<dyn std::any::Any + std::marker::Send + std::marker::Sync>,
) -> std::sync::Arc<dyn crate::PreDestruct>;

/// An inventory entry that carries lifecycle hook function pointers for one
/// Injectable type.
pub struct InjectableHooksEntry {
    type_id_fn: fn() -> std::any::TypeId,
    post_construct_fn: Option<PostConstructFnPtr>,
    make_pre_destruct_fn: Option<MakePreDestructFnPtr>,
}

impl InjectableHooksEntry {
    /// Create a new hooks entry.
    pub const fn new_const(
        type_id_fn: fn() -> std::any::TypeId,
        post_construct_fn: Option<PostConstructFnPtr>,
        make_pre_destruct_fn: Option<MakePreDestructFnPtr>,
    ) -> Self {
        Self {
            type_id_fn,
            post_construct_fn,
            make_pre_destruct_fn,
        }
    }

    /// `TypeId` of the Injectable type this entry belongs to.
    pub fn type_id(&self) -> std::any::TypeId {
        (self.type_id_fn)()
    }

    /// Returns the post-construct hook function, if any.
    pub fn post_construct_fn(&self) -> Option<PostConstructFnPtr> {
        self.post_construct_fn
    }

    /// Returns the pre-destruct adapter factory, if any.
    pub fn make_pre_destruct_fn(&self) -> Option<MakePreDestructFnPtr> {
        self.make_pre_destruct_fn
    }
}

inventory::collect!(InjectableHooksEntry);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DynProvider;

    #[test]
    fn new_registry_is_empty() {
        let r = ProviderRegistry::new();
        assert!(r.is_empty());
        assert_eq!(r.len(), 0);
    }

    #[test]
    fn has_returns_false_for_unregistered() {
        let r = ProviderRegistry::new();
        assert!(!r.has::<u32>());
        assert!(!r.has_with_token::<u32>("primary"));
    }

    #[test]
    fn has_returns_true_after_register_default() {
        let mut r = ProviderRegistry::new();
        r.register("", DynProvider::from_value(42u32));
        assert!(r.has::<u32>());
        assert!(r.has_with_token::<u32>(""));
        assert!(!r.has_with_token::<u32>("other"));
        assert_eq!(r.len(), 1);
        assert!(!r.is_empty());
    }

    #[test]
    fn has_returns_true_after_register_named() {
        let mut r = ProviderRegistry::new();
        r.register("primary", DynProvider::from_value(42u32));
        assert!(!r.has::<u32>(), "default token should be absent");
        assert!(r.has_with_token::<u32>("primary"));
        assert!(!r.has_with_token::<u32>("replica"));
    }

    #[test]
    fn multiple_tokens_same_type_coexist() {
        let mut r = ProviderRegistry::new();
        r.register("primary", DynProvider::from_value(1u32));
        r.register("replica", DynProvider::from_value(2u32));
        r.register("", DynProvider::from_value(0u32));
        assert_eq!(r.len(), 3);
        assert!(r.has::<u32>());
        assert!(r.has_with_token::<u32>("primary"));
        assert!(r.has_with_token::<u32>("replica"));
    }

    #[test]
    fn duplicate_same_token_is_recorded() {
        let mut r = ProviderRegistry::new();
        r.register("", DynProvider::from_value(1u32));
        r.register("", DynProvider::from_value(2u32));
        assert_eq!(r.len(), 1); // replaced, not added
        assert_eq!(r.duplicates().len(), 1);
    }

    #[test]
    fn duplicate_different_tokens_not_recorded() {
        let mut r = ProviderRegistry::new();
        r.register("primary", DynProvider::from_value(1u32));
        r.register("replica", DynProvider::from_value(2u32));
        assert_eq!(
            r.duplicates().len(),
            0,
            "different tokens are not duplicates"
        );
    }

    #[test]
    fn register_or_replace_does_not_record_duplicate() {
        let mut r = ProviderRegistry::new();
        r.register("", DynProvider::from_value(1u32));
        r.register_or_replace("", DynProvider::from_value(2u32));
        assert_eq!(r.duplicates().len(), 0);
    }

    #[test]
    fn debug_shows_count() {
        let mut r = ProviderRegistry::new();
        r.register("", DynProvider::from_value(0u8));
        let s = format!("{r:?}");
        assert!(s.contains("ProviderRegistry"));
        assert!(s.contains('1'));
    }

    #[test]
    fn default_creates_empty() {
        let r = ProviderRegistry::default();
        assert!(r.is_empty());
    }

    #[test]
    fn duplicate_label_includes_token_for_named() {
        let label = ProviderRegistry::duplicate_label::<u32>("primary");
        assert!(label.contains("primary"));
        assert!(label.contains("u32"));
    }

    #[test]
    fn duplicate_label_no_token_suffix_for_default() {
        let label = ProviderRegistry::duplicate_label::<u32>("");
        assert!(!label.contains('['), "default token adds no bracket suffix");
    }
}