From 01b7b60dc260aa7cf1dab6c15db13a88d5d92ada Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Fri, 10 Oct 2025 00:55:51 -0700 Subject: basic structure of the interactive features of the CLI tool Change-Id: Ifec5dbbe20b2c625d448eba436620622af6c5120 --- src/commands.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/commands.rs (limited to 'src/commands.rs') 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 { + name: String, + pub implementation: fn(mode: Box, params: &[&str]) + -> Result>>, +} + + +pub struct CommandMap(HashMap>); + +impl CommandMap { + pub fn new() -> CommandMap { + CommandMap(HashMap::new()) + } + + pub fn get(self: &Self, name: &str) -> Option<&Command> { + self.0.get(name) + } + + pub fn insert(self: &mut Self, command: Command) + -> 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, params: &[&str]) + -> Result>>; +} + +impl Command { + pub fn new(name: impl Deref, + implementation: fn(mode: Box, params: &[&str]) + -> Result>>) + -> Self + { + Command { + name: name.to_string(), + implementation: implementation, + } + } +} + -- cgit 1.4.1