use console_prompt::{Command, command_loop, DynamicContext};
use std::error::Error;
use std::any::Any;
fn yes(_args: &[&str], _context: Option<&Box<dyn Any>>)->Result<String, Box<dyn Error>>{
Ok(format!("You responded with a yes"))
}
fn no(_args: &[&str], _context: Option<&Box<dyn Any>>)->Result<String, Box<dyn Error>>{
Ok(format!("You responded with a no"))
}
fn change(_args: &[&str], context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
match context.get_mut::<String>() {
Some(mut_ref) => {
*mut_ref = _args[0].to_string();
return Ok(format!("you changed their name to {}", *mut_ref));
},
None => return Ok("you are not in a conversation with a person".to_string()),
}
}
fn hello(_args: &[&str], context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
match context.get::<String>() {
None => {
return Ok("you are not in a conversation with a person".to_string())
},
Some(name ) => {
return Ok(format!("You said hello to {name} I guess"));
}
}
}
fn converse(args: &[&str], _context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
if args.len() == 0 {
return Ok("no name provided".to_string());
}
println!("interacting with: {}", args[0]);
let commands = vec![
Command{command: "hello", func: hello, help_output: "hello - say hello"},
Command{command: "change", func: change, help_output: "change <name> - change the name of the person with whom you're speaking"},
];
let mut context = DynamicContext::new();
context.set(args[0].to_string());
if let Err(e) = command_loop(&commands, &mut context){
eprintln!("error running interact command loop: {}", e);
}
Ok(String::new())
}
fn main(){
let commands = vec![
Command{
command: "converse",
func: converse,
help_output: "converse <name> - interact with a person"
},
];
if let Err(e) = command_loop(&commands, &mut DynamicContext::new()){
eprintln!("error running command loop: {}", e.to_string());
}
}