a3s-boot 0.1.3

Adapter-first modular Rust web framework for A3S inspired by Nest.js
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use super::{
    ExecutionTransportGuard, ExecutionTransportInterceptor, IntoTransportReply, MessagePatternKind,
    TransportContext, TransportGuard, TransportInterceptor, TransportMessage, TransportPipe,
    TransportReply,
};
use crate::pipeline::{PipelineComponent, PipelineOverrides, ProviderEnhancerComponents};
use crate::{
    catch_errors, validate_json_value_with_options, BootError, BootErrorKind, BootRequest,
    BoxFuture, CallHandler, ContextId, ContextIdFactory, ExecutionInterceptor, Guard, HttpMethod,
    ModuleRef, Result, TransportExceptionFilter, Validate, ValidationOptions, ValidationSchema,
};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::Value;
use std::collections::BTreeMap;
use std::future::Future;
use std::sync::{Arc, Mutex};

type TransportHandlerFuture = BoxFuture<'static, Result<Option<TransportReply>>>;
type MessageValidator =
    Arc<dyn Fn(TransportMessage, ValidationOptions) -> Result<TransportMessage> + Send + Sync>;

trait TransportMessageHandler: Send + Sync + 'static {
    fn call(&self, message: TransportMessage) -> TransportHandlerFuture;
}

struct TransportHandlerAdapter<H> {
    handler: H,
}

impl<H, Fut, R> TransportMessageHandler for TransportHandlerAdapter<H>
where
    H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<R>> + Send + 'static,
    R: IntoTransportReply + Send + 'static,
{
    fn call(&self, message: TransportMessage) -> TransportHandlerFuture {
        let future = (self.handler)(message);
        Box::pin(async move { Ok(future.await?.into_transport_reply()) })
    }
}

type ScopedTransportHandlerFactory =
    dyn Fn(&ModuleRef) -> Result<Arc<dyn TransportMessageHandler>> + Send + Sync;

#[derive(Clone)]
enum TransportHandlerDefinition {
    Static(Arc<dyn TransportMessageHandler>),
    Scoped(Arc<ScopedTransportHandlerFactory>),
}

impl TransportHandlerDefinition {
    fn resolve(
        &self,
        module_ref: Option<&ModuleRef>,
        pattern: &str,
    ) -> Result<Arc<dyn TransportMessageHandler>> {
        match self {
            Self::Static(handler) => Ok(Arc::clone(handler)),
            Self::Scoped(factory) => {
                let module_ref = module_ref.ok_or_else(|| {
                    BootError::Internal(format!(
                        "scoped transport message pattern `{pattern}` requires a declaring or default module context"
                    ))
                })?;
                factory(module_ref)
            }
        }
    }

    fn is_scoped(&self) -> bool {
        matches!(self, Self::Scoped(_))
    }
}

#[derive(Default)]
struct DispatchHandlerCache {
    handler: Mutex<Option<Arc<dyn TransportMessageHandler>>>,
}

impl DispatchHandlerCache {
    fn resolve(
        &self,
        definition: &TransportHandlerDefinition,
        module_ref: Option<&ModuleRef>,
        pattern: &str,
    ) -> Result<Arc<dyn TransportMessageHandler>> {
        let mut cached = self
            .handler
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if let Some(handler) = cached.as_ref() {
            return Ok(Arc::clone(handler));
        }

        let handler = definition.resolve(module_ref, pattern)?;
        *cached = Some(Arc::clone(&handler));
        Ok(handler)
    }
}

#[derive(Clone)]
struct MessageDispatchState {
    context_id: ContextId,
    module_ref: Option<ModuleRef>,
    handler: Arc<DispatchHandlerCache>,
}

impl MessageDispatchState {
    fn new(module_ref: Option<&ModuleRef>) -> Self {
        let context_id = ContextIdFactory::create();
        let module_ref = module_ref.map(|module_ref| module_ref.context_scope(&context_id));
        Self {
            context_id,
            module_ref,
            handler: Arc::new(DispatchHandlerCache::default()),
        }
    }

    fn request(&self) -> BootRequest {
        let request = BootRequest::new(HttpMethod::Post, "/__transport");
        match &self.module_ref {
            Some(module_ref) => request.with_module_ref(module_ref.clone()),
            None => request,
        }
    }
}

