feat: add a very basic command registry
This commit is contained in:
parent
e7973db1e7
commit
e2b7f66435
13 changed files with 130 additions and 2 deletions
6
utils/Cargo.toml
Normal file
6
utils/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "boxutils"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
3
utils/src/commands.rs
Normal file
3
utils/src/commands.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub trait Command {
|
||||
fn execute(&self);
|
||||
}
|
||||
2
utils/src/lib.rs
Normal file
2
utils/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod commands;
|
||||
pub mod registry;
|
||||
30
utils/src/registry.rs
Normal file
30
utils/src/registry.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
use super::commands::Command;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct CommandRegistry {
|
||||
commands: HashMap<String, Box<dyn Command>>,
|
||||
}
|
||||
|
||||
impl CommandRegistry {
|
||||
pub fn new() -> Self {
|
||||
CommandRegistry {
|
||||
commands: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(&mut self, name: &str, command: Box<dyn Command>) {
|
||||
self.commands.insert(name.to_string(), command);
|
||||
}
|
||||
|
||||
pub fn get(&self, name: &str) -> Option<&Box<dyn Command>> {
|
||||
self.commands.get(name)
|
||||
}
|
||||
|
||||
pub fn execute(&self, name: &str) {
|
||||
if let Some(command) = self.get(name) {
|
||||
command.execute();
|
||||
} else {
|
||||
println!("Command not found: {}", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue