cf-gears-toolkit 0.10.0

Core ToolKit library
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Minimalistic, type-safe `ClientHub`.
//!
//! Design goals:
//! - Providers register an implementation once (local or remote).
//! - Consumers fetch by *interface type* (trait object): `get::<dyn my::Api>()`.
//! - For plugin-like scenarios, multiple implementations of the same interface can coexist
//!   under different scopes (e.g. selected by GTS instance ID).
//!
//! Implementation details:
//! - Key = type name. We use `type_name::<T>()`, which works for `T = dyn Trait`.
//! - Value = `Arc<T>` stored as `Box<dyn Any + Send + Sync>` (downcast on read).
//! - Sync hot path: `get()` is non-async; no hidden per-entry cells or lazy slots.
//!
//! Notes:
//! - Re-registering overwrites the previous value atomically; existing Arcs held by consumers remain valid.
//! - For testing, just register a mock under the same trait type.

use parking_lot::RwLock;
use std::{any::Any, collections::HashMap, fmt, sync::Arc};

/// Stable type key for trait objects — uses fully-qualified `type_name::<T>()`.
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct TypeKey(&'static str);

impl TypeKey {
    #[inline]
    fn of<T: ?Sized + 'static>() -> Self {
        TypeKey(std::any::type_name::<T>())
    }
}

impl fmt::Debug for TypeKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.0)
    }
}

/// A scope for resolving multiple implementations of the same interface type.
///
/// This is intentionally opaque: the scope semantics are defined by the caller.
/// One common scope is a full GTS entity/instance ID.
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct ClientScope(Arc<str>);

impl ClientScope {
    /// Create a new scope from an arbitrary string.
    #[inline]
    #[must_use]
    pub fn new(scope: impl Into<Arc<str>>) -> Self {
        Self(scope.into())
    }

    /// Create a scope derived from a GTS identifier.
    ///
    /// Internally we prefix the scope to avoid accidental collisions with other scope kinds.
    #[must_use]
    pub fn gts_id(gts_id: &str) -> Self {
        let mut s = String::with_capacity("gts:".len() + gts_id.len());
        s.push_str("gts:");
        s.push_str(gts_id);
        Self(Arc::<str>::from(s))
    }

    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Debug for ClientScope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

#[derive(Clone, Eq, PartialEq, Hash)]
struct ScopedKey {
    type_key: TypeKey,
    scope: ClientScope,
}

impl fmt::Debug for ScopedKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ScopedKey")
            .field("type_key", &self.type_key)
            .field("scope", &self.scope)
            .finish()
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ClientHubError {
    #[error("client not found: type={type_key:?}")]
    NotFound { type_key: TypeKey },

    #[error("type mismatch in hub for type={type_key:?}")]
    TypeMismatch { type_key: TypeKey },

    #[error("scoped client not found: type={type_key:?} scope={scope:?}")]
    ScopedNotFound {
        type_key: TypeKey,
        scope: ClientScope,
    },

    #[error("type mismatch in hub for type={type_key:?} scope={scope:?}")]
    ScopedTypeMismatch {
        type_key: TypeKey,
        scope: ClientScope,
    },
}

type Boxed = Box<dyn Any + Send + Sync>;

/// Internal map type for the client hub.
type ClientMap = HashMap<TypeKey, Boxed>;

/// Internal map type for the scoped client hub.
type ScopedClientMap = HashMap<ScopedKey, Boxed>;

/// Type-safe registry of clients keyed by interface type.
#[derive(Default)]
pub struct ClientHub {
    map: RwLock<ClientMap>,
    scoped_map: RwLock<ScopedClientMap>,
    /// Types registered by consumer wiring as a discovery-resolving remote
    /// proxy rather than a genuine in-process implementation.
    ///
    /// The hub is keyed by type alone, so a proxy and a local impl are
    /// indistinguishable once stored. Consumer wiring needs to tell them apart:
    /// finding a *proxy* under a contract does not mean the dependency is
    /// satisfied locally, and treating it as such marks the dependency
    /// readiness-resolved while the provider is still absent.
    remote_proxies: RwLock<std::collections::HashSet<TypeKey>>,
}

impl ClientHub {
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            map: RwLock::new(HashMap::new()),
            scoped_map: RwLock::new(HashMap::new()),
            remote_proxies: RwLock::new(std::collections::HashSet::new()),
        }
    }
}