/// Framework-neutral message pattern handler definition.
#[derive(Clone)]
pub struct MessagePatternDefinition {
    pattern: String,
    kind: MessagePatternKind,
    handler: TransportHandlerDefinition,
    pipes: Vec<PipelineComponent<dyn TransportPipe>>,
    guards: Vec<PipelineComponent<dyn TransportGuard>>,
    interceptors: Vec<PipelineComponent<dyn TransportInterceptor>>,
    filters: Vec<PipelineComponent<dyn TransportExceptionFilter>>,
    validators: Vec<MessageValidator>,
    validation_enabled: bool,
    validation_disabled: bool,
    validation_options: ValidationOptions,
    metadata: BTreeMap<String, Value>,
    module_name: Option<String>,
    module_ref: Option<ModuleRef>,
}

impl MessagePatternDefinition {
    pub fn request<H, Fut, R>(pattern: impl Into<String>, handler: H) -> Result<Self>
    where
        H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R>> + Send + 'static,
        R: IntoTransportReply + Send + 'static,
    {
        Self::new(pattern, MessagePatternKind::RequestResponse, handler)
    }

    /// Build a request-response pattern whose handler is created from the
    /// current message dispatch's dependency-injection scope.
    pub fn request_scoped<F, H, Fut, R>(pattern: impl Into<String>, factory: F) -> Result<Self>
    where
        F: Fn(&ModuleRef) -> Result<H> + Send + Sync + 'static,
        H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R>> + Send + 'static,
        R: IntoTransportReply + Send + 'static,
    {
        Self::new_scoped(pattern, MessagePatternKind::RequestResponse, factory)
    }

