Clippy on schala-repl

This commit is contained in:
Greg Shuflin 2021-10-14 02:08:32 -07:00
parent 5dcfce46cc
commit 6ac0628265
6 changed files with 22 additions and 110 deletions

View File

@ -89,7 +89,7 @@ impl CommandTree {
pub fn perform<L: ProgrammingLanguageInterface>( pub fn perform<L: ProgrammingLanguageInterface>(
&self, &self,
repl: &mut Repl<L>, repl: &mut Repl<L>,
arguments: &Vec<&str>, arguments: &[&str],
) -> InterpreterDirectiveOutput { ) -> InterpreterDirectiveOutput {
let mut dir_pointer: &CommandTree = self; let mut dir_pointer: &CommandTree = self;
let mut idx = 0; let mut idx = 0;
@ -103,7 +103,7 @@ impl CommandTree {
} => { } => {
let next_command = match arguments.get(idx) { let next_command = match arguments.get(idx) {
Some(cmd) => cmd, Some(cmd) => cmd,
None => break Err(format!("Command requires arguments")), None => break Err("Command requires arguments".to_owned()),
}; };
idx += 1; idx += 1;
match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) { match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) {

View File

@ -1,6 +1,6 @@
use crate::help::help; use crate::help::help;
use crate::language::{ use crate::language::{
DebugAsk, DebugResponse, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface, LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
}; };
use crate::{InterpreterDirectiveOutput, Repl}; use crate::{InterpreterDirectiveOutput, Repl};
use std::fmt::Write as FmtWrite; use std::fmt::Write as FmtWrite;
@ -11,9 +11,6 @@ pub enum DirectiveAction {
Help, Help,
QuitProgram, QuitProgram,
ListPasses, ListPasses,
ShowImmediate,
Show,
Hide,
TotalTimeOff, TotalTimeOff,
TotalTimeOn, TotalTimeOn,
StageTimeOff, StageTimeOff,
@ -45,7 +42,7 @@ impl DirectiveAction {
}; };
let mut buf = String::new(); let mut buf = String::new();
for pass in pass_names.iter().map(|name| Some(name)).intersperse(None) { for pass in pass_names.iter().map(Some).intersperse(None) {
match pass { match pass {
Some(pass) => write!(buf, "{}", pass).unwrap(), Some(pass) => write!(buf, "{}", pass).unwrap(),
None => write!(buf, " -> ").unwrap(), None => write!(buf, " -> ").unwrap(),
@ -53,60 +50,6 @@ impl DirectiveAction {
} }
Some(buf) Some(buf)
} }
ShowImmediate => {
let stage_name = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a thing to debug")),
};
let meta = LangMetaRequest::ImmediateDebug(DebugAsk::ByStage {
stage_name: stage_name.clone(),
token: None,
});
let meta_response = repl.language_state.request_meta(meta);
let response = match meta_response {
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => match ask {
DebugAsk::ByStage {
stage_name: ref this_stage_name,
..
} if *this_stage_name == stage_name => value,
_ => return Some(format!("Wrong debug stage")),
},
_ => return Some(format!("Invalid language meta response")),
};
Some(response)
}
Show => {
let this_stage_name = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to show")),
};
let token = arguments.get(1).map(|s| s.to_string());
repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == this_stage_name => false,
_ => true,
});
let ask = DebugAsk::ByStage {
stage_name: this_stage_name,
token,
};
repl.options.debug_asks.insert(ask);
None
}
Hide => {
let stage_name_to_remove = match arguments.get(0) {
Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to hide")),
};
repl.options.debug_asks.retain(|ask| match ask {
DebugAsk::ByStage { stage_name, .. } if *stage_name == stage_name_to_remove => {
false
}
_ => true,
});
None
}
TotalTimeOff => { TotalTimeOff => {
repl.options.show_total_time = false; repl.options.show_total_time = false;
None None
@ -139,8 +82,8 @@ fn doc<L: ProgrammingLanguageInterface>(
let meta = LangMetaRequest::Docs { source }; let meta = LangMetaRequest::Docs { source };
match repl.language_state.request_meta(meta) { match repl.language_state.request_meta(meta) {
LangMetaResponse::Docs { doc_string } => Some(doc_string), LangMetaResponse::Docs { doc_string } => Some(doc_string),
_ => Some(format!("Invalid doc response")), _ => Some("Invalid doc response".to_owned()),
} }
}) })
.unwrap_or(Some(format!(":docs needs an argument"))) .unwrap_or_else(|| Some(":docs needs an argument".to_owned()))
} }

View File

