microde-application 0.4.0

Composition and lifecycle runtime for modular Microde applications.
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
use std::collections::{HashMap, HashSet};
use std::future::Future;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use futures::future::{BoxFuture, FutureExt, join_all, pending, ready};
use futures::stream::{FuturesUnordered, StreamExt};

#[cfg(test)]
use crate::MicrodeContext;
use crate::dependency_graph::DependencyGraph;
use crate::lifecycle_context::ResolvedRelationship;
use crate::runtime::{
    ErrorPriority, ErrorRecorder, InstalledModule, ModuleStage, RuntimeContext, RuntimeControl,
    spawn, terminate_process,
};
use crate::{
    MicrodeApplicationState, MicrodeContextHandle, MicrodeError, MicrodeExecutionResult,
    MicrodeModule, MicrodeStopRequest, ModuleFuture, ModuleHandle, ModuleHandleIdentity,
    ModuleInstanceId, ModuleKind, RelationshipKind, RelationshipSlot, RunContext, SetupContext,
};

static NEXT_SERVICE_ID: AtomicU64 = AtomicU64::new(0);

#[derive(Clone)]
struct Binding {
    owner: ModuleInstanceId,
    target: ModuleInstanceId,
    port_id: u64,
    provider: crate::Provider,
}

#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct ModuleExecutionErrors {
    pub(crate) execution: Vec<MicrodeError>,
    pub(crate) stop: Vec<MicrodeError>,
}

type ModuleRunFuture =
    Pin<Box<dyn Future<Output = (usize, ModuleKind, Result<(), MicrodeError>)> + Send + 'static>>;

type ApplicationMain = Box<dyn FnOnce(MicrodeContextHandle) -> ModuleFuture + Send + 'static>;

/// Composes modules and coordinates their lifecycle.
pub struct MicrodeApplication {
    pub(crate) modules: Vec<InstalledModule>,
    pub(crate) context: MicrodeContextHandle,
    pub(crate) control: Arc<RuntimeControl>,
    pub(crate) current_state: Arc<Mutex<MicrodeApplicationState>>,
    composition_id: u64,
    bindings: HashMap<u64, Binding>,
    resolutions: HashMap<u64, ResolvedRelationship>,
    composition_sealed: bool,
}

struct InstallationStateReset(Arc<Mutex<MicrodeApplicationState>>);

impl Drop for InstallationStateReset {
    fn drop(&mut self) {
        *self
            .0
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = MicrodeApplicationState::Idle;
    }
}

impl MicrodeApplication {
    /// Creates an idle application with the production module context.
    pub fn new() -> Self {
        let control = Arc::new(RuntimeControl::default());
        let current_state = Arc::new(Mutex::new(MicrodeApplicationState::Idle));
        let context = Arc::new(RuntimeContext::new(
            control.clone(),
            current_state.clone(),
            terminate_process,
        ));
        Self {
            modules: Vec::new(),
            context,
            control,
            current_state,
            composition_id: NEXT_SERVICE_ID.fetch_add(1, Ordering::Relaxed),
            bindings: HashMap::new(),
            resolutions: HashMap::new(),
            composition_sealed: false,
        }
    }

    #[cfg(test)]
    pub(crate) fn with_context(context: MicrodeContextHandle) -> Self {
        Self::with_context_and_control(context, Arc::new(RuntimeControl::default()))
    }

    #[cfg(test)]
    pub(crate) fn with_context_and_control(
        context: MicrodeContextHandle,
        control: Arc<RuntimeControl>,
    ) -> Self {
        Self {
            modules: Vec::new(),
            context,
            control,
            current_state: Arc::new(Mutex::new(MicrodeApplicationState::Idle)),
            composition_id: NEXT_SERVICE_ID.fetch_add(1, Ordering::Relaxed),
            bindings: HashMap::new(),
            resolutions: HashMap::new(),
            composition_sealed: false,
        }
    }

    pub fn state(&self) -> MicrodeApplicationState {
        *self
            .current_state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    pub fn install<Module, Factory>(&mut self, factory: Factory) -> Result<(), MicrodeError>
    where
        Module: MicrodeModule + 'static,
        Factory: FnOnce(MicrodeContextHandle) -> Module,
    {
        match self.ensure_installable() {
            Ok(()) => {}
            Err(error) => return Err(error),
        }
        self.set_state(MicrodeApplicationState::Installing);
        let module = {
            let reset = InstallationStateReset(self.current_state.clone());
            let module = factory(self.context.clone());
            drop(reset);
            module
        };
        let id = ModuleInstanceId::new(format!("@installation/{}", self.modules.len()));
        self.modules.push(InstalledModule::new(id, module));
        Ok(())
    }

    pub fn install_named<Module, Factory>(
        &mut self,
        id: impl Into<String>,
        factory: Factory,
    ) -> Result<ModuleHandle<Module>, MicrodeError>
    where
        Module: MicrodeModule + 'static,
        Factory: FnOnce(MicrodeContextHandle) -> Module,
    {
        let id = self.reserve_named_id(id.into())?;
        self.set_state(MicrodeApplicationState::Installing);
        let module = {
            let reset = InstallationStateReset(self.current_state.clone());
            let module = factory(self.context.clone());
            drop(reset);
            module
        };
        let handle = ModuleHandle::new(id.clone(), self.composition_id);
        self.modules.push(InstalledModule::new(id, module));
        Ok(handle)
    }

    fn reserve_named_id(&self, value: String) -> Result<ModuleInstanceId, MicrodeError> {
        self.ensure_installable()?;
        let id = ModuleInstanceId::new(value);
        if self.modules.iter().any(|module| module.id() == &id) {
            return Err(MicrodeError::new(format!(
                "module instance ID '{}' is already installed",
                id.as_str()
            )));
        }
        Ok(id)
    }

    pub fn bind(
        &mut self,
        consumer: &dyn ModuleHandleIdentity,
        slot: &dyn RelationshipSlot,
        target: &dyn ModuleHandleIdentity,
    ) -> Result<(), MicrodeError> {
        self.ensure_installable()?;
        for handle in [
            (consumer.module_instance_id(), consumer.composition_owner()),
            (target.module_instance_id(), target.composition_owner()),
        ] {
            if handle.1 != self.composition_id {
                return Err(MicrodeError::new(format!(
                    "module handle '{}' belongs to another application",
                    handle.0.as_str()
                )));
            }
        }
        let descriptor = slot.descriptor();
        let installed = self
            .modules
            .iter()
            .find(|module| module.id() == consumer.module_instance_id())
            .unwrap();
        if !installed
            .relationships()
            .iter()
            .any(|known| known.slot_id == descriptor.slot_id)
        {
            return Err(MicrodeError::new(format!(
                "unknown relationship '{}.{}'",
                consumer.module_instance_id().as_str(),
                descriptor.name
            )));
        }
        if self.bindings.contains_key(&descriptor.slot_id) {
            return Err(MicrodeError::new(format!(
                "relationship '{}.{}' is already bound",
                consumer.module_instance_id().as_str(),
                descriptor.name
            )));
        }
        let provider = self
            .modules
            .iter()
            .find(|module| module.id() == target.module_instance_id())
            .unwrap();
        if let Some((required, name)) = descriptor.module_type
            && provider.module_type() != required
        {
            return Err(MicrodeError::new(format!(
                "module '{}' does not satisfy concrete module requirement '{}'",
                target.module_instance_id().as_str(),
                name.rsplit("::").next().unwrap_or(name)
            )));
        }
        let Some(exported) = provider
            .providers()
            .iter()
            .find(|known| known.port_id == descriptor.port_id)
        else {
            return Err(MicrodeError::new(format!(
                "module '{}' does not provide port '{}'",
                target.module_instance_id().as_str(),
                descriptor.port_description
            )));
        };
        self.bindings.insert(
            descriptor.slot_id,
            Binding {
                owner: consumer.module_instance_id().clone(),
                target: target.module_instance_id().clone(),
                port_id: descriptor.port_id,
                provider: exported.clone(),
            },
        );
        Ok(())
    }

    fn wire_composition(&mut self) -> Result<(), MicrodeError> {
        let mut graph = DependencyGraph::new(
            self.modules
                .iter()
                .map(|module| module.id().clone())
                .collect(),
        );
        for module in &self.modules {
            for relationship in module.relationships() {
                let binding = self.bindings.get(&relationship.slot_id).ok_or_else(|| {
                    MicrodeError::new(format!(
                        "missing binding for relationship '{}.{}'",
                        module.id().as_str(),
                        relationship.name
                    ))
                })?;
                if relationship.kind == RelationshipKind::Dependency {
                    graph.add_validated_dependency(&binding.owner, &binding.target);
                }
            }
        }
        let order = graph.order()?;
        let mut staged = HashMap::new();
        let mut provider_values: HashMap<
            (ModuleInstanceId, u64),
            Arc<dyn std::any::Any + Send + Sync>,
        > = HashMap::new();
        for module in &self.modules {
            for relationship in module.relationships() {
                let binding = &self.bindings[&relationship.slot_id];
                let provider_key = (binding.target.clone(), binding.port_id);
                let value = match provider_values.get(&provider_key) {
                    Some(value) => value.clone(),
                    None => {
                        let value = binding.provider.resolve()?;
                        provider_values.insert(provider_key, value.clone());
                        value
                    }
                };
                staged.insert(
                    relationship.slot_id,
                    ResolvedRelationship {
                        owner: module.id().clone(),
                        name: relationship.name.clone(),
                        kind: relationship.kind,
                        value,
                    },
                );
            }
        }
        self.modules
            .sort_by_key(|module| order.iter().position(|id| id == module.id()));
        self.resolutions = staged;
        Ok(())
    }

    fn ensure_installable(&self) -> Result<(), MicrodeError> {
        if self.state() != MicrodeApplicationState::Idle {
            return Err(MicrodeError::new(format!(
                "cannot install module after application has started; current state: {:?}",
                self.state()
            )));
        }
        if self.composition_sealed {
            return Err(MicrodeError::new(
                "cannot modify composition after it is sealed",
            ));
        }
        Ok(())
    }

    pub(crate) fn set_state(&self, state: MicrodeApplicationState) {
        *self
            .current_state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = state;
    }

    /// Serves the application using module completion and stop requests to control its lifetime.
    ///
    /// The lifecycle continues if the returned future is dropped. Any later call to [`Self::stop`]
    /// receives the same shared completion result.
    pub fn serve(&mut self) -> BoxFuture<'static, Result<MicrodeExecutionResult, MicrodeError>> {
        self.start(None)
    }

