effectful 0.3.0

Effect<A, E, R> (sync + async), context/layers, pipe — interpreter-style, no bundled executor
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Effect-style service layers for [`ServiceContext`](crate::ServiceContext).
//!
//! This is the v1 dependency-injection surface: a [`Layer`] is a lazy recipe that
//! builds one or more services, may fail with `E`, and may require upstream
//! services represented by `RIn`.

use std::cell::RefCell;
use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;

use crate::context::{Service, ServiceContext};
use crate::kernel::{BoxFuture, Effect, box_future};
use crate::layer::graph::{LayerDiagnostic, LayerGraph, LayerNode};

type LayerBuildFn<E> =
  dyn Fn(ServiceContext) -> BoxFuture<'static, Result<ServiceContext, E>> + 'static;

/// Trait for types that declare layer requirements as stable service keys.
///
/// Implemented for `()` (no requirements), single [`Service`] types, and common
/// tuples of services so that `Layer::effect` and `Layer::succeed` can produce
/// canonical planner metadata automatically.
pub trait LayerRequirements {
  /// Returns the stable service keys this type requires from upstream layers.
  fn layer_requirements() -> Vec<&'static str>;
}

impl LayerRequirements for () {
  fn layer_requirements() -> Vec<&'static str> {
    Vec::new()
  }
}

impl<S: Service> LayerRequirements for S {
  fn layer_requirements() -> Vec<&'static str> {
    vec![S::NAME]
  }
}

impl<A: Service, B: Service> LayerRequirements for (A, B) {
  fn layer_requirements() -> Vec<&'static str> {
    vec![A::NAME, B::NAME]
  }
}

impl<A: Service, B: Service, C: Service> LayerRequirements for (A, B, C) {
  fn layer_requirements() -> Vec<&'static str> {
    vec![A::NAME, B::NAME, C::NAME]
  }
}

/// A lazy service graph node.
///
/// `ROut` is the service output type carried in signatures, `E` is the layer
/// error channel, and `RIn` documents the services this layer expects to find in
/// its input [`ServiceContext`]. The runtime representation is a typed service
/// table so application code does not need HList paths.
pub struct Layer<ROut, E, RIn = ()>
where
  ROut: 'static,
  E: 'static,
  RIn: 'static,
{
  name: &'static str,
  build: Rc<LayerBuildFn<E>>,
  nodes: Vec<LayerNode>,
  _pd: PhantomData<fn(RIn) -> ROut>,
}

impl<ROut, E, RIn> Clone for Layer<ROut, E, RIn>
where
  ROut: 'static,
  E: 'static,
  RIn: 'static,
{
  fn clone(&self) -> Self {
    Self {
      name: self.name,
      build: Rc::clone(&self.build),
      nodes: self.nodes.clone(),
      _pd: PhantomData,
    }
  }
}

impl<ROut, E, RIn> fmt::Debug for Layer<ROut, E, RIn>
where
  ROut: 'static,
  E: 'static,
  RIn: 'static,
{
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("Layer").field("name", &self.name).finish()
  }
}