    pub fn event<H, Fut>(pattern: impl Into<String>, handler: H) -> Result<Self>
    where
        H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        Self::new(pattern, MessagePatternKind::Event, handler)
    }

    /// Build an event pattern whose handler is created from the current
    /// message dispatch's dependency-injection scope.
    pub fn event_scoped<F, H, Fut>(pattern: impl Into<String>, factory: F) -> Result<Self>
    where
        F: Fn(&ModuleRef) -> Result<H> + Send + Sync + 'static,
        H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        Self::new_scoped(pattern, MessagePatternKind::Event, factory)
    }

    pub fn request_json<T, H, Fut, R>(pattern: impl Into<String>, handler: H) -> Result<Self>
    where
        T: DeserializeOwned + Send + 'static,
        H: Fn(T) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R>> + Send + 'static,
        R: Serialize + Send + 'static,
    {
        Self::request(pattern, move |message: TransportMessage| {
            let payload = message.data_as::<T>();
            let future = payload.map(&handler);
            async move {
                let response = future?.await?;
                TransportReply::json(&response)
            }
        })
    }

    pub fn request_validated_json<T, H, Fut, R>(
        pattern: impl Into<String>,
        handler: H,
    ) -> Result<Self>
    where
        T: DeserializeOwned + Validate + Send + 'static,
        H: Fn(T) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R>> + Send + 'static,
        R: Serialize + Send + 'static,
    {
        Self::request(pattern, move |message: TransportMessage| {
            let payload = message.validated_data::<T>();
            let future = payload.map(&handler);
            async move {
                let response = future?.await?;
                TransportReply::json(&response)
            }
        })
    }

    pub fn event_json<T, H, Fut>(pattern: impl Into<String>, handler: H) -> Result<Self>
    where
        T: DeserializeOwned + Send + 'static,
        H: Fn(T) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        Self::event(pattern, move |message: TransportMessage| {
            let payload = message.data_as::<T>();
            let future = payload.map(&handler);
            async move { future?.await }
        })
    }

    pub fn event_validated_json<T, H, Fut>(pattern: impl Into<String>, handler: H) -> Result<Self>
    where
        T: DeserializeOwned + Validate + Send + 'static,
        H: Fn(T) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        Self::event(pattern, move |message: TransportMessage| {
            let payload = message.validated_data::<T>();
            let future = payload.map(&handler);
            async move { future?.await }
        })
    }

    fn new<H, Fut, R>(
        pattern: impl Into<String>,
        kind: MessagePatternKind,
        handler: H,
    ) -> Result<Self>
    where
        H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R>> + Send + 'static,
        R: IntoTransportReply + Send + 'static,
    {
        Self::from_handler(
            pattern,
            kind,
            TransportHandlerDefinition::Static(Arc::new(TransportHandlerAdapter { handler })),
        )
    }

    fn new_scoped<F, H, Fut, R>(
        pattern: impl Into<String>,
        kind: MessagePatternKind,
        factory: F,
    ) -> Result<Self>
    where
        F: Fn(&ModuleRef) -> Result<H> + Send + Sync + 'static,
        H: Fn(TransportMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<R>> + Send + 'static,
        R: IntoTransportReply + Send + 'static,
    {
        let factory = move |module_ref: &ModuleRef| {
            let handler = factory(module_ref)?;
            Ok(Arc::new(TransportHandlerAdapter { handler }) as Arc<dyn TransportMessageHandler>)
        };
        Self::from_handler(
            pattern,
            kind,
            TransportHandlerDefinition::Scoped(Arc::new(factory)),
        )
    }

    fn from_handler(
        pattern: impl Into<String>,
        kind: MessagePatternKind,
        handler: TransportHandlerDefinition,
    ) -> Result<Self> {
        let pattern = pattern.into();
        validate_pattern(&pattern)?;
        Ok(Self {
            pattern,
            kind,
            handler,
            pipes: Vec::new(),
            guards: Vec::new(),
            interceptors: Vec::new(),
            filters: Vec::new(),
            validators: Vec::new(),
            validation_enabled: false,
            validation_disabled: false,
            validation_options: ValidationOptions::default(),
            metadata: BTreeMap::new(),
            module_name: None,
            module_ref: None,
        })
    }

    pub fn pattern(&self) -> &str {
        &self.pattern
    }

    pub fn kind(&self) -> MessagePatternKind {
        self.kind
    }

    /// Return whether this pattern constructs its handler from each dispatch scope.
    pub fn is_scoped(&self) -> bool {
        self.handler.is_scoped()
    }

    pub fn module_name(&self) -> Option<&str> {
        self.module_name.as_deref()
    }

    pub fn metadata(&self) -> &BTreeMap<String, Value> {
        &self.metadata
    }

    pub fn metadata_value(&self, key: &str) -> Option<&Value> {
        self.metadata.get(key)
    }

    pub fn with_metadata<V>(self, key: impl Into<String>, value: V) -> Result<Self>
    where
        V: Serialize,
    {
        let key = key.into();
        let value = serde_json::to_value(value).map_err(|error| {
            BootError::Internal(format!(
                "failed to serialize message pattern metadata `{key}`: {error}"
            ))
        })?;
        Ok(self.with_metadata_value(key, value))
    }

    pub fn with_metadata_value(mut self, key: impl Into<String>, value: Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    pub fn with_pipe<P>(mut self, pipe: P) -> Self
    where
        P: TransportPipe,
    {
        self.pipes
            .push(PipelineComponent::<dyn TransportPipe>::new(pipe));
        self
    }

    pub fn with_guard<G>(mut self, guard: G) -> Self
    where
        G: TransportGuard,
    {
        self.guards
            .push(PipelineComponent::<dyn TransportGuard>::new(guard));
        self
    }

    pub fn with_execution_guard<G>(mut self, guard: G) -> Self
    where
        G: Guard,
    {
        self.guards
            .push(PipelineComponent::<dyn TransportGuard>::new(
                ExecutionTransportGuard { inner: guard },
            ));
        self
    }

    pub(crate) fn with_execution_pipeline_prefix(
        mut self,
        guards: &[Arc<dyn Guard>],
        interceptors: &[Arc<dyn ExecutionInterceptor>],
    ) -> Self {
        self.guards = prepend_execution_guards(guards, self.guards);
        self.interceptors = prepend_execution_interceptors(interceptors, self.interceptors);
        self
    }

    pub(crate) fn with_guard_prefix(mut self, guards: &[Arc<dyn TransportGuard>]) -> Self {
        let mut merged = guards
            .iter()
            .cloned()
            .map(PipelineComponent::<dyn TransportGuard>::from_arc)
            .collect::<Vec<_>>();
        merged.extend(self.guards);
        self.guards = merged;
        self
    }

    pub(crate) fn with_interceptor_prefix(
        mut self,
        interceptors: &[Arc<dyn TransportInterceptor>],
    ) -> Self {
        let mut merged = interceptors
            .iter()
            .cloned()
            .map(PipelineComponent::<dyn TransportInterceptor>::from_arc)
            .collect::<Vec<_>>();
        merged.extend(self.interceptors);
        self.interceptors = merged;
        self
    }

    pub(crate) fn with_pipe_prefix(mut self, pipes: &[Arc<dyn TransportPipe>]) -> Self {
        let mut merged = pipes
            .iter()
            .cloned()
            .map(PipelineComponent::<dyn TransportPipe>::from_arc)
            .collect::<Vec<_>>();
        merged.extend(self.pipes);
        self.pipes = merged;
        self
    }

    pub(crate) fn with_filter_prefix(
        mut self,
        filters: &[Arc<dyn TransportExceptionFilter>],
    ) -> Self {
        let mut merged = filters
            .iter()
            .cloned()
            .map(PipelineComponent::<dyn TransportExceptionFilter>::from_arc)
            .collect::<Vec<_>>();
        merged.extend(self.filters);
        self.filters = merged;
        self
    }

    pub fn with_interceptor<I>(mut self, interceptor: I) -> Self
    where
        I: TransportInterceptor,
    {
        self.interceptors
            .push(PipelineComponent::<dyn TransportInterceptor>::new(
                interceptor,
            ));
        self
    }

    pub fn with_execution_interceptor<I>(mut self, interceptor: I) -> Self
    where
        I: ExecutionInterceptor,
    {
        self.interceptors
            .push(PipelineComponent::<dyn TransportInterceptor>::new(
                ExecutionTransportInterceptor { inner: interceptor },
            ));
        self
    }

    pub fn with_filter<F>(mut self, filter: F) -> Self
    where
        F: TransportExceptionFilter,
    {
        self.filters
            .push(PipelineComponent::<dyn TransportExceptionFilter>::new(
                filter,
            ));
        self
    }

    pub fn with_catch_filter<I, F>(self, kinds: I, filter: F) -> Self
    where
        I: IntoIterator<Item = BootErrorKind>,
        F: TransportExceptionFilter,
    {
        self.with_filter(catch_errors(kinds, filter))
    }

    pub(crate) fn with_pipeline_overrides(mut self, overrides: &PipelineOverrides) -> Self {
        overrides.apply_to_transport_pipes(&mut self.pipes);
        overrides.apply_to_transport_guards(&mut self.guards);
        overrides.apply_to_transport_interceptors(&mut self.interceptors);
        overrides.apply_to_transport_filters(&mut self.filters);
        self
    }

    pub(crate) fn with_provider_enhancer_prefix(
        mut self,
        enhancers: &ProviderEnhancerComponents,
    ) -> Self {
        let mut pipes = enhancers.transport_pipes.clone();
        pipes.extend(self.pipes);
        self.pipes = pipes;

        let mut guards = enhancers.transport_guards.clone();
        guards.extend(self.guards);
        self.guards = guards;

        let mut interceptors = enhancers.transport_interceptors.clone();
        interceptors.extend(self.interceptors);
        self.interceptors = interceptors;

        let mut filters = enhancers.transport_filters.clone();
        filters.extend(self.filters);
        self.filters = filters;
        self
    }

    pub fn with_validation(mut self) -> Self {
        self.validation_enabled = true;
        self.validation_disabled = false;
        self
    }

    pub fn with_validation_options(mut self, options: ValidationOptions) -> Self {
        self.validation_enabled = true;
        self.validation_disabled = false;
        self.validation_options = self.validation_options.merge(options);
        self
    }

    pub fn without_validation(mut self) -> Self {
        self.validation_enabled = false;
        self.validation_disabled = true;
        self
    }

    pub(crate) fn with_validation_prefix(
        mut self,
        validation_enabled: bool,
        validation_options: ValidationOptions,
    ) -> Self {
        if !self.validation_disabled {
            self.validation_enabled = validation_enabled || self.validation_enabled;
            self.validation_options = validation_options.merge(self.validation_options);
        }
        self
    }

    pub fn with_payload_validation<T>(mut self) -> Self
    where
        T: DeserializeOwned + Validate + 'static,
    {
        self.validators.push(Arc::new(|message, _| {
            message.validated_data::<T>().map(|_| message)
        }));
        self.with_validation()
    }

    pub fn with_payload_validation_options<T>(mut self, options: ValidationOptions) -> Self
    where
        T: DeserializeOwned + Serialize + Validate + ValidationSchema + 'static,
    {
        self.validators
            .push(Arc::new(move |mut message, inherited_options| {
                let options = inherited_options.merge(options);
                let data = validate_json_value_with_options::<T>(
                    message.data.clone(),
                    options,
                    "message property",
                )?;
                if options.transform || options.whitelist {
                    message.data = data;
                }
                Ok(message)
            }));
        self.with_validation()
    }

    pub async fn dispatch(&self, message: TransportMessage) -> Result<Option<TransportReply>> {
        let state = MessageDispatchState::new(self.module_ref.as_ref());
        let context = TransportContext::new(self, &message.pattern, state.request());
        match self
            .dispatch_pipeline(message, context.clone(), state.clone())
            .await
        {
            Ok(reply) => Ok(reply),
            Err(error) => self.handle_error(context, error, &state.context_id).await,
        }
    }

    async fn dispatch_pipeline(
        &self,
        message: TransportMessage,
        context: TransportContext,
        state: MessageDispatchState,
    ) -> Result<Option<TransportReply>> {
        if message.pattern != self.pattern {
            return Err(BootError::NotFound(format!(
                "message pattern {}",
                message.pattern
            )));
        }

        for guard in &self.guards {
            let can_activate = guard
                .resolve(&state.context_id)?
                .can_activate(context.clone())
                .await?;
            if !can_activate {
                return Err(BootError::Forbidden(format!(
                    "message pattern {}",
                    message.pattern
                )));
            }
        }

        let reply = self
            .dispatch_interceptor_chain(0, context, message, state)
            .await?;
        Ok(if self.kind == MessagePatternKind::Event {
            None
        } else {
            reply
        })
    }

    fn dispatch_interceptor_chain<'a>(
        &'a self,
        index: usize,
        context: TransportContext,
        message: TransportMessage,
        state: MessageDispatchState,
    ) -> BoxFuture<'a, Result<Option<TransportReply>>> {
        Box::pin(async move {
            let Some(interceptor) = self.interceptors.get(index) else {
                return self.dispatch_handler_pipeline(message, state).await;
            };
            let interceptor = interceptor.resolve(&state.context_id)?;

            let next_context = context.clone();
            let next_message = message.clone();
            let next_state = state.clone();
            let next = CallHandler::from_fn(move || {
                self.dispatch_interceptor_chain(
                    index + 1,
                    next_context.clone(),
                    next_message.clone(),
                    next_state.clone(),
                )
            });
            interceptor.intercept(context, next).await
        })
    }

    async fn dispatch_handler_pipeline(
        &self,
        mut message: TransportMessage,
        state: MessageDispatchState,
    ) -> Result<Option<TransportReply>> {
        for pipe in &self.pipes {
            message = pipe.resolve(&state.context_id)?.transform(message).await?;
        }

        if self.validation_enabled {
            for validator in &self.validators {
                message = validator(message, self.validation_options)?;
            }
        }

        let handler = state.handler.resolve(
            &self.handler,
            state.module_ref.as_ref(),
            self.pattern.as_str(),
        )?;
        let mut reply = handler.call(message).await?;
        if self.kind == MessagePatternKind::Event {
            reply = None;
        }
        Ok(reply)
    }

    async fn handle_error(
        &self,
        context: TransportContext,
        error: BootError,
        context_id: &ContextId,
    ) -> Result<Option<TransportReply>> {
        for filter in self.filters.iter().rev() {
            let filter = filter.resolve(context_id)?;
            if let Some(response) = filter
                .catch(context.clone(), error.clone_for_filter())
                .await?
            {
                return Ok(if self.kind == MessagePatternKind::Event {
                    None
                } else {
                    response.into_reply()
                });
            }
        }
        Err(error)
    }

    pub(crate) fn with_module_name(mut self, module_name: &str) -> Self {
        self.module_name = Some(module_name.to_string());
        self
    }

    pub(crate) fn with_module_ref(mut self, module_ref: ModuleRef) -> Self {
        self.module_ref = Some(module_ref);
        self
    }

    pub(crate) fn with_default_module_ref(mut self, module_ref: ModuleRef) -> Self {
        if self.module_ref.is_none() {
            self.module_ref = Some(module_ref);
        }
        self
    }
}

