perspt_core/plugin/
registry.rs1use super::*;
2
3pub struct PluginRegistry {
5 plugins: Vec<Box<dyn LanguagePlugin>>,
6}
7
8impl PluginRegistry {
9 pub fn new() -> Self {
11 Self {
12 plugins: vec![
13 Box::new(RustPlugin),
14 Box::new(PythonPlugin),
15 Box::new(JsPlugin),
16 ],
17 }
18 }
19
20 pub fn empty() -> Self {
22 Self {
23 plugins: Vec::new(),
24 }
25 }
26
27 pub fn register(&mut self, plugin: Box<dyn LanguagePlugin>) {
30 self.plugins.push(plugin);
31 }
32
33 pub fn detect(&self, path: &Path) -> Option<&dyn LanguagePlugin> {
35 self.plugins
36 .iter()
37 .find(|p| p.detect(path))
38 .map(|p| p.as_ref())
39 }
40
41 pub fn detect_all(&self, path: &Path) -> Vec<&dyn LanguagePlugin> {
46 self.plugins
47 .iter()
48 .filter(|p| p.detect(path))
49 .map(|p| p.as_ref())
50 .collect()
51 }
52
53 pub fn get(&self, name: &str) -> Option<&dyn LanguagePlugin> {
55 self.plugins
56 .iter()
57 .find(|p| p.name() == name)
58 .map(|p| p.as_ref())
59 }
60
61 pub fn all(&self) -> &[Box<dyn LanguagePlugin>] {
63 &self.plugins
64 }
65}
66
67impl Default for PluginRegistry {
68 fn default() -> Self {
69 Self::new()
70 }
71}