1#![doc = include_str!("../../Plugin.md")]
2
3pub mod component;
5pub mod lazy;
7pub mod service;
9
10use crate::error::Result;
11use crate::{app::AppBuilder, error::AppError};
12use async_trait::async_trait;
13use component::ComponentRef;
14pub use lazy::LazyComponent;
15use std::{
16 any::{self, Any},
17 ops::Deref,
18 sync::Arc,
19};
20pub use service::Service;
21
22#[derive(Clone)]
24pub struct PluginRef(Arc<dyn Plugin>);
25
26#[async_trait]
28pub trait Plugin: Any + Send + Sync {
29 async fn build(&self, _app: &mut AppBuilder) {}
31
32 fn immediately_build(&self, _app: &mut AppBuilder) {}
36
37 fn name(&self) -> &str {
40 std::any::type_name::<Self>()
41 }
42
43 fn dependencies(&self) -> Vec<&str> {
45 vec![]
46 }
47
48 fn immediately(&self) -> bool {
50 false
51 }
52}
53
54impl PluginRef {
55 pub(crate) fn new<T: Plugin>(plugin: T) -> Self {
56 Self(Arc::new(plugin))
57 }
58}
59
60impl Deref for PluginRef {
61 type Target = dyn Plugin;
62
63 fn deref(&self) -> &Self::Target {
64 &*self.0
65 }
66}
67
68pub trait ComponentRegistry {
70 fn get_component_ref<T>(&self) -> Option<ComponentRef<T>>
72 where
73 T: Any + Send + Sync;
74
75 fn get_expect_component_ref<T>(&self) -> ComponentRef<T>
78 where
79 T: Clone + Send + Sync + 'static,
80 {
81 self.get_component_ref().unwrap_or_else(|| {
82 panic!(
83 "{} component not exists in registry",
84 std::any::type_name::<T>()
85 )
86 })
87 }
88
89 fn try_get_component_ref<T>(&self) -> Result<ComponentRef<T>>
92 where
93 T: Clone + Send + Sync + 'static,
94 {
95 self.get_component_ref()
96 .ok_or_else(|| AppError::ComponentNotExist(std::any::type_name::<T>()))
97 }
98
99 fn get_component<T>(&self) -> Option<T>
101 where
102 T: Clone + Send + Sync + 'static;
103
104 fn get_expect_component<T>(&self) -> T
107 where
108 T: Clone + Send + Sync + 'static,
109 {
110 self.get_component().unwrap_or_else(|| {
111 panic!(
112 "{} component not exists in registry",
113 std::any::type_name::<T>()
114 )
115 })
116 }
117
118 fn try_get_component<T>(&self) -> Result<T>
121 where
122 T: Clone + Send + Sync + 'static,
123 {
124 self.get_component()
125 .ok_or_else(|| AppError::ComponentNotExist(std::any::type_name::<T>()))
126 }
127
128 fn has_component<T>(&self) -> bool
130 where
131 T: Any + Send + Sync;
132}
133
134pub trait MutableComponentRegistry: ComponentRegistry {
136 fn add_component<C>(&mut self, component: C) -> &mut Self
138 where
139 C: Clone + any::Any + Send + Sync;
140}