keystore/
keystore.rs

1use repl_framework::Repl;
2
3fn main() -> std::io::Result<()> {
4    Repl::default()
5        // makes command with name "get" which calls Store::get when called
6        .with_function("get", Store::get)
7        .with_function("set", Store::set)
8        // commands with the name "" get called whenever the user inputs a commmand which is not declared
9        .with_function("", Store::unknown_command)
10        .run()
11}
12
13#[derive(Debug, Default)]
14struct Store {
15    data: std::collections::HashMap<String, String>,
16}
17
18impl Store {
19    fn get(&mut self, args: Vec<String>) {
20        args.into_iter()
21            .for_each(|f| println!("{}", &self.data[&f]));
22    }
23    fn set(&mut self, args: Vec<String>) {
24        args.chunks_exact(2).for_each(|f| {
25            self.data.insert(f[0].clone(), f[1].clone());
26        })
27    }
28    fn unknown_command(&mut self, args: Vec<String>) {
29        eprintln!("unknown command {:?}", args.first())
30    }
31}