fn prepend_execution_guards(
    prefix: &[Arc<dyn Guard>],
    values: Vec<PipelineComponent<dyn TransportGuard>>,
) -> Vec<PipelineComponent<dyn TransportGuard>> {
    let mut merged = prefix
        .iter()
        .cloned()
        .map(|guard| {
            PipelineComponent::<dyn TransportGuard>::new(ExecutionTransportGuard { inner: guard })
        })
        .collect::<Vec<_>>();
    merged.extend(values);
    merged
}

fn prepend_execution_interceptors(
    prefix: &[Arc<dyn ExecutionInterceptor>],
    values: Vec<PipelineComponent<dyn TransportInterceptor>>,
) -> Vec<PipelineComponent<dyn TransportInterceptor>> {
    let mut merged = prefix
        .iter()
        .cloned()
        .map(|interceptor| {
            PipelineComponent::<dyn TransportInterceptor>::new(ExecutionTransportInterceptor {
                inner: interceptor,
            })
        })
        .collect::<Vec<_>>();
    merged.extend(values);
    merged
}

fn validate_pattern(pattern: &str) -> Result<()> {
    if pattern.trim().is_empty() {
        return Err(BootError::BadRequest(
            "message pattern cannot be empty".to_string(),
        ));
    }
    Ok(())
}