    /// Runs an application-level task after all modules have started.
    ///
    /// Completion or failure of the task begins orderly application shutdown.
    pub fn run<Main, MainFuture>(
        &mut self,
        main: Main,
    ) -> BoxFuture<'static, Result<MicrodeExecutionResult, MicrodeError>>
    where
        Main: FnOnce(MicrodeContextHandle) -> MainFuture + Send + 'static,
        MainFuture: Future<Output = Result<(), MicrodeError>> + Send + 'static,
    {
        self.start(Some(Box::new(move |context| Box::pin(main(context)))))
    }

    fn start(
        &mut self,
        main: Option<ApplicationMain>,
    ) -> BoxFuture<'static, Result<MicrodeExecutionResult, MicrodeError>> {
        if self.state() != MicrodeApplicationState::Idle {
            return ready(Err(MicrodeError::new(format!(
                "cannot start application more than once; current state: {:?}",
                self.state()
            ))))
            .boxed();
        }

        if self.composition_sealed {
            return ready(Err(MicrodeError::new(
                "cannot start application more than once; composition is sealed",
            )))
            .boxed();
        }
        self.composition_sealed = true;

        if let Err(error) = self.wire_composition() {
            return ready(Err(error)).boxed();
        }

        self.set_state(MicrodeApplicationState::Initialization);
        let mut runner = Self {
            modules: std::mem::take(&mut self.modules),
            context: self.context.clone(),
            control: self.control.clone(),
            current_state: self.current_state.clone(),
            composition_id: self.composition_id,
            bindings: std::mem::take(&mut self.bindings),
            resolutions: std::mem::take(&mut self.resolutions),
            composition_sealed: true,
        };

        let control = runner.control.clone();
        let completion = control.clone();
        spawn(async move {
            let result = AssertUnwindSafe(runner.execute_lifecycle(main))
                .catch_unwind()
                .await
                .map_err(|panic| {
                    runner.set_state(MicrodeApplicationState::Failed);
                    MicrodeError::new(panic_message(panic))
                });
            control.complete(result);
        });

