sayiir-core 1.0.0

Core types and traits for the Sayiir durable workflow engine
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
//! Dependency-injection container and integration trait.
//!
//! Two halves sit in this module:
//!
//! 1. The [`Deps`] type-keyed service container plus its [`DepsBuilder`] —
//!    register cloneable values once, resolve them by type from anywhere a
//!    `&Deps` is available.
//! 2. The [`DepsInjectable`] trait that ties [`crate::task::RegisterableTask`]
//!    to `Deps`. Implemented automatically by the `#[task]` proc-macro; used
//!    by [`crate::registry::TaskRegistry::register_from_deps`]
//!    and the `workflow! { deps: … }` expansion.
//!
//! # Quick Example
//!
//! ```
//! use sayiir_core::deps::Deps;
//! use std::sync::Arc;
//!
//! #[derive(Clone)]
//! struct HttpClient;
//!
//! let deps = Deps::builder()
//!     .insert(Arc::new(HttpClient))
//!     .build();
//!
//! let client: Arc<HttpClient> = deps.expect();
//! ```
//!
//! # Lookup Rules
//!
//! Resolution is by **exact `TypeId`**. Insert `Arc<HttpClient>` → resolve
//! `Arc<HttpClient>`. There is no coercion to traits or supertypes.
//!
//! Stored values must be `Send + Sync + 'static`, and `get` / `expect` /
//! `try_get` require `Clone` because the container owns one copy per type.

use std::any::{Any, TypeId, type_name};
use std::collections::HashMap;
use std::fmt;

use crate::task::RegisterableTask;

/// A type-keyed container of cloneable services.
///
/// Built with [`Deps::builder`] and read by type via [`Deps::get`], [`Deps::expect`],
/// or [`Deps::try_get`]. Used by `#[task]`-generated `from_deps` constructors.
#[derive(Default)]
pub struct Deps {
    map: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
}

impl Deps {
    /// Create an empty container.
    #[must_use]
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    /// Start a builder.
    #[must_use]
    pub fn builder() -> DepsBuilder {
        DepsBuilder { inner: Deps::new() }
    }

    /// Resolve `T`, returning a fresh clone, or `None` if the type was never inserted.
    #[must_use]
    pub fn get<T>(&self) -> Option<T>
    where
        T: Clone + Send + Sync + 'static,
    {
        self.map
            .get(&TypeId::of::<T>())
            .and_then(|v| v.downcast_ref::<T>())
            .cloned()
    }

    /// Resolve `T`, returning a fresh clone, or [`MissingDep`] describing the missing type.
    ///
    /// # Errors
    ///
    /// Returns [`MissingDep`] if no value of type `T` was inserted into the container.
    pub fn try_get<T>(&self) -> Result<T, MissingDep>
    where
        T: Clone + Send + Sync + 'static,
    {
        self.get::<T>().ok_or_else(MissingDep::of::<T>)
    }

    /// Resolve `T`, panicking with the type name on miss.
    ///
    /// Use [`Deps::try_get`] (or [`Deps::get`]) when a missing dependency should
    /// be a recoverable error. `expect` is meant for codegen sites that have
    /// *already* been verified by `verify_deps`.
    ///
    /// # Panics
    ///
    /// Panics if no value of type `T` was inserted into the container.
    #[must_use]
    pub fn expect<T>(&self) -> T
    where
        T: Clone + Send + Sync + 'static,
    {
        match self.get::<T>() {
            Some(v) => v,
            None => missing_panic(type_name::<T>()),
        }
    }

    /// Returns `true` if a value of type `T` is present.
    #[must_use]
    pub fn contains<T>(&self) -> bool
    where
        T: 'static,
    {
        self.map.contains_key(&TypeId::of::<T>())
    }

    /// Number of registered types.
    #[must_use]
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Whether no types are registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /// Move every entry from `other` into `self`. For any type present in both
    /// containers, the entry from `other` wins.
    ///
    /// Use this to layer service containers — e.g. start from a base
    /// container provided by a library, then merge in application-specific
    /// services before passing the result to `workflow! { deps: … }`.
    ///
    /// Merging does not retroactively affect tasks that were already
    /// constructed from this container (they hold their own clones).
    pub fn merge(&mut self, other: Self) {
        self.map.extend(other.map);
    }
}

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