@ -1,7 +1,7 @@
use crate::command_tree::CommandTree; use crate::command_tree::CommandTree;
use crate::directive_actions::DirectiveAction; use crate::directive_actions::DirectiveAction;
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree { pub fn directives_from_pass_names(pass_names: &[String]) -> CommandTree {
let passes_directives: Vec<CommandTree> = pass_names let passes_directives: Vec<CommandTree> = pass_names
.iter() .iter()
.map(|pass_name| { .map(|pass_name| {
@ -23,7 +23,7 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
CommandTree::Top(get_list(&passes_directives, true)) CommandTree::Top(get_list(&passes_directives, true))
} }
fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<CommandTree> { fn get_list(passes_directives: &[CommandTree], include_help: bool) -> Vec<CommandTree> {
use DirectiveAction::*; use DirectiveAction::*;
vec![ vec![
@ -50,24 +50,6 @@ fn get_list(passes_directives: &Vec<CommandTree>, include_help: bool) -> Vec<Com
vec![], vec![],
ListPasses, ListPasses,
), ),
CommandTree::terminal(
"show-immediate",
None,
passes_directives.clone(),
ShowImmediate,
),
CommandTree::terminal(
"show",
Some("Show debug output for a specific pass"),
passes_directives.clone(),
Show,
),
CommandTree::terminal(
"hide",
Some("Hide debug output for a specific pass"),
passes_directives.clone(),
Hide,
),
CommandTree::nonterm( CommandTree::nonterm(
"total-time", "total-time",
None, None,

View File

@ -10,7 +10,7 @@ pub fn help<L: ProgrammingLanguageInterface>(
arguments: &[&str], arguments: &[&str],
) -> InterpreterDirectiveOutput { ) -> InterpreterDirectiveOutput {
match arguments { match arguments {
[] => return global_help(repl), [] => global_help(repl),
commands => { commands => {
let dirs = repl.get_directives(); let dirs = repl.get_directives();
Some(match get_directive_from_commands(commands, &dirs) { Some(match get_directive_from_commands(commands, &dirs) {
@ -73,7 +73,7 @@ fn global_help<L: ProgrammingLanguageInterface>(repl: &mut Repl<L>) -> Interpret
.unwrap(); .unwrap();
} }
writeln!(buf, "").unwrap(); writeln!(buf).unwrap();
writeln!( writeln!(
buf, buf,
"Language-specific help for {}", "Language-specific help for {}",

View File

@ -10,7 +10,7 @@ pub trait ProgrammingLanguageInterface {
fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse { fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse {
LangMetaResponse::Custom { LangMetaResponse::Custom {
kind: format!("not-implemented"), kind: "not-implemented".to_owned(),
value: format!(""), value: format!(""),
} }
} }
@ -50,15 +50,6 @@ pub enum DebugAsk {
}, },
} }
impl DebugAsk {
pub fn is_for_stage(&self, name: &str) -> bool {
match self {
DebugAsk::ByStage { stage_name, .. } if stage_name == name => true,
_ => false,
}
}
}
pub struct DebugResponse { pub struct DebugResponse {
pub ask: DebugAsk, pub ask: DebugAsk,
pub value: String, pub value: String,

View File

@ -29,10 +29,10 @@ pub use language::{
}; };
include!(concat!(env!("OUT_DIR"), "/static.rs")); include!(concat!(env!("OUT_DIR"), "/static.rs"));
const VERSION_STRING: &'static str = "0.1.0"; const VERSION_STRING: &str = "0.1.0";
const HISTORY_SAVE_FILE: &'static str = ".schala_history"; const HISTORY_SAVE_FILE: &str = ".schala_history";
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl"; const OPTIONS_SAVE_FILE: &str = ".schala_repl";
type InterpreterDirectiveOutput = Option<String>; type InterpreterDirectiveOutput = Option<String>;
@ -112,7 +112,7 @@ impl<L: ProgrammingLanguageInterface> Repl<L> {
self.line_reader.add_history_unique(input.to_string()); self.line_reader.add_history_unique(input.to_string());
let mut chars = input.chars().peekable(); let mut chars = input.chars().peekable();
let repl_responses = match chars.nth(0) { let repl_responses = match chars.next() {
Some(ch) if ch == self.sigil => { Some(ch) if ch == self.sigil => {
if chars.peek() == Some(&'{') { if chars.peek() == Some(&'{') {
let mut buf = String::new(); let mut buf = String::new();
@ -125,14 +125,13 @@ impl<L: ProgrammingLanguageInterface> Repl<L> {
break 'multiline; break 'multiline;
} else { } else {
buf.push_str(new_input); buf.push_str(new_input);
buf.push_str("\n"); buf.push('\n');
} }
} }
self.handle_input(&buf) self.handle_input(&buf)
} else { } else {
match self.handle_interpreter_directive(input.get(1..).unwrap()) { if let Some(output) = self.handle_interpreter_directive(input.get(1..).unwrap()) {
Some(directive_output) => println!("{}", directive_output), println!("{}", output);
None => (),
} }
continue; continue;
} }
@ -159,7 +158,7 @@ impl<L: ProgrammingLanguageInterface> Repl<L> {
PromptStyle::Multiline => ">| ", PromptStyle::Multiline => ">| ",
}; };
self.line_reader.set_prompt(&prompt_str).unwrap(); self.line_reader.set_prompt(prompt_str).unwrap();
} }
fn save_before_exit(&self) { fn save_before_exit(&self) {
@ -172,7 +171,7 @@ impl<L: ProgrammingLanguageInterface> Repl<L> {
fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput { fn handle_interpreter_directive(&mut self, input: &str) -> InterpreterDirectiveOutput {
let arguments: Vec<&str> = input.split_whitespace().collect(); let arguments: Vec<&str> = input.split_whitespace().collect();
if arguments.len() < 1 { if arguments.is_empty() {
return None; return None;
} }
@ -245,14 +244,11 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
loop { loop {
match words.next() { match words.next() {
None => { None => {
let top = match command_tree { let top = matches!(command_tree, Some(CommandTree::Top(_)));
Some(CommandTree::Top(_)) => true,
_ => false,
};
let word = if top { word.get(1..).unwrap() } else { word }; let word = if top { word.get(1..).unwrap() } else { word };
for cmd in command_tree for cmd in command_tree
.map(|x| x.get_subcommands()) .map(|x| x.get_subcommands())
.unwrap_or(vec![]) .unwrap_or_default()
.into_iter() .into_iter()
{ {
if cmd.starts_with(word) { if cmd.starts_with(word) {