        async move { completion.wait_for_completion().await }.boxed()
    }

    /// Requests an orderly stop and waits for lifecycle completion.
    pub fn stop(
        &self,
        request: MicrodeStopRequest,
    ) -> BoxFuture<'static, Result<MicrodeExecutionResult, MicrodeError>> {
        let state = self.state();
        if matches!(
            state,
            MicrodeApplicationState::Idle | MicrodeApplicationState::Installing
        ) {
            return ready(Err(MicrodeError::new(format!(
                "cannot stop application before it has started; current state: {state:?}"
            ))))
            .boxed();
        }

        self.control.request_stop(request);
        let control = self.control.clone();
        async move { control.wait_for_completion().await }.boxed()
    }

    async fn execute_lifecycle(&mut self, main: Option<ApplicationMain>) -> MicrodeExecutionResult {
        let mut errors = ErrorRecorder::default();
        let mut forward_failed = false;

        if let Err(error) = self.initialize_modules().await {
            errors.record(error, ErrorPriority::Lifecycle);
            forward_failed = true;
        }

        if !forward_failed && !self.control.stop_requested() {
            self.set_state(MicrodeApplicationState::Setup);
            if let Err(error) = self.setup_modules().await {
                errors.record(error, ErrorPriority::Lifecycle);
                forward_failed = true;
            }
        }

        if !forward_failed && !self.control.stop_requested() {
            self.set_state(MicrodeApplicationState::Running);
            let execution_errors = self.execute_modules(main).await;
            for error in execution_errors.execution {
                errors.record(error, ErrorPriority::Execution);
            }
            for error in execution_errors.stop {
                errors.record(error, ErrorPriority::Stop);
            }
        }

        self.set_state(MicrodeApplicationState::TearDown);
        for error in self.teardown_modules().await {
            errors.record(error, ErrorPriority::Lifecycle);
        }

        self.set_state(MicrodeApplicationState::Shutdown);
        for error in self.shutdown_modules().await {
            errors.record(error, ErrorPriority::Lifecycle);
        }

        self.set_state(MicrodeApplicationState::CleanUp);
        for error in self.cleanup_modules().await {
            errors.record(error, ErrorPriority::Lifecycle);
        }

        let stop_request = self.control.stop_request();
        let exit_code = stop_request.as_ref().and_then(|request| request.exit_code);
        if let Some(error) = stop_request.and_then(|request| request.error) {
            errors.record(error, ErrorPriority::StopRequest);
        }

        let result = errors.into_result(exit_code);
        if result.error.is_some() || result.exit_code != 0 {
            self.set_state(MicrodeApplicationState::Failed);
        } else {
            self.set_state(MicrodeApplicationState::Finished);
        }
        result
    }

    pub(crate) async fn initialize_modules(&mut self) -> Result<(), MicrodeError> {
        for installed in &mut self.modules {
            if self.control.stop_requested() {
                break;
            }

            installed.set_stage(ModuleStage::Initializing);
            match installed.initialize().await {
                Ok(()) => installed.set_stage(ModuleStage::Initialized),
                Err(error) => return Err(error),
            }
        }
        Ok(())
    }

    pub(crate) async fn setup_modules(&mut self) -> Result<(), MicrodeError> {
        let resolutions = Arc::new(self.resolutions.clone());
        for installed in &mut self.modules {
            if self.control.stop_requested() {
                break;
            }

            installed.set_stage(ModuleStage::SettingUp);
            let context = SetupContext::new(installed.id().clone(), resolutions.clone());
            match installed.setup_with_context(context).await {
                Ok(()) => installed.set_stage(ModuleStage::SetUp),
                Err(error) => return Err(error),
            }
        }
        Ok(())
    }

    pub(crate) async fn execute_modules(
        &mut self,
        main: Option<ApplicationMain>,
    ) -> ModuleExecutionErrors {
        let mut errors = ModuleExecutionErrors::default();
        let mut runs: FuturesUnordered<ModuleRunFuture> = FuturesUnordered::new();
        let resolutions = Arc::new(self.resolutions.clone());

        for (index, installed) in self.modules.iter_mut().enumerate() {
            installed.set_stage(ModuleStage::Executing);
            let kind = installed.kind();
            let context = RunContext::new(installed.id().clone(), resolutions.clone());
            let run = installed.run_with_context(context);
            runs.push(Box::pin(async move { (index, kind, run.await) }));
        }

        let stop_signal = self.control.take_stop_receiver().fuse();
        futures::pin_mut!(stop_signal);

        let has_main = main.is_some();
        let main_future = match main {
            Some(main) => main(self.context.clone()).boxed(),
            None => pending::<Result<(), MicrodeError>>().boxed(),
        }
        .fuse();
        futures::pin_mut!(main_future);

        loop {
            if runs.is_empty() {
                if !has_main {
                    break;
                }
                futures::select_biased! {
                    _ = stop_signal => break,
                    result = main_future => {
                        if let Err(error) = result {
                            errors.execution.push(error);
                        }
                        break;
                    }
                }
            }
            futures::select_biased! {
                _ = stop_signal => break,
                result = main_future => {
                    if let Err(error) = result {
                        errors.execution.push(error);
                    }
                    break;
                },
                outcome = runs.next().fuse() => {
                    let (index, kind, result) = outcome
                        .expect("non-empty module runs have a next completion");
                    let should_stop = kind == ModuleKind::Active || result.is_err();
                    self.record_run_completion((index, kind, result), &mut errors);
                    if should_stop {
                        break;
                    }
                }
            }
        }

        let module_indexes = self
            .modules
            .iter()
            .enumerate()
            .rev()
            .map(|(index, _)| index)
            .collect::<Vec<_>>();
        let mut stop_futures = Vec::with_capacity(module_indexes.len());
        for index in module_indexes {
            let stop = self.modules[index].stop();
            stop_futures.push(async move { (index, stop.await) });
        }

        let mut required_completions = self
            .modules
            .iter()
            .enumerate()
            .filter_map(|(index, module)| {
                (module.kind() == ModuleKind::Passive && module.stage() != ModuleStage::Executed)
                    .then_some(index)
            })
            .collect::<HashSet<_>>();

        for (index, result) in join_all(stop_futures).await {
            match result {
                Ok(()) => {
                    if self.modules[index].stage() != ModuleStage::Executed {
                        required_completions.insert(index);
                    }
                }
                Err(error) => errors.stop.push(error),
            }
        }

        while !required_completions.is_empty() {
            let outcome = runs
                .next()
                .await
                .expect("required module completions have corresponding run futures");
            required_completions.remove(&outcome.0);
            self.record_run_completion(outcome, &mut errors);
        }

        // JavaScript promises continue running even when the lifecycle no longer awaits them.
        // Preserve that behavior for active runs whose stop operation failed instead of
        // cancelling their futures when `runs` is dropped.
        if !runs.is_empty() {
            spawn(async move { while runs.next().await.is_some() {} });
        }

        errors
    }

    fn record_run_completion(
        &mut self,
        (index, _kind, result): (usize, ModuleKind, Result<(), MicrodeError>),
        errors: &mut ModuleExecutionErrors,
    ) {
        self.modules[index].set_stage(ModuleStage::Executed);
        if let Err(error) = result {
            errors.execution.push(error);
        }
    }

    pub(crate) async fn teardown_modules(&mut self) -> Vec<MicrodeError> {
        let mut errors = Vec::new();
        for installed in self.modules.iter_mut().rev() {
            if installed.stage() < ModuleStage::SettingUp
                || installed.stage() >= ModuleStage::TearingDown
            {
                continue;
            }

            installed.set_stage(ModuleStage::TearingDown);
            if let Err(error) = installed.teardown().await {
                errors.push(error);
            }
            installed.set_stage(ModuleStage::TornDown);
        }
        errors
    }

    pub(crate) async fn shutdown_modules(&mut self) -> Vec<MicrodeError> {
        let mut errors = Vec::new();
        for installed in self.modules.iter_mut().rev() {
            if installed.stage() < ModuleStage::Initializing
                || installed.stage() >= ModuleStage::ShuttingDown
            {
                continue;
            }

            installed.set_stage(ModuleStage::ShuttingDown);
            if let Err(error) = installed.shutdown().await {
                errors.push(error);
            }
            installed.set_stage(ModuleStage::Shutdown);
        }
        errors
    }

    pub(crate) async fn cleanup_modules(&mut self) -> Vec<MicrodeError> {
        let mut errors = Vec::new();
        for installed in self.modules.iter_mut().rev() {
            installed.set_stage(ModuleStage::CleaningUp);
            if let Err(error) = installed.cleanup().await {
                errors.push(error);
            }
            installed.set_stage(ModuleStage::CleanedUp);
        }
        errors
    }
}

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

fn panic_message(panic: Box<dyn std::any::Any + Send>) -> String {
    if let Some(message) = panic.downcast_ref::<&str>() {
        return (*message).to_owned();
    }
    if let Some(message) = panic.downcast_ref::<String>() {
        return message.clone();
    }
    "application lifecycle panicked".to_owned()
}

#[cfg(test)]
#[path = "tests/composition_wiring.rs"]
mod composition_wiring_tests;
#[cfg(test)]
#[path = "tests/dependency_lifecycle.rs"]
mod dependency_lifecycle_tests;
#[cfg(test)]
#[path = "tests/execution.rs"]
mod execution_tests;
#[cfg(test)]
#[path = "tests/initialization_and_setup.rs"]
mod initialization_and_setup_tests;
#[cfg(test)]
#[path = "tests/installation.rs"]
mod installation_tests;
#[cfg(test)]
#[path = "tests/public_runtime.rs"]
mod public_runtime_tests;
#[cfg(test)]
#[path = "tests/unwind.rs"]
mod unwind_tests;