/// Builder for [`Deps`]. Use [`Deps::builder`] to create one.
pub struct DepsBuilder {
    inner: Deps,
}

impl DepsBuilder {
    /// Insert (or replace) a value of type `T`.
    ///
    /// `T` is the key — if two `insert` calls share the same type, the later
    /// one wins.
    #[must_use]
    pub fn insert<T>(mut self, dep: T) -> Self
    where
        T: Clone + Send + Sync + 'static,
    {
        self.inner.map.insert(TypeId::of::<T>(), Box::new(dep));
        self
    }

    /// Merge every entry from `other` into the builder. For any type present
    /// in both, the entry from `other` wins.
    ///
    /// Useful for layering: start from a pre-built library container and
    /// extend it with application-specific services before calling `build`.
    #[must_use]
    pub fn merge(mut self, other: Deps) -> Self {
        self.inner.merge(other);
        self
    }

    /// Finalize and return the [`Deps`] container.
    #[must_use]
    pub fn build(self) -> Deps {
        self.inner
    }
}

impl Default for DepsBuilder {
    fn default() -> Self {
        Deps::builder()
    }
}

/// A dependency that was requested from a [`Deps`] container but not present.
///
/// `#[task]`-generated `verify_deps` returns a `Vec<MissingDep>`; the
/// `workflow!` macro converts those into build errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingDep {
    /// The `std::any::type_name` of the missing type.
    pub type_name: &'static str,
}

impl MissingDep {
    /// Build a `MissingDep` for type `T`.
    #[must_use]
    pub fn of<T: ?Sized + 'static>() -> Self {
        Self {
            type_name: type_name::<T>(),
        }
    }
}

impl fmt::Display for MissingDep {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "missing dependency `{}` in Deps container",
            self.type_name
        )
    }
}

impl std::error::Error for MissingDep {}

#[cold]
#[inline(never)]
#[allow(clippy::panic)]
fn missing_panic(type_name: &'static str) -> ! {
    panic!(
        "Deps::expect: missing dependency `{type_name}` (verify_deps should have caught this at workflow build time)"
    )
}

