1 use std::{collections::HashMap, sync::Mutex}; 2 3 type HelpMap = HashMap<String, fn() -> ()>; 4 5 macro_rules! help { 6 ($cmd:expr,$func:expr) => { 7 ($cmd.to_string(), $func as fn() -> ()) 8 }; 9 } 10 11 static mut HELP_MAP: Option<Mutex<HelpMap>> = None; 12 13 #[derive(Debug)] 14 pub struct Helper; 15 16 impl Helper { help()17 pub unsafe fn help() { 18 let map = HELP_MAP.as_ref().unwrap().lock().unwrap(); 19 for (name, func) in map.iter() { 20 print!("{name}:",); 21 func(); 22 } 23 } 24 init()25 pub unsafe fn init() { 26 HELP_MAP = Some(Mutex::new(HelpMap::new())); 27 let mut map = HELP_MAP.as_ref().unwrap().lock().unwrap(); 28 29 let mut insert = |tuple: (String, fn() -> ())| map.insert(tuple.0, tuple.1); 30 31 insert(help!("cd", Self::shell_help_cd)); 32 insert(help!("exec", Self::shell_help_exec)); 33 } 34 shell_help_cd()35 fn shell_help_cd() { 36 println!("Usage: cd [directory]"); 37 } 38 shell_help_exec()39 fn shell_help_exec() { 40 println!("exec: exec file"); 41 } 42 } 43