blob: 56cc853a1454f3ca5b2a366583430e1d04d68219 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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,
}
}
}
|