lash_core/
plugin_stack.rs1use std::sync::Arc;
2
3use crate::PluginFactory;
4
5#[derive(Clone, Default)]
6pub struct PluginStack {
7 factories: Vec<Arc<dyn PluginFactory>>,
8}
9
10impl PluginStack {
11 pub fn new() -> Self {
12 Self::default()
13 }
14
15 pub fn from_factories(factories: impl IntoIterator<Item = Arc<dyn PluginFactory>>) -> Self {
16 Self {
17 factories: factories.into_iter().collect(),
18 }
19 }
20
21 pub fn factories(&self) -> &[Arc<dyn PluginFactory>] {
22 &self.factories
23 }
24
25 pub fn into_factories(self) -> Vec<Arc<dyn PluginFactory>> {
26 self.factories
27 }
28
29 pub fn push(&mut self, plugin: Arc<dyn PluginFactory>) -> &mut Self {
30 self.factories.push(plugin);
31 self
32 }
33
34 pub fn extend(
35 &mut self,
36 plugins: impl IntoIterator<Item = Arc<dyn PluginFactory>>,
37 ) -> &mut Self {
38 self.factories.extend(plugins);
39 self
40 }
41
42 pub fn remove(&mut self, id: &str) -> &mut Self {
43 self.factories.retain(|plugin| plugin.id() != id);
44 self
45 }
46
47 pub fn replace(&mut self, plugin: Arc<dyn PluginFactory>) -> &mut Self {
48 let id = plugin.id();
49 if let Some(slot) = self
50 .factories
51 .iter_mut()
52 .find(|existing| existing.id() == id)
53 {
54 *slot = plugin;
55 } else {
56 self.factories.push(plugin);
57 }
58 self
59 }
60
61 pub fn retain(&mut self, keep: impl FnMut(&Arc<dyn PluginFactory>) -> bool) -> &mut Self {
62 self.factories.retain(keep);
63 self
64 }
65
66 pub fn configure(mut self, configure: impl FnOnce(&mut PluginStack)) -> Self {
67 configure(&mut self);
68 self
69 }
70}