spring/plugin/
mod.rs

1#![doc = include_str!("../../Plugin.md")]
2
3/// Component definition
4pub mod component;
5pub mod service;
6
7use crate::error::Result;
8use crate::{app::AppBuilder, error::AppError};
9use async_trait::async_trait;
10use component::ComponentRef;
11use std::{
12    any::{self, Any},
13    ops::Deref,
14    sync::Arc,
15};
16
17/// Plugin Reference
18#[derive(Clone)]
19pub struct PluginRef(Arc<dyn Plugin>);
20
21/// Defined plugin interface
22#[async_trait]
23pub trait Plugin: Any + Send + Sync {
24    /// Configures the `App` to which this plugin is added.
25    async fn build(&self, _app: &mut AppBuilder) {}
26
27    /// Configures the `App` to which this plugin is added.
28    /// The immediately plugin will not be added to the registry,
29    /// and the plugin cannot obtain components registered in the registry.
30    fn immediately_build(&self, _app: &mut AppBuilder) {}
31
32    /// Configures a name for the [`Plugin`] which is primarily used for checking plugin
33    /// uniqueness and debugging.
34    fn name(&self) -> &str {
35        std::any::type_name::<Self>()
36    }
37
38    /// A list of plugins to depend on. The plugin will be built after the plugins in this list.
39    fn dependencies(&self) -> Vec<&str> {
40        vec![]
41    }
42
43    /// Whether the plugin should be built immediately when added
44    fn immediately(&self) -> bool {
45        false
46    }
47}
48
49impl PluginRef {
50    pub(crate) fn new<T: Plugin>(plugin: T) -> Self {
51        Self(Arc::new(plugin))
52    }
53}
54
55impl Deref for PluginRef {
56    type Target = dyn Plugin;
57
58    fn deref(&self) -> &Self::Target {
59        &*self.0
60    }
61}
62
63/// Component Registry
64pub trait ComponentRegistry {
65    /// Get the component reference of the specified type
66    fn get_component_ref<T>(&self) -> Option<ComponentRef<T>>
67    where
68        T: Any + Send + Sync;
69
70    /// Get the component reference of the specified type.
71    /// If the component does not exist, it will panic.
72    fn get_expect_component_ref<T>(&self) -> ComponentRef<T>
73    where
74        T: Clone + Send + Sync + 'static,
75    {
76        self.get_component_ref().unwrap_or_else(|| {
77            panic!(
78                "{} component not exists in registry",
79                std::any::type_name::<T>()
80            )
81        })
82    }
83
84    /// Get the component reference of the specified type.
85    /// If the component does not exist, it will return AppError::ComponentNotExist.
86    fn try_get_component_ref<T>(&self) -> Result<ComponentRef<T>>
87    where
88        T: Clone + Send + Sync + 'static,
89    {
90        self.get_component_ref()
91            .ok_or_else(|| AppError::ComponentNotExist(std::any::type_name::<T>()))
92    }
93
94    /// Get the component of the specified type
95    fn get_component<T>(&self) -> Option<T>
96    where
97        T: Clone + Send + Sync + 'static;
98
99    /// Get the component of the specified type.
100    /// If the component does not exist, it will panic.
101    fn get_expect_component<T>(&self) -> T
102    where
103        T: Clone + Send + Sync + 'static,
104    {
105        self.get_component().unwrap_or_else(|| {
106            panic!(
107                "{} component not exists in registry",
108                std::any::type_name::<T>()
109            )
110        })
111    }
112
113    /// Get the component of the specified type.
114    /// If the component does not exist, it will return AppError::ComponentNotExist.
115    fn try_get_component<T>(&self) -> Result<T>
116    where
117        T: Clone + Send + Sync + 'static,
118    {
119        self.get_component()
120            .ok_or_else(|| AppError::ComponentNotExist(std::any::type_name::<T>()))
121    }
122
123    /// Is there a component of the specified type in the registry?
124    fn has_component<T>(&self) -> bool
125    where
126        T: Any + Send + Sync;
127}
128
129/// Mutable Component Registry
130pub trait MutableComponentRegistry: ComponentRegistry {
131    /// Add component to the registry
132    fn add_component<C>(&mut self, component: C) -> &mut Self
133    where
134        C: Clone + any::Any + Send + Sync;
135}