use crate::help::help; use crate::language::{ LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface, }; use crate::{InterpreterDirectiveOutput, Repl}; use std::fmt::Write as FmtWrite; #[derive(Debug, Clone)] pub enum DirectiveAction { Null, Help, QuitProgram, ListPasses, TotalTimeOff, TotalTimeOn, StageTimeOff, StageTimeOn, Doc, } impl DirectiveAction { pub fn perform( &self, repl: &mut Repl, arguments: &[&str], ) -> InterpreterDirectiveOutput { use DirectiveAction::*; match self { Null => None, Help => help(repl, arguments), QuitProgram => { repl.save_before_exit(); ::std::process::exit(0) } ListPasses => { let pass_names = match repl .language_state .request_meta(LangMetaRequest::StageNames) { LangMetaResponse::StageNames(names) => names, _ => vec![], }; let mut buf = String::new(); for pass in pass_names.iter().map(Some).intersperse(None) { match pass { Some(pass) => write!(buf, "{}", pass).unwrap(), None => write!(buf, " -> ").unwrap(), } } Some(buf) } TotalTimeOff => { repl.options.show_total_time = false; None } TotalTimeOn => { repl.options.show_total_time = true; None } StageTimeOff => { repl.options.show_stage_times = false; None } StageTimeOn => { repl.options.show_stage_times = true; None } Doc => doc(repl, arguments), } } } fn doc( repl: &mut Repl, arguments: &[&str], ) -> InterpreterDirectiveOutput { arguments .get(0) .map(|cmd| { let source = cmd.to_string(); let meta = LangMetaRequest::Docs { source }; match repl.language_state.request_meta(meta) { LangMetaResponse::Docs { doc_string } => Some(doc_string), _ => Some("Invalid doc response".to_owned()), } }) .unwrap_or_else(|| Some(":docs needs an argument".to_owned())) }