Skip to main content

fusion_core/plugin/
mod.rs

1use crate::application::ApplicationBuilder;
2use async_trait::async_trait;
3use std::{any::Any, ops::Deref, sync::Arc};
4
5/// Plugin Reference
6#[derive(Clone)]
7pub struct PluginRef(Arc<dyn Plugin>);
8
9/// Defined plugin interface
10#[async_trait]
11pub trait Plugin: Any + Send + Sync {
12  /// Configures the `App` to which this plugin is added.
13  async fn build(&self, _app: &mut ApplicationBuilder) {}
14
15  /// Configures the `App` to which this plugin is added.
16  /// The immediately plugin will not be added to the registry,
17  /// and the plugin cannot obtain components registered in the registry.
18  fn immediately_build(&self, _app: &mut ApplicationBuilder) {}
19
20  /// Configures a name for the [`Plugin`] which is primarily used for checking plugin
21  /// uniqueness and debugging.
22  fn name(&self) -> &str {
23    std::any::type_name::<Self>()
24  }
25
26  /// A list of plugin names to depend on. The plugin will be built after the plugins in this list.
27  fn dependencies(&self) -> Vec<&str> {
28    vec![]
29  }
30
31  /// Whether the plugin should be built immediately when added
32  fn immediately(&self) -> bool {
33    false
34  }
35}
36
37impl PluginRef {
38  pub(crate) fn new<T: Plugin>(plugin: T) -> Self {
39    Self(Arc::new(plugin))
40  }
41}
42
43impl Deref for PluginRef {
44  type Target = dyn Plugin;
45
46  fn deref(&self) -> &Self::Target {
47    &*self.0
48  }
49}