From 1470e7fbdde2d0695ab7ff1a5f0c8c390b43d402 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 24 Nov 2021 23:51:30 -0800 Subject: [PATCH] Run cargo fmt --- schala-repl/build.rs | 5 +- schala-repl/src/command_tree.rs | 33 ++++-------- schala-repl/src/directive_actions.rs | 16 +++--- schala-repl/src/directives.rs | 16 ++---- schala-repl/src/help.rs | 44 ++++------------ schala-repl/src/language.rs | 13 ++--- schala-repl/src/lib.rs | 79 +++++++++------------------- schala-repl/src/repl_options.rs | 16 +++--- schala-repl/src/response.rs | 24 ++++----- src/main.rs | 41 +++++++-------- 10 files changed, 94 insertions(+), 193 deletions(-) diff --git a/schala-repl/build.rs b/schala-repl/build.rs index a6395ef..e14cb05 100644 --- a/schala-repl/build.rs +++ b/schala-repl/build.rs @@ -3,8 +3,5 @@ extern crate includedir_codegen; use includedir_codegen::Compression; fn main() { - includedir_codegen::start("WEBFILES") - .dir("../static", Compression::Gzip) - .build("static.rs") - .unwrap(); + includedir_codegen::start("WEBFILES").dir("../static", Compression::Gzip).build("static.rs").unwrap(); } diff --git a/schala-repl/src/command_tree.rs b/schala-repl/src/command_tree.rs index 9d94f6e..6592d97 100644 --- a/schala-repl/src/command_tree.rs +++ b/schala-repl/src/command_tree.rs @@ -1,8 +1,10 @@ -use crate::directive_actions::DirectiveAction; -use crate::language::ProgrammingLanguageInterface; -use crate::{InterpreterDirectiveOutput, Repl}; use colored::*; +use crate::{ + directive_actions::DirectiveAction, language::ProgrammingLanguageInterface, InterpreterDirectiveOutput, + Repl, +}; + /// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first /// Terminal, it will use the `DirectiveAction` found there to find an appropriate function to execute, /// and then execute it with any remaining arguments @@ -39,12 +41,7 @@ impl CommandTree { children: Vec, action: DirectiveAction, ) -> CommandTree { - CommandTree::Terminal { - name: s.to_string(), - help_msg: help.map(|x| x.to_string()), - children, - action, - } + CommandTree::Terminal { name: s.to_string(), help_msg: help.map(|x| x.to_string()), children, action } } pub fn nonterm(s: &str, help: Option<&str>, children: Vec) -> CommandTree { @@ -65,14 +62,10 @@ impl CommandTree { } pub fn get_help(&self) -> &str { match self { - CommandTree::Terminal { help_msg, .. } => help_msg - .as_ref() - .map(|s| s.as_str()) - .unwrap_or(""), - CommandTree::NonTerminal { help_msg, .. } => help_msg - .as_ref() - .map(|s| s.as_str()) - .unwrap_or(""), + CommandTree::Terminal { help_msg, .. } => + help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""), + CommandTree::NonTerminal { help_msg, .. } => + help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""), CommandTree::Top(_) => "", } } @@ -96,11 +89,7 @@ impl CommandTree { let res: Result<(DirectiveAction, usize), String> = loop { match dir_pointer { - CommandTree::Top(subcommands) - | CommandTree::NonTerminal { - children: subcommands, - .. - } => { + CommandTree::Top(subcommands) | CommandTree::NonTerminal { children: subcommands, .. } => { let next_command = match arguments.get(idx) { Some(cmd) => cmd, None => break Err("Command requires arguments".to_owned()), diff --git a/schala-repl/src/directive_actions.rs b/schala-repl/src/directive_actions.rs index 3175a17..46cf39b 100644 --- a/schala-repl/src/directive_actions.rs +++ b/schala-repl/src/directive_actions.rs @@ -1,10 +1,11 @@ -use crate::help::help; -use crate::language::{ - LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface, -}; -use crate::{InterpreterDirectiveOutput, Repl}; use std::fmt::Write as FmtWrite; +use crate::{ + help::help, + language::{LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface}, + InterpreterDirectiveOutput, Repl, +}; + #[derive(Debug, Clone)] pub enum DirectiveAction { Null, @@ -31,10 +32,7 @@ impl DirectiveAction { ::std::process::exit(0) } ListPasses => { - let pass_names = match repl - .language_state - .request_meta(LangMetaRequest::StageNames) - { + let pass_names = match repl.language_state.request_meta(LangMetaRequest::StageNames) { LangMetaResponse::StageNames(names) => names, _ => vec![], }; diff --git a/schala-repl/src/directives.rs b/schala-repl/src/directives.rs index 6dc7216..f01847c 100644 --- a/schala-repl/src/directives.rs +++ b/schala-repl/src/directives.rs @@ -1,5 +1,4 @@ -use crate::command_tree::CommandTree; -use crate::directive_actions::DirectiveAction; +use crate::{command_tree::CommandTree, directive_actions::DirectiveAction}; pub fn directives_from_pass_names(pass_names: &[String]) -> CommandTree { let passes_directives: Vec = pass_names @@ -33,11 +32,7 @@ fn get_list(passes_directives: &[CommandTree], include_help: bool) -> Vec Vec( repl: &mut Repl, arguments: &[&str], @@ -21,8 +22,7 @@ pub fn help( let children = dir.get_children(); writeln!(buf, "`{}` - {}", cmd, dir.get_help()).unwrap(); for sub in children.iter() { - writeln!(buf, "\t`{} {}` - {}", cmd, sub.get_cmd(), sub.get_help()) - .unwrap(); + writeln!(buf, "\t`{} {}` - {}", cmd, sub.get_cmd(), sub.get_help()).unwrap(); } buf } @@ -31,16 +31,11 @@ pub fn help( } } -fn get_directive_from_commands<'a>( - commands: &[&str], - dirs: &'a CommandTree, -) -> Option<&'a CommandTree> { +fn get_directive_from_commands<'a>(commands: &[&str], dirs: &'a CommandTree) -> Option<&'a CommandTree> { let mut directive_list = dirs.get_children(); let mut matched_directive = None; for cmd in commands { - let found = directive_list - .iter() - .find(|directive| directive.get_cmd() == *cmd); + let found = directive_list.iter().find(|directive| directive.get_cmd() == *cmd); if let Some(dir) = found { directive_list = dir.get_children(); } @@ -53,33 +48,16 @@ fn get_directive_from_commands<'a>( fn global_help(repl: &mut Repl) -> InterpreterDirectiveOutput { let mut buf = String::new(); - writeln!( - buf, - "{} version {}", - "Schala REPL".bright_red().bold(), - crate::VERSION_STRING - ) - .unwrap(); + writeln!(buf, "{} version {}", "Schala REPL".bright_red().bold(), crate::VERSION_STRING).unwrap(); writeln!(buf, "-----------------------").unwrap(); for directive in repl.get_directives().get_children() { - writeln!( - buf, - "{}{} - {}", - repl.sigil, - directive.get_cmd(), - directive.get_help() - ) - .unwrap(); + writeln!(buf, "{}{} - {}", repl.sigil, directive.get_cmd(), directive.get_help()).unwrap(); } writeln!(buf).unwrap(); - writeln!( - buf, - "Language-specific help for {}", - ::language_name() - ) - .unwrap(); + writeln!(buf, "Language-specific help for {}", ::language_name()) + .unwrap(); writeln!(buf, "-----------------------").unwrap(); Some(buf) } diff --git a/schala-repl/src/language.rs b/schala-repl/src/language.rs index eb11629..7c588ac 100644 --- a/schala-repl/src/language.rs +++ b/schala-repl/src/language.rs @@ -1,5 +1,4 @@ -use std::collections::HashSet; -use std::time; +use std::{collections::HashSet, time}; pub trait ProgrammingLanguageInterface { type Config: Default + Clone; @@ -9,10 +8,7 @@ pub trait ProgrammingLanguageInterface { fn run_computation(&mut self, _request: ComputationRequest) -> ComputationResponse; fn request_meta(&mut self, _request: LangMetaRequest) -> LangMetaResponse { - LangMetaResponse::Custom { - kind: "not-implemented".to_owned(), - value: format!(""), - } + LangMetaResponse::Custom { kind: "not-implemented".to_owned(), value: format!("") } } } @@ -37,10 +33,7 @@ pub struct GlobalOutputStats { #[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)] pub enum DebugAsk { Timing, - ByStage { - stage_name: String, - token: Option, - }, + ByStage { stage_name: String, token: Option }, } pub struct DebugResponse { diff --git a/schala-repl/src/lib.rs b/schala-repl/src/lib.rs index 72cddb0..8cba074 100644 --- a/schala-repl/src/lib.rs +++ b/schala-repl/src/lib.rs @@ -17,16 +17,14 @@ mod directives; use directives::directives_from_pass_names; mod help; mod response; -use response::ReplResponse; +use std::{collections::HashSet, sync::Arc}; use colored::*; -use std::collections::HashSet; -use std::sync::Arc; - pub use language::{ - ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats, - LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface, + ComputationRequest, ComputationResponse, DebugAsk, DebugResponse, GlobalOutputStats, LangMetaRequest, + LangMetaResponse, ProgrammingLanguageInterface, }; +use response::ReplResponse; include!(concat!(env!("OUT_DIR"), "/static.rs")); const VERSION_STRING: &str = "0.1.0"; @@ -58,20 +56,12 @@ impl Repl { let line_reader = Interface::new("schala-repl").unwrap(); let sigil = ':'; - Repl { - sigil, - line_reader, - language_state: initial_state, - options: ReplOptions::new(), - } + Repl { sigil, line_reader, language_state: initial_state, options: ReplOptions::new() } } pub fn run_repl(&mut self, config: L::Config) { println!("Schala meta-interpeter version {}", VERSION_STRING); - println!( - "Type {} for help with the REPL", - format!("{}help", self.sigil).bright_green().bold() - ); + println!("Type {} for help with the REPL", format!("{}help", self.sigil).bright_green().bold()); self.load_options(); self.handle_repl_loop(config); self.save_before_exit(); @@ -79,9 +69,7 @@ impl Repl { } fn load_options(&mut self) { - self.line_reader - .load_history(HISTORY_SAVE_FILE) - .unwrap_or(()); + self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(()); match ReplOptions::load_from_file(OPTIONS_SAVE_FILE) { Ok(options) => { self.options = options; @@ -113,7 +101,7 @@ impl Repl { self.line_reader.add_history_unique(input.to_string()); let mut chars = input.chars().peekable(); let repl_responses = match chars.next() { - Some(ch) if ch == self.sigil => { + Some(ch) if ch == self.sigil => if chars.peek() == Some(&'{') { let mut buf = String::new(); buf.push_str(input.get(2..).unwrap()); @@ -130,12 +118,11 @@ impl Repl { } self.handle_input(&buf, &config) } else { - if let Some(output) = self.handle_interpreter_directive(input.get(1..).unwrap()) { - println!("{}", output); + if let Some(output) = self.handle_interpreter_directive(input.get(1..).unwrap()) { + println!("{}", output); } continue; - } - } + }, _ => self.handle_input(input, &config), }; @@ -147,8 +134,7 @@ impl Repl { fn update_line_reader(&mut self) { let tab_complete_handler = TabCompleteHandler::new(self.sigil, self.get_directives()); - self.line_reader - .set_completer(Arc::new(tab_complete_handler)); //TODO fix this here + self.line_reader.set_completer(Arc::new(tab_complete_handler)); //TODO fix this here self.set_prompt(PromptStyle::Normal); } @@ -162,9 +148,7 @@ impl Repl { } fn save_before_exit(&self) { - self.line_reader - .save_history(HISTORY_SAVE_FILE) - .unwrap_or(()); + self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(()); self.options.save_to_file(OPTIONS_SAVE_FILE); } @@ -185,20 +169,13 @@ impl Repl { debug_requests.insert(ask.clone()); } - let request = ComputationRequest { - source: input, - config: config.clone(), - debug_requests, - }; + let request = ComputationRequest { source: input, config: config.clone(), debug_requests }; let response = self.language_state.run_computation(request); response::handle_computation_response(response, &self.options) } fn get_directives(&mut self) -> CommandTree { - let pass_names = match self - .language_state - .request_meta(LangMetaRequest::StageNames) - { + let pass_names = match self.language_state.request_meta(LangMetaRequest::StageNames) { LangMetaResponse::StageNames(names) => names, _ => vec![], }; @@ -212,15 +189,14 @@ struct TabCompleteHandler { top_level_commands: CommandTree, } -use linefeed::complete::{Completer, Completion}; -use linefeed::terminal::Terminal; +use linefeed::{ + complete::{Completer, Completion}, + terminal::Terminal, +}; impl TabCompleteHandler { fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler { - TabCompleteHandler { - top_level_commands, - sigil, - } + TabCompleteHandler { top_level_commands, sigil } } } @@ -247,11 +223,7 @@ impl Completer for TabCompleteHandler { None => { let top = matches!(command_tree, Some(CommandTree::Top(_))); let word = if top { word.get(1..).unwrap() } else { word }; - for cmd in command_tree - .map(|x| x.get_subcommands()) - .unwrap_or_default() - .into_iter() - { + for cmd in command_tree.map(|x| x.get_subcommands()).unwrap_or_default().into_iter() { if cmd.starts_with(word) { completions.push(Completion { completion: format!("{}{}", if top { ":" } else { "" }, cmd), @@ -265,12 +237,9 @@ impl Completer for TabCompleteHandler { Some(s) => { let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm { CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s), - CommandTree::NonTerminal { children, .. } => { - children.iter().find(|c| c.get_cmd() == s) - } - CommandTree::Terminal { children, .. } => { - children.iter().find(|c| c.get_cmd() == s) - } + CommandTree::NonTerminal { children, .. } => + children.iter().find(|c| c.get_cmd() == s), + CommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), }); command_tree = new_ptr; } diff --git a/schala-repl/src/repl_options.rs b/schala-repl/src/repl_options.rs index e1f20fd..c40a1c4 100644 --- a/schala-repl/src/repl_options.rs +++ b/schala-repl/src/repl_options.rs @@ -1,8 +1,10 @@ -use crate::language::DebugAsk; +use std::{ + collections::HashSet, + fs::File, + io::{self, Read, Write}, +}; -use std::collections::HashSet; -use std::fs::File; -use std::io::{self, Read, Write}; +use crate::language::DebugAsk; #[derive(Serialize, Deserialize)] pub struct ReplOptions { @@ -13,11 +15,7 @@ pub struct ReplOptions { impl ReplOptions { pub fn new() -> ReplOptions { - ReplOptions { - debug_asks: HashSet::new(), - show_total_time: true, - show_stage_times: false, - } + ReplOptions { debug_asks: HashSet::new(), show_total_time: true, show_stage_times: false } } pub fn save_to_file(&self, filename: &str) { diff --git a/schala-repl/src/response.rs b/schala-repl/src/response.rs index 44ad59d..d68df20 100644 --- a/schala-repl/src/response.rs +++ b/schala-repl/src/response.rs @@ -1,9 +1,11 @@ -use colored::*; -use std::fmt; -use std::fmt::Write; +use std::{fmt, fmt::Write}; -use crate::language::{ComputationResponse, DebugAsk}; -use crate::ReplOptions; +use colored::*; + +use crate::{ + language::{ComputationResponse, DebugAsk}, + ReplOptions, +}; pub struct ReplResponse { label: Option, @@ -64,16 +66,8 @@ pub fn handle_computation_response( } responses.push(match response.main_output { - Ok(s) => ReplResponse { - label: None, - text: s, - color: None, - }, - Err(e) => ReplResponse { - label: Some("Error".to_string()), - text: e, - color: Some(Color::Red), - }, + Ok(s) => ReplResponse { label: None, text: s, color: None }, + Err(e) => ReplResponse { label: Some("Error".to_string()), text: e, color: Some(Color::Red) }, }); responses diff --git a/src/main.rs b/src/main.rs index 6caa217..1e4aadb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,15 @@ -use schala_repl::{Repl, ProgrammingLanguageInterface, ComputationRequest}; +use std::{collections::HashSet, fs::File, io::Read, path::PathBuf, process::exit}; -use std::{fs::File, io::Read, path::PathBuf, process::exit, collections::HashSet}; use schala_lang::{Schala, SchalaConfig}; +use schala_repl::{ComputationRequest, ProgrammingLanguageInterface, Repl}; //TODO specify multiple langs, and have a way to switch between them fn main() { let args: Vec = std::env::args().collect(); - let matches = command_line_options() - .parse(&args[1..]) - .unwrap_or_else(|e| { - eprintln!("Error parsing options: {}", e); - exit(1); - }); + let matches = command_line_options().parse(&args[1..]).unwrap_or_else(|e| { + eprintln!("Error parsing options: {}", e); + exit(1); + }); if matches.opt_present("help") { println!("{}", command_line_options().usage("Schala metainterpreter")); @@ -27,27 +25,28 @@ fn main() { let paths: Vec = matches.free.iter().map(PathBuf::from).collect(); //TODO handle more than one file let filename = &paths[0]; - let extension = filename.extension().and_then(|e| e.to_str()) - .unwrap_or_else(|| { + let extension = filename.extension().and_then(|e| e.to_str()).unwrap_or_else(|| { eprintln!("Source file `{}` has no extension.", filename.display()); exit(1); - }); + }); //TODO this proably should be a macro for every supported language if extension == Schala::source_file_suffix() { - let config = SchalaConfig { - repl: false, - }; + let config = SchalaConfig { repl: false }; - run_noninteractive(paths, Schala::new(), config); + run_noninteractive(paths, Schala::new(), config); } else { - eprintln!("Extension .{} not recognized", extension); - exit(1); + eprintln!("Extension .{} not recognized", extension); + exit(1); } } } -pub fn run_noninteractive(filenames: Vec, mut language: L, config: L::Config) { +pub fn run_noninteractive( + filenames: Vec, + mut language: L, + config: L::Config, +) { // for now, ony do something with the first filename let filename = &filenames[0]; @@ -55,11 +54,7 @@ pub fn run_noninteractive(filenames: Vec