#![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, } } }