Skip to main content

microde_application/
module.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use crate::{MicrodeError, ModuleKind, Provider, RelationshipDescriptor, RunContext, SetupContext};
5
6/// The object-safe future returned by module lifecycle operations.
7pub type ModuleFuture = Pin<Box<dyn Future<Output = Result<(), MicrodeError>> + Send + 'static>>;
8
9/// The lifecycle shared by every installed Microde module.
10pub trait MicrodeModule: Send {
11    /// Declares how the runtime interprets completion of [`Self::run`].
12    const KIND: ModuleKind;
13
14    fn relationships(&self) -> Vec<RelationshipDescriptor> {
15        Vec::new()
16    }
17
18    fn providers(&self) -> Vec<Provider> {
19        Vec::new()
20    }
21
22    fn initialize(&mut self) -> ModuleFuture {
23        Box::pin(async { Ok(()) })
24    }
25
26    fn setup(&mut self) -> ModuleFuture {
27        Box::pin(async { Ok(()) })
28    }
29
30    fn setup_with_context(&mut self, _context: SetupContext) -> ModuleFuture {
31        self.setup()
32    }
33
34    fn run(&mut self) -> ModuleFuture {
35        Box::pin(async { Ok(()) })
36    }
37
38    fn run_with_context(&mut self, _context: RunContext) -> ModuleFuture {
39        self.run()
40    }
41
42    fn stop(&mut self) -> ModuleFuture {
43        Box::pin(async { Ok(()) })
44    }
45
46    fn teardown(&mut self) -> ModuleFuture {
47        Box::pin(async { Ok(()) })
48    }
49
50    fn shutdown(&mut self) -> ModuleFuture {
51        Box::pin(async { Ok(()) })
52    }
53
54    fn cleanup(&mut self) -> ModuleFuture {
55        Box::pin(async { Ok(()) })
56    }
57}
58
59pub(crate) trait RuntimeModule: Send {
60    fn initialize(&mut self) -> ModuleFuture;
61    fn setup_with_context(&mut self, context: SetupContext) -> ModuleFuture;
62    fn run_with_context(&mut self, context: RunContext) -> ModuleFuture;
63    fn stop(&mut self) -> ModuleFuture;
64    fn teardown(&mut self) -> ModuleFuture;
65    fn shutdown(&mut self) -> ModuleFuture;
66    fn cleanup(&mut self) -> ModuleFuture;
67}
68
69impl<Module: MicrodeModule> RuntimeModule for Module {
70    fn initialize(&mut self) -> ModuleFuture {
71        MicrodeModule::initialize(self)
72    }
73    fn setup_with_context(&mut self, context: SetupContext) -> ModuleFuture {
74        MicrodeModule::setup_with_context(self, context)
75    }
76    fn run_with_context(&mut self, context: RunContext) -> ModuleFuture {
77        MicrodeModule::run_with_context(self, context)
78    }
79    fn stop(&mut self) -> ModuleFuture {
80        MicrodeModule::stop(self)
81    }
82    fn teardown(&mut self) -> ModuleFuture {
83        MicrodeModule::teardown(self)
84    }
85    fn shutdown(&mut self) -> ModuleFuture {
86        MicrodeModule::shutdown(self)
87    }
88    fn cleanup(&mut self) -> ModuleFuture {
89        MicrodeModule::cleanup(self)
90    }
91}