miwa_core/core/
extension.rs

1use std::any::TypeId;
2
3use super::{error::MiwaResult, MiwaContext, SystemGroup};
4
5#[async_trait::async_trait]
6pub trait Extension: Send {
7    async fn start(&self) -> MiwaResult<()>;
8    async fn shutdown(&self) -> MiwaResult<()>;
9}
10
11#[async_trait::async_trait]
12pub trait ExtensionFactory: Send + Sync
13where
14    Self: 'static,
15{
16    fn name(&self) -> &str;
17    async fn init(&self, context: &MiwaContext) -> MiwaResult<Box<dyn Extension>>;
18
19    fn exposes(&self) -> Vec<TypeId> {
20        vec![]
21    }
22
23    fn requires(&self) -> Vec<TypeId> {
24        vec![]
25    }
26
27    fn id(&self) -> TypeId {
28        TypeId::of::<Self>()
29    }
30}
31
32#[async_trait::async_trait]
33pub(crate) trait InternalExtensionFactory: Send {
34    fn name(&self) -> &str;
35    async fn init(&self, context: &MiwaContext) -> MiwaResult<Box<dyn Extension>>;
36
37    fn exposes(&self) -> Vec<TypeId> {
38        vec![]
39    }
40
41    fn requires(&self) -> Vec<TypeId> {
42        vec![]
43    }
44}
45
46pub(crate) struct IntoInternalExtensionFactory<F> {
47    factory: F,
48}
49
50impl<F> IntoInternalExtensionFactory<F> {
51    pub fn from_extension_factory(factory: F) -> Self {
52        IntoInternalExtensionFactory { factory }
53    }
54}
55
56#[async_trait::async_trait]
57impl<F> InternalExtensionFactory for IntoInternalExtensionFactory<F>
58where
59    F: ExtensionFactory,
60{
61    fn name(&self) -> &str {
62        self.factory.name()
63    }
64    async fn init(&self, context: &MiwaContext) -> MiwaResult<Box<dyn Extension>> {
65        self.factory.init(context).await
66    }
67    fn exposes(&self) -> Vec<TypeId> {
68        self.factory.exposes()
69    }
70
71    fn requires(&self) -> Vec<TypeId> {
72        self.factory.requires()
73    }
74}
75
76pub trait ExtensionGroup {
77    fn register(&self, system: &mut SystemGroup);
78}