1use crate::{
2 event::EventDispatcher,
3 plugin::PluginOptions,
4 strategy::StrategyOptions,
5 units::error::{BError, MyError},
6};
7use serde_json::Value;
8use std::collections::HashMap;
9
10type PluginsMap = HashMap<String, Box<dyn PluginOptions>>;
11type StrategysMap = HashMap<String, Box<dyn StrategyOptions>>;
12
13pub struct Engine {
14 pub event: EventDispatcher,
15 plugins: PluginsMap,
16 strategys: StrategysMap,
17}
18
19impl Engine {
20 pub fn new() -> Self {
21 Self {
22 event: EventDispatcher::new(),
23 plugins: HashMap::new(),
24 strategys: HashMap::new(),
25 }
26 }
27 pub fn get_plugins(&self) -> &PluginsMap {
29 &self.plugins
30 }
31 pub fn plugin_data(&self, name: &str) -> Option<Value> {
33 if let Some(x) = self.plugins.get(name) {
34 x.get_data()
35 } else {
36 println!("插件{}不存在", name);
37 None
38 }
39 }
40 pub fn get_strategys(&self) -> &StrategysMap {
42 &self.strategys
43 }
44 pub fn strategy_data(&self, name: &str) -> Option<Value> {
46 if let Some(x) = self.strategys.get(name) {
47 x.get_data()
48 } else {
49 println!("策略{}不存在", name);
50 None
51 }
52 }
53 pub fn install<P: PluginOptions + 'static>(&mut self, plugin: P) -> MyError<()> {
55 let name = plugin.name();
56 if !self.plugins.contains_key(name) {
57 let mut deps: Vec<&str> = Vec::new();
61 for dep in plugin.deps() {
62 if !self.plugins.contains_key(dep) {
64 deps.push(dep);
65 }
66 }
67 if !deps.is_empty() {
68 return Err(BError::with_msg(&format!(
70 "安装插件 '{}' 出错:依赖插件 '{}' 不存在",
71 name,
72 deps.join(",")
73 )));
74 }
75
76 let mut plugin_box = Box::new(plugin);
78 plugin_box.install(self);
79
80 self.plugins
82 .insert(plugin_box.name().to_string(), plugin_box);
83 }
84 Ok(())
85 }
86 pub fn uninstall(&mut self, plugin_name: &str) {
88 if let Some(mut plugin) = self.plugins.remove(plugin_name) {
89 let mut strategies = Vec::new();
91 for (_, strategy) in self.strategys.iter() {
92 if strategy.condition().contains(&plugin_name) {
93 strategies.push(strategy.name().to_string());
94 }
95 }
96 for strategy_name in strategies {
98 self.rollback(&strategy_name);
99 }
100
101 let mut plugins = Vec::new();
103 for (_, plugin) in self.plugins.iter() {
104 if plugin.deps().contains(&plugin_name) {
105 plugins.push(plugin.name().to_string());
106 }
107 }
108 for plugin_name in plugins {
110 self.uninstall(&plugin_name);
111 }
112
113 plugin.dispose(self);
115 } else {
116 println!("插件 {} 不存在", plugin_name)
117 }
118 }
119 pub fn exec<S: StrategyOptions + 'static>(&mut self, strategy: S) -> MyError<()> {
121 let name = strategy.name();
122 if !self.strategys.contains_key(name) {
123 let mut plugins: Vec<&str> = Vec::new();
127 for plugin in strategy.condition() {
128 if !self.plugins.contains_key(plugin) {
130 plugins.push(plugin);
131 }
132 }
133 if plugins.len() > 0 {
134 return Err(BError::with_msg(&format!(
136 "执行策略 '{}' 出错:依赖插件 '{}' 不存在",
137 name,
138 plugins.join(",")
139 )));
140 }
141
142 strategy.exec(self);
144
145 self.strategys
147 .insert(strategy.name().to_string(), Box::new(strategy));
148 }
149
150 Ok(())
151 }
152 pub fn rollback(&mut self, strategy_name: &str) {
154 if let Some(mut strategy) = self.strategys.remove(strategy_name) {
156 strategy.rollback(self);
158 } else {
159 println!("策略 {} 不存在", strategy_name)
160 }
161 }
162}
163
164#[cfg(test)]
165mod tests {
166 #[test]
167 fn test_fun() {}
168}