golem_rib_repl/command/
registry.rs

1use crate::command::builtin::{Clear, Exports, TypeInfo};
2use crate::UntypedCommand;
3use std::collections::HashMap;
4use std::sync::Arc;
5
6#[derive(Default)]
7pub struct CommandRegistry {
8    commands: HashMap<String, Arc<dyn UntypedCommand>>,
9}
10
11impl CommandRegistry {
12    pub(crate) fn built_in() -> Self {
13        let mut registry = Self::default();
14        registry.register(TypeInfo);
15        registry.register(Clear);
16        registry.register(Exports);
17        registry
18    }
19
20    pub fn merge(&mut self, other: CommandRegistry) {
21        for (name, command) in other.commands {
22            self.commands.insert(name, command);
23        }
24    }
25
26    pub fn get_commands(&self) -> Vec<String> {
27        self.commands.keys().map(|name| name.to_string()).collect()
28    }
29
30    pub fn register<T>(&mut self, command: T)
31    where
32        T: UntypedCommand + 'static,
33    {
34        let name = command.command_name().to_string();
35        self.commands.insert(name, Arc::new(command));
36    }
37
38    pub fn get_command(&self, name: &str) -> Option<Arc<dyn UntypedCommand>> {
39        let result = self.commands.get(name);
40        match result {
41            Some(command) => Some(command.clone()),
42            None => None,
43        }
44    }
45}