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