impl ClientHub {
    /// Register a client under the interface type `T`.
    /// `T` can be a trait object like `dyn my_gear::api::MyClient`.
    pub fn register<T>(&self, client: Arc<T>)
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let type_key = TypeKey::of::<T>();
        let mut w = self.map.write();
        w.insert(type_key, Box::new(client));
    }

    /// Register a scoped client under the interface type `T`.
    ///
    /// This enables multiple implementations of the same interface to coexist,
    /// distinguished by a caller-defined `ClientScope` (e.g., a GTS instance ID).
    pub fn register_scoped<T>(&self, scope: ClientScope, client: Arc<T>)
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let key = ScopedKey {
            type_key: TypeKey::of::<T>(),
            scope,
        };
        let mut w = self.scoped_map.write();
        w.insert(key, Box::new(client));
    }

    /// Fetch a client by interface type `T`.
    ///
    /// # Errors
    /// Returns `ClientHubError::NotFound` if no client is registered for the type.
    /// Returns `ClientHubError::TypeMismatch` if the stored type doesn't match.
    pub fn get<T>(&self) -> Result<Arc<T>, ClientHubError>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let type_key = TypeKey::of::<T>();
        let r = self.map.read();

        let boxed = r.get(&type_key).ok_or(ClientHubError::NotFound {
            type_key: type_key.clone(),
        })?;

        // Stored value is exactly `Arc<T>`; downcast is safe and cheap.
        if let Some(arc_t) = boxed.downcast_ref::<Arc<T>>() {
            return Ok(arc_t.clone());
        }
        Err(ClientHubError::TypeMismatch { type_key })
    }

    /// Try to fetch a client by interface type `T`.
    ///
    /// Returns `None` if no client is registered for the type or if the stored
    /// type doesn't match. This sees every registration, proxies included — use
    /// [`Self::try_get_local`] when the answer needs to mean "satisfied
    /// in-process".
    pub fn try_get<T>(&self) -> Option<Arc<T>>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let type_key = TypeKey::of::<T>();
        let r = self.map.read();
        let boxed = r.get(&type_key)?;
        boxed.downcast_ref::<Arc<T>>().cloned()
    }

    /// Register a client that is a discovery-resolving proxy to a *remote*
    /// provider, not an in-process implementation.
    ///
    /// Stored exactly like [`Self::register`]; the difference is that the type
    /// is remembered as remote so [`Self::try_get_local`] will not report it as
    /// a local implementation.
    pub fn register_remote_proxy<T>(&self, client: Arc<T>)
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let type_key = TypeKey::of::<T>();
        self.remote_proxies.write().insert(type_key.clone());
        self.map.write().insert(type_key, Box::new(client));
    }

    /// Try to fetch a client by interface type `T`, ignoring remote proxies.
    ///
    /// This is the short-circuit probe for consumer wiring: a compile-time
    /// (local) registration takes priority over a discovery-based proxy, so the
    /// wiring closure asks whether a *local* implementation exists before
    /// registering one of its own.
    ///
    /// Using plain [`Self::try_get`] here would be wrong when two consumers in
    /// one process depend on the same contract: whichever wires second would
    /// find the first one's proxy, conclude the dependency is local, and mark
    /// it readiness-resolved — turning `/readyz` green before the provider
    /// exists.
    pub fn try_get_local<T>(&self) -> Option<Arc<T>>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let type_key = TypeKey::of::<T>();
        if self.remote_proxies.read().contains(&type_key) {
            return None;
        }
        let r = self.map.read();
        let boxed = r.get(&type_key)?;
        boxed.downcast_ref::<Arc<T>>().cloned()
    }

    /// Whether a remote proxy is already registered for interface type `T`.
    ///
    /// Lets consumer wiring reuse an existing proxy instead of building and
    /// registering a second identical one for the same contract.
    #[must_use]
    pub fn has_remote_proxy<T>(&self) -> bool
    where
        T: ?Sized + Send + Sync + 'static,
    {
        self.remote_proxies.read().contains(&TypeKey::of::<T>())
    }

    /// Fetch a scoped client by interface type `T` and scope.
    ///
    /// # Errors
    /// Returns `ClientHubError::ScopedNotFound` if no client is registered for the `(type, scope)` pair.
    /// Returns `ClientHubError::ScopedTypeMismatch` if the stored type doesn't match.
    pub fn get_scoped<T>(&self, scope: &ClientScope) -> Result<Arc<T>, ClientHubError>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let key = ScopedKey {
            type_key: TypeKey::of::<T>(),
            scope: scope.clone(),
        };
        let r = self.scoped_map.read();

        let boxed = r.get(&key).ok_or_else(|| ClientHubError::ScopedNotFound {
            type_key: key.type_key.clone(),
            scope: key.scope.clone(),
        })?;

        if let Some(arc_t) = boxed.downcast_ref::<Arc<T>>() {
            return Ok(arc_t.clone());
        }
        Err(ClientHubError::ScopedTypeMismatch {
            type_key: key.type_key,
            scope: key.scope,
        })
    }

    /// Try to fetch a scoped client by interface type `T` and scope.
    ///
    /// Returns `None` if not found or if the stored type doesn't match.
    pub fn try_get_scoped<T>(&self, scope: &ClientScope) -> Option<Arc<T>>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let key = ScopedKey {
            type_key: TypeKey::of::<T>(),
            scope: scope.clone(),
        };
        let r = self.scoped_map.read();
        let boxed = r.get(&key)?;

        boxed.downcast_ref::<Arc<T>>().cloned()
    }

    /// Remove a client by interface type; returns the removed client if it was present.
    pub fn remove<T>(&self) -> Option<Arc<T>>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let type_key = TypeKey::of::<T>();
        let mut w = self.map.write();
        let boxed = w.remove(&type_key)?;
        boxed.downcast::<Arc<T>>().ok().map(|b| *b)
    }

    /// Remove a scoped client by interface type + scope; returns the removed client if it was present.
    pub fn remove_scoped<T>(&self, scope: &ClientScope) -> Option<Arc<T>>
    where
        T: ?Sized + Send + Sync + 'static,
    {
        let key = ScopedKey {
            type_key: TypeKey::of::<T>(),
            scope: scope.clone(),
        };
        let mut w = self.scoped_map.write();
        let boxed = w.remove(&key)?;
        boxed.downcast::<Arc<T>>().ok().map(|b| *b)
    }

    /// Clear everything (useful in tests).
    pub fn clear(&self) {
        self.map.write().clear();
        self.scoped_map.write().clear();
    }

    /// Introspection: (total entries).
    pub fn len(&self) -> usize {
        self.map.read().len() + self.scoped_map.read().len()
    }

    /// Check if the hub is empty.
    pub fn is_empty(&self) -> bool {
        self.map.read().is_empty() && self.scoped_map.read().is_empty()
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use toolkit_gts::gts_id;

    #[async_trait::async_trait]
    trait TestApi: Send + Sync {
        async fn id(&self) -> usize;
    }

    struct ImplA(usize);
    #[async_trait::async_trait]
    impl TestApi for ImplA {
        async fn id(&self) -> usize {
            self.0
        }
    }

    #[tokio::test]
    async fn register_and_get_dyn_trait() {
        let hub = ClientHub::new();
        let api: Arc<dyn TestApi> = Arc::new(ImplA(7));
        hub.register::<dyn TestApi>(api.clone());

        let got = hub.get::<dyn TestApi>().unwrap();
        assert_eq!(got.id().await, 7);
        assert_eq!(Arc::as_ptr(&api), Arc::as_ptr(&got));
    }

    #[tokio::test]
    async fn remove_works() {
        let hub = ClientHub::new();
        let api: Arc<dyn TestApi> = Arc::new(ImplA(42));
        hub.register::<dyn TestApi>(api);

        assert!(hub.get::<dyn TestApi>().is_ok());

        let removed = hub.remove::<dyn TestApi>();
        assert!(removed.is_some());
        assert!(hub.get::<dyn TestApi>().is_err());
    }

    #[tokio::test]
    async fn try_get_local_ignores_a_remote_proxy() {
        let hub = ClientHub::new();
        let proxy: Arc<dyn TestApi> = Arc::new(ImplA(1));
        hub.register_remote_proxy::<dyn TestApi>(proxy);

        // Reachable for business calls...
        assert!(hub.try_get::<dyn TestApi>().is_some());
        assert!(hub.get::<dyn TestApi>().is_ok());
        // ...but not a local implementation. This is what stops a second
        // consumer of the same contract from reporting `WireOutcome::Local`
        // and marking the dependency readiness-resolved while the provider is
        // still absent.
        assert!(hub.try_get_local::<dyn TestApi>().is_none());
        assert!(hub.has_remote_proxy::<dyn TestApi>());
    }

    #[tokio::test]
    async fn try_get_local_sees_a_plain_registration() {
        let hub = ClientHub::new();
        let local: Arc<dyn TestApi> = Arc::new(ImplA(2));
        hub.register::<dyn TestApi>(local);

        assert!(hub.try_get_local::<dyn TestApi>().is_some());
        assert!(!hub.has_remote_proxy::<dyn TestApi>());
    }

    #[tokio::test]
    async fn overwrite_replaces_atomically() {
        let hub = ClientHub::new();
        hub.register::<dyn TestApi>(Arc::new(ImplA(1)));

        let old = hub.get::<dyn TestApi>().unwrap();
        assert_eq!(old.id().await, 1);

        hub.register::<dyn TestApi>(Arc::new(ImplA(2)));

        let new = hub.get::<dyn TestApi>().unwrap();
        assert_eq!(new.id().await, 2);

        // Old Arc is still valid
        assert_eq!(old.id().await, 1);
    }

    #[tokio::test]
    async fn scoped_register_and_get_dyn_trait() {
        let hub = ClientHub::new();
        let scope_a = ClientScope::gts_id(gts_id!(
            "cf.core.toolkit.plugins.v1~cf.core.tenant_resolver.plugin.v1~contoso.app._.plugin.v1.0"
        ));
        let scope_b = ClientScope::gts_id(gts_id!(
            "cf.core.toolkit.plugins.v1~cf.core.tenant_resolver.plugin.v1~fabrikam.app._.plugin.v1.0"
        ));

        let api_a: Arc<dyn TestApi> = Arc::new(ImplA(1));
        let api_b: Arc<dyn TestApi> = Arc::new(ImplA(2));

        hub.register_scoped::<dyn TestApi>(scope_a.clone(), api_a.clone());
        hub.register_scoped::<dyn TestApi>(scope_b.clone(), api_b.clone());

        assert_eq!(
            hub.get_scoped::<dyn TestApi>(&scope_a).unwrap().id().await,
            1
        );
        assert_eq!(
            hub.get_scoped::<dyn TestApi>(&scope_b).unwrap().id().await,
            2
        );
    }

    #[test]
    fn scoped_get_is_independent_from_global_get() {
        let hub = ClientHub::new();
        let scope = ClientScope::gts_id(gts_id!(
            "cf.core.toolkit.plugins.v1~cf.core.tenant_resolver.plugin.v1~fabrikam.app._.plugin.v1.0"
        ));
        hub.register::<str>(Arc::from("global"));
        hub.register_scoped::<str>(scope.clone(), Arc::from("scoped"));

        assert_eq!(&*hub.get::<str>().unwrap(), "global");
        assert_eq!(&*hub.get_scoped::<str>(&scope).unwrap(), "scoped");
    }

    #[test]
    fn try_get_scoped_returns_some_on_hit() {
        let hub = ClientHub::new();
        let scope = ClientScope::gts_id(gts_id!(
            "cf.core.toolkit.plugins.v1~cf.core.tenant_resolver.plugin.v1~contoso.app._.plugin.v1.0"
        ));
        hub.register_scoped::<str>(scope.clone(), Arc::from("scoped"));

        let got = hub.try_get_scoped::<str>(&scope);
        assert_eq!(got.as_deref(), Some("scoped"));
    }

    #[test]
    fn try_get_scoped_returns_none_on_miss() {
        let hub = ClientHub::new();
        let scope = ClientScope::gts_id(gts_id!(
            "cf.core.toolkit.plugins.v1~cf.core.tenant_resolver.plugin.v1~fabrikam.app._.plugin.v1.0"
        ));

        let got = hub.try_get_scoped::<str>(&scope);
        assert!(got.is_none());
    }
}