diff options
| author | Irene Knapp <ireneista@internetsafetylabs.org> | 2025-10-10 00:55:51 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@internetsafetylabs.org> | 2025-11-26 14:47:27 -0800 |
| commit | 01b7b60dc260aa7cf1dab6c15db13a88d5d92ada (patch) | |
| tree | 7cb838c4b6c941e6d67e7265438600d2bbcd15cd /src/commands.rs | |
| parent | a0061153aca2bb13b167efd6263a6fa83150e160 (diff) | |
basic structure of the interactive features of the CLI tool
Change-Id: Ifec5dbbe20b2c625d448eba436620622af6c5120
Diffstat (limited to 'src/commands.rs')
| -rw-r--r-- | src/commands.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..56cc853 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,56 @@ +#![forbid(unsafe_code)] +use crate::error::*; + +use std::collections::HashMap; +use std::ops::Deref; + + +pub struct Command<M: Mode + ?Sized> { + name: String, + pub implementation: fn(mode: Box<M>, params: &[&str]) + -> Result<Option<Box<dyn Mode>>>, +} + + +pub struct CommandMap<M: Mode + ?Sized>(HashMap<String, Command<M>>); + +impl<M: Mode> CommandMap<M> { + pub fn new() -> CommandMap<M> { + CommandMap(HashMap::new()) + } + + pub fn get(self: &Self, name: &str) -> Option<&Command<M>> { + self.0.get(name) + } + + pub fn insert(self: &mut Self, command: Command<M>) + -> Result<()> + { + let name = command.name.to_string(); + + let _ = self.0.insert(name, command); + + Ok(()) + } +} + + +pub trait Mode { + fn prompt_text(&self) -> Result<&str>; + fn dispatch(self: Box<Self>, params: &[&str]) + -> Result<Option<Box<dyn Mode>>>; +} + +impl<M: Mode> Command<M> { + pub fn new(name: impl Deref<Target=str>, + implementation: fn(mode: Box<M>, params: &[&str]) + -> Result<Option<Box<dyn Mode>>>) + -> Self + { + Command { + name: name.to_string(), + implementation: implementation, + } + } +} + |