impl<ROut, E, RIn> Layer<ROut, E, RIn>
where
  ROut: 'static,
  E: 'static,
  RIn: 'static,
{
  /// Create a layer from a low-level build function.
  #[inline]
  pub fn from_context<F>(name: &'static str, build: F) -> Self
  where
    F: Fn(ServiceContext) -> BoxFuture<'static, Result<ServiceContext, E>> + 'static,
  {
    Self {
      name,
      build: Rc::new(build),
      nodes: Vec::new(),
      _pd: PhantomData,
    }
  }

  /// Private constructor that carries planner metadata.
  #[inline]
  fn from_context_with_nodes<F>(name: &'static str, build: F, nodes: Vec<LayerNode>) -> Self
  where
    F: Fn(ServiceContext) -> BoxFuture<'static, Result<ServiceContext, E>> + 'static,
  {
    Self {
      name,
      build: Rc::new(build),
      nodes,
      _pd: PhantomData,
    }
  }

  /// Human-readable name used in diagnostics.
  #[inline]
  pub fn name(&self) -> &'static str {
    self.name
  }

  /// Build this layer from an existing service context.
  #[inline]
  pub fn build_from(&self, context: ServiceContext) -> Effect<ServiceContext, E, ()> {
    let layer = self.clone();
    Effect::new_async(move |_env: &mut ()| layer.build_with(context))
  }

  /// Build this layer from an empty service context.
  #[inline]
  pub fn build(&self) -> Effect<ServiceContext, E, ()> {
    self.build_from(ServiceContext::empty())
  }

  /// Convert this layer into an effect that reads its input context from `R`.
  #[inline]
  pub fn to_effect(&self) -> Effect<ServiceContext, E, ServiceContext> {
    let layer = self.clone();
    Effect::new_async(move |context: &mut ServiceContext| layer.build_with(context.clone()))
  }

  /// Map the error channel.
  #[inline]
  pub fn map_error<E2, F>(self, f: F) -> Layer<ROut, E2, RIn>
  where
    E2: 'static,
    F: Fn(E) -> E2 + Clone + 'static,
  {
    let name = self.name;
    let nodes = self.nodes.clone();
    Layer::from_context_with_nodes(
      name,
      move |context| {
        let layer = self.clone();
        let f = f.clone();
        box_future(async move { layer.build_with(context).await.map_err(f) })
      },
      nodes,
    )
  }

  /// Memoize this layer's first result for subsequent builds of the same value.
  ///
  /// Use this for expensive layers that do not depend on changing input
  /// contexts. The cached value is cloned into each caller.
  #[inline]
  pub fn memoized(self) -> Self
  where
    E: Clone,
  {
    let cache: Rc<RefCell<Option<Result<ServiceContext, E>>>> = Rc::new(RefCell::new(None));
    let name = self.name;
    let nodes = self.nodes.clone();
    Self::from_context_with_nodes(
      name,
      move |context| {
        let layer = self.clone();
        let cache = Rc::clone(&cache);
        if let Some(cached) = cache.borrow().clone() {
          return box_future(async move { cached });
        }
        box_future(async move {
          let result = layer.build_with(context).await;
          *cache.borrow_mut() = Some(result.clone());
          result
        })
      },
      nodes,
    )
  }

  /// Merge this layer with another layer that uses the same input context.
  #[inline]
  pub fn merge<ROut2>(self, that: Layer<ROut2, E, RIn>) -> Layer<(ROut, ROut2), E, RIn>
  where
    ROut2: 'static,
  {
    let mut nodes = self.nodes.clone();
    nodes.extend(that.nodes.clone());
    Layer::from_context_with_nodes(
      "Layer.merge",
      move |context| {
        let left = self.clone();
        let right = that.clone();
        box_future(async move {
          let left_context = left.build_with(context.clone()).await?;
          let right_context = right.build_with(context).await?;
          Ok(left_context.merge(right_context))
        })
      },
      nodes,
    )
  }

  /// Provide this layer's requirements with another layer.
  ///
  /// The output contains only this layer's services; provider services are used
  /// to build `self` and are then hidden, matching Effect's `Layer.provide`.
  #[inline]
  pub fn provide<RProvider>(self, provider: Layer<RProvider, E, ()>) -> Layer<ROut, E, ()>
  where
    RProvider: 'static,
  {
    let mut nodes = self.nodes.clone();
    nodes.extend(provider.nodes.clone());
    Layer::from_context_with_nodes(
      "Layer.provide",
      move |context| {
        let layer = self.clone();
        let provider = provider.clone();
        box_future(async move {
          let provided = provider.build_with(context.clone()).await?;
          let full_context = context.merge(provided);
          layer.build_with(full_context).await
        })
      },
      nodes,
    )
  }

  /// Provide this layer's requirements and keep provider services in the output.
  #[inline]
  pub fn provide_merge<RProvider>(
    self,
    provider: Layer<RProvider, E, ()>,
  ) -> Layer<(ROut, RProvider), E, ()>
  where
    RProvider: 'static,
  {
    let mut nodes = self.nodes.clone();
    nodes.extend(provider.nodes.clone());
    Layer::from_context_with_nodes(
      "Layer.provide_merge",
      move |context| {
        let layer = self.clone();
        let provider = provider.clone();
        box_future(async move {
          let provided = provider.build_with(context.clone()).await?;
          let output = layer.build_with(context.merge(provided.clone())).await?;
          Ok(provided.merge(output))
        })
      },
      nodes,
    )
  }

  /// Returns planner diagnostics for this layer's dependency graph.
  ///
  /// Diagnostics are empty when the layer can be built without additional
  /// providers; otherwise they contain a single [`LayerDiagnostic`] from the
  /// canonical planner with stable service keys.
  #[inline]
  pub fn diagnostics(&self) -> Vec<LayerDiagnostic> {
    self.diagnostics_with_context(&ServiceContext::empty())
  }

  /// Returns planner diagnostics, treating services in `context` as available.
  #[inline]
  pub fn diagnostics_with_context(&self, context: &ServiceContext) -> Vec<LayerDiagnostic> {
    let mut nodes = self.nodes.clone();
    let already_provided: std::collections::HashSet<&str> = self
      .nodes
      .iter()
      .flat_map(|n| n.provides.iter().map(|s| s.as_str()))
      .collect();
    for name in context.service_names() {
      if !already_provided.contains(name) {
        nodes.push(LayerNode::new(name, Vec::<&str>::new(), [name]));
      }
    }
    LayerGraph::new(nodes).diagnostics()
  }

  pub(crate) fn build_with(
    &self,
    context: ServiceContext,
  ) -> BoxFuture<'static, Result<ServiceContext, E>> {
    (self.build)(context)
  }
}

impl<S, E> Layer<S, E, ()>
where
  S: Service,
  E: 'static,
{
  /// Construct an infallible layer from a concrete service value.
  #[inline]
  pub fn succeed(service: S) -> Self {
    let nodes = vec![LayerNode::new(S::NAME, Vec::<&str>::new(), [S::NAME])];
    Self::from_context_with_nodes(
      S::NAME,
      move |_context| {
        let service = service.clone();
        box_future(async move { Ok(ServiceContext::empty().add(service)) })
      },
      nodes,
    )
  }
}

impl<S, E, RIn> Layer<S, E, RIn>
where
  S: Service,
  E: 'static,
  RIn: LayerRequirements + 'static,
{
  /// Construct a layer from an effectful service constructor.
  ///
  /// The constructor receives dependencies through [`ServiceContext`], so it can
  /// use [`Effect::service`](crate::Effect::service) or `MyService::use_sync`.
  #[inline]
  pub fn effect<F>(name: &'static str, make: F) -> Self
  where
    F: Fn() -> Effect<S, E, ServiceContext> + 'static,
  {
    let nodes = vec![LayerNode::new(name, RIn::layer_requirements(), [S::NAME])];
    Self::from_context_with_nodes(
      name,
      move |mut context| {
        let effect = make();
        box_future(async move {
          let service = effect.run(&mut context).await?;
          Ok(ServiceContext::empty().add(service))
        })
      },
      nodes,
    )
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::{ContextService, MissingService, run_blocking};

  #[derive(Clone, Debug, PartialEq, effectful::Service)]
  struct Config {
    url: String,
  }

  #[derive(Clone, Debug, PartialEq, effectful::Service)]
  struct Database {
    url: String,
  }

  #[derive(Clone, Debug, PartialEq, effectful::Service)]
  struct Logger {
    level: String,
  }

  #[test]
  fn succeed_builds_service_context() {
    let layer = Layer::<Config, MissingService>::succeed(Config {
      url: "postgres://".to_string(),
    });

    let context = run_blocking(layer.build(), ()).expect("layer should build");
    assert_eq!(
      context.get::<Config>(),
      Some(&Config {
        url: "postgres://".to_string()
      })
    );
  }

  #[test]
  fn provide_satisfies_layer_dependencies() {
    let config = Layer::<Config, MissingService>::succeed(Config {
      url: "postgres://".to_string(),
    });
    let database = Layer::<Database, MissingService, Config>::effect("Database", || {
      Config::use_sync(|config| Database { url: config.url })
    });
    let program: Effect<String, MissingService, ServiceContext> =
      Database::use_sync(|database| database.url);

    let result = run_blocking(program.provide(database.provide(config)), ());

    assert_eq!(result, Ok("postgres://".to_string()));
  }

  #[test]
  fn merge_keeps_services_from_both_layers() {
    let config = Layer::<Config, MissingService>::succeed(Config {
      url: "postgres://".to_string(),
    });
    let logger = Layer::<Logger, MissingService>::succeed(Logger {
      level: "debug".to_string(),
    });

    let context = run_blocking(config.merge(logger).build(), ()).expect("layer should build");

    assert!(context.contains::<Config>());
    assert!(context.contains::<Logger>());
  }

  #[test]
  fn missing_dependency_fails_layer_build() {
    let database = Layer::<Database, MissingService, Config>::effect("Database", || {
      Config::use_sync(|config| Database { url: config.url })
    });

    let result = run_blocking(database.build(), ());

    assert!(matches!(
      result,
      Err(MissingService { name }) if name == Config::NAME
    ));
  }

  mod diagnostics {
    use super::*;

    #[test]
    fn diagnostics_when_effect_layer_missing_provider_returns_stable_service_key() {
      let database = Layer::<Database, MissingService, Config>::effect("Database", || {
        Config::use_sync(|config| Database { url: config.url })
      });

      let diagnostics = database.diagnostics();

      assert_eq!(diagnostics.len(), 1);
      assert_eq!(diagnostics[0].code, "missing-provider");
      assert!(diagnostics[0].message.contains(Config::NAME));
    }

    #[test]
    fn diagnostics_when_provider_supplies_requirement_returns_empty() {
      let config = Layer::<Config, MissingService>::succeed(Config {
        url: "postgres://".to_string(),
      });
      let database = Layer::<Database, MissingService, Config>::effect("Database", || {
        Config::use_sync(|config| Database { url: config.url })
      });

      let diagnostics = database.provide(config).diagnostics();

      assert!(diagnostics.is_empty());
    }

    #[test]
    fn diagnostics_with_context_when_context_supplies_requirement_returns_empty() {
      let database = Layer::<Database, MissingService, Config>::effect("Database", || {
        Config::use_sync(|config| Database { url: config.url })
      });
      let ctx = ServiceContext::empty().add(Config {
        url: "postgres://".to_string(),
      });

      let diagnostics = database.diagnostics_with_context(&ctx);

      assert!(diagnostics.is_empty());
    }

    #[test]
    fn diagnostics_merge_preserves_existing_build_behavior() {
      let config = Layer::<Config, MissingService>::succeed(Config {
        url: "postgres://".to_string(),
      });
      let logger = Layer::<Logger, MissingService>::succeed(Logger {
        level: "debug".to_string(),
      });

      let context = run_blocking(config.merge(logger).build(), ()).expect("layer should build");
      assert!(context.contains::<Config>());
      assert!(context.contains::<Logger>());
    }

    #[test]
    fn diagnostics_provide_preserves_existing_build_behavior() {
      let config = Layer::<Config, MissingService>::succeed(Config {
        url: "postgres://".to_string(),
      });
      let database = Layer::<Database, MissingService, Config>::effect("Database", || {
        Config::use_sync(|config| Database { url: config.url })
      });
      let program: Effect<String, MissingService, ServiceContext> =
        Database::use_sync(|database| database.url);

      let result = run_blocking(program.provide(database.provide(config)), ());
      assert_eq!(result, Ok("postgres://".to_string()));
    }

    #[test]
    fn diagnostics_provide_merge_preserves_existing_build_behavior() {
      let config = Layer::<Config, MissingService>::succeed(Config {
        url: "postgres://".to_string(),
      });
      let database = Layer::<Database, MissingService, Config>::effect("Database", || {
        Config::use_sync(|config| Database { url: config.url })
      });

      let merged = database.provide_merge(config);
      let diagnostics = merged.diagnostics();
      assert!(diagnostics.is_empty());
    }

    #[test]
    fn diagnostics_service_layer_preserves_existing_build_behavior() {
      let config = Config {
        url: "postgres://".to_string(),
      };
      let layer = config.layer();

      let context = run_blocking(layer.build(), ()).expect("layer should build");
      assert!(context.contains::<Config>());
    }

    #[test]
    fn diagnostics_succeed_preserves_existing_build_behavior() {
      let layer = Layer::<Config, MissingService>::succeed(Config {
        url: "postgres://".to_string(),
      });

      let context = run_blocking(layer.build(), ()).expect("layer should build");
      assert!(context.contains::<Config>());
    }

    #[test]
    fn diagnostics_with_context_when_service_already_provided_by_layer_returns_empty() {
      let layer = Layer::<Config, MissingService>::succeed(Config {
        url: "postgres://".to_string(),
      });
      let ctx = ServiceContext::empty().add(Config {
        url: "other://".to_string(),
      });

      let diagnostics = layer.diagnostics_with_context(&ctx);

      assert!(diagnostics.is_empty());
    }
  }
}