/// A [`RegisterableTask`] whose dependencies can be resolved from a [`Deps`]
/// container.
///
/// Implemented automatically by the `#[task]` proc-macro. Drives the
/// `workflow! { deps: … }` expansion and
/// [`TaskRegistry::register_from_deps`](crate::registry::TaskRegistry::register_from_deps)
/// when they need to construct task instances generically.
pub trait DepsInjectable: RegisterableTask
where
    Self::Input: Send + 'static,
    Self::Output: Send + 'static,
    Self::Future: Send + 'static,
{
    /// Build an instance by resolving every `#[inject]` parameter from
    /// `deps`. Panics on miss — call [`Self::verify_deps`] first when the
    /// container's contents are not statically known.
    fn from_deps(deps: &Deps) -> Self;

    /// Return one [`MissingDep`] per `#[inject]` type that is absent from
    /// `deps`. Empty slice means [`Self::from_deps`] will not panic.
    fn verify_deps(deps: &Deps) -> ::std::vec::Vec<MissingDep>;
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use std::sync::Arc;

    #[derive(Clone, Debug, PartialEq, Eq)]
    struct ServiceA(u32);

    #[derive(Clone, Debug, PartialEq, Eq)]
    struct ServiceB(&'static str);

    #[test]
    fn insert_and_get_concrete() {
        let deps = Deps::builder().insert(ServiceA(7)).build();
        assert_eq!(deps.get::<ServiceA>(), Some(ServiceA(7)));
    }

    #[test]
    fn insert_arc_keeps_arc_key() {
        let deps = Deps::builder().insert(Arc::new(ServiceA(7))).build();
        assert!(deps.contains::<Arc<ServiceA>>());
        assert!(!deps.contains::<ServiceA>());
        let resolved: Arc<ServiceA> = deps.expect();
        assert_eq!(*resolved, ServiceA(7));
    }

    #[test]
    fn multiple_types_coexist() {
        let deps = Deps::builder()
            .insert(ServiceA(1))
            .insert(ServiceB("hi"))
            .build();
        assert_eq!(deps.len(), 2);
        assert_eq!(deps.get::<ServiceA>(), Some(ServiceA(1)));
        assert_eq!(deps.get::<ServiceB>(), Some(ServiceB("hi")));
    }

    #[test]
    fn missing_type_returns_none() {
        let deps = Deps::new();
        assert!(deps.get::<ServiceA>().is_none());
    }

    #[test]
    fn try_get_reports_type_name() {
        let deps = Deps::new();
        let err = deps.try_get::<ServiceA>().unwrap_err();
        assert!(err.type_name.contains("ServiceA"));
    }

    #[test]
    fn last_insert_wins_for_same_type() {
        let deps = Deps::builder()
            .insert(ServiceA(1))
            .insert(ServiceA(2))
            .build();
        assert_eq!(deps.get::<ServiceA>(), Some(ServiceA(2)));
    }

    #[test]
    fn expect_returns_value() {
        let deps = Deps::builder().insert(ServiceA(42)).build();
        let value: ServiceA = deps.expect();
        assert_eq!(value, ServiceA(42));
    }

    #[test]
    #[should_panic(expected = "missing dependency")]
    fn expect_panics_with_message() {
        let deps = Deps::new();
        let _: ServiceA = deps.expect();
    }

    #[test]
    fn missing_dep_display() {
        let m = MissingDep::of::<ServiceA>();
        let rendered = format!("{m}");
        assert!(rendered.contains("ServiceA"));
        assert!(rendered.contains("missing dependency"));
    }

    #[test]
    fn empty_and_len() {
        let mut deps = Deps::new();
        assert!(deps.is_empty());
        deps = Deps::builder().insert(ServiceA(0)).build();
        assert!(!deps.is_empty());
        assert_eq!(deps.len(), 1);
    }

    #[test]
    fn merge_non_overlapping() {
        let mut base = Deps::builder().insert(ServiceA(1)).build();
        let extra = Deps::builder().insert(ServiceB("x")).build();
        base.merge(extra);

        assert_eq!(base.len(), 2);
        assert_eq!(base.get::<ServiceA>(), Some(ServiceA(1)));
        assert_eq!(base.get::<ServiceB>(), Some(ServiceB("x")));
    }

    #[test]
    fn merge_overlap_other_wins() {
        let mut base = Deps::builder().insert(ServiceA(1)).build();
        let extra = Deps::builder().insert(ServiceA(99)).build();
        base.merge(extra);

        assert_eq!(base.len(), 1);
        assert_eq!(base.get::<ServiceA>(), Some(ServiceA(99)));
    }

    #[test]
    fn merge_empty_into_populated() {
        let mut base = Deps::builder().insert(ServiceA(1)).build();
        base.merge(Deps::new());
        assert_eq!(base.len(), 1);
        assert_eq!(base.get::<ServiceA>(), Some(ServiceA(1)));
    }

    #[test]
    fn merge_populated_into_empty() {
        let mut base = Deps::new();
        let extra = Deps::builder().insert(ServiceA(7)).build();
        base.merge(extra);
        assert_eq!(base.len(), 1);
        assert_eq!(base.get::<ServiceA>(), Some(ServiceA(7)));
    }

    #[test]
    fn builder_merge_layers_containers() {
        let library = Deps::builder().insert(ServiceA(1)).build();
        let combined = Deps::builder()
            .insert(ServiceB("local"))
            .merge(library)
            .build();

        assert_eq!(combined.len(), 2);
        assert_eq!(combined.get::<ServiceA>(), Some(ServiceA(1)));
        assert_eq!(combined.get::<ServiceB>(), Some(ServiceB("local")));
    }

    #[test]
    fn builder_merge_other_wins_on_overlap() {
        let library = Deps::builder().insert(ServiceA(2)).build();
        let combined = Deps::builder().insert(ServiceA(1)).merge(library).build();

        assert_eq!(combined.get::<ServiceA>(), Some(ServiceA(2)));
    }
}