From c68c23ed68ab1a7aa4a93f5b422ce0660d871afb Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 14 May 2019 00:40:38 -0700 Subject: [PATCH] Restore option-saving --- schala-repl/Cargo.toml | 6 +-- schala-repl/src/language.rs | 3 +- schala-repl/src/repl/mod.rs | 74 ++++++++-------------------- schala-repl/src/repl/repl_options.rs | 38 ++++++++++++++ 4 files changed, 64 insertions(+), 57 deletions(-) create mode 100644 schala-repl/src/repl/repl_options.rs diff --git a/schala-repl/Cargo.toml b/schala-repl/Cargo.toml index 6c9a10c..451dfac 100644 --- a/schala-repl/Cargo.toml +++ b/schala-repl/Cargo.toml @@ -12,9 +12,9 @@ getopts = "0.2.18" lazy_static = "0.2.8" maplit = "*" colored = "1.7" -serde = "1.0.15" -serde_derive = "1.0.15" -serde_json = "1.0.3" +serde = "1.0.91" +serde_derive = "1.0.91" +serde_json = "1.0.15" rocket = "0.4.0" rocket_contrib = "0.4.0" phf = "0.7.12" diff --git a/schala-repl/src/language.rs b/schala-repl/src/language.rs index 61f90fe..72c3d8b 100644 --- a/schala-repl/src/language.rs +++ b/schala-repl/src/language.rs @@ -1,3 +1,4 @@ +use serde::{Serialize, Deserialize}; use std::time; pub trait ProgrammingLanguageInterface { @@ -32,7 +33,7 @@ pub struct DebugRequest { pub ask: DebugAsk, } -#[derive(Debug, Clone, Hash, Eq, PartialEq)] +#[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)] pub enum DebugAsk { Timing, ByStage { stage_name: String }, diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index e696194..7d9b70a 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -13,6 +13,8 @@ DebugRequest, DebugAsk, DebugResponse}; mod command_tree; use self::command_tree::{CommandTree, BoxedCommandFunction}; +mod repl_options; +use repl_options::SaveOptions; const HISTORY_SAVE_FILE: &'static str = ".schala_history"; const OPTIONS_SAVE_FILE: &'static str = ".schala_repl"; @@ -29,26 +31,34 @@ impl Repl { use linefeed::Interface; let line_reader = Interface::new("schala-repl").unwrap(); let interpreter_directive_sigil = ':'; - let debug_asks = HashSet::new(); Repl { - interpreter_directive_sigil, line_reader, language_states: initial_states, debug_asks + interpreter_directive_sigil, + line_reader, + language_states: initial_states, + debug_asks: HashSet::new(), } } pub fn run_repl(&mut self) { - self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(()); - println!("Schala MetaInterpreter version {}", crate::VERSION_STRING); println!("Type {}help for help with the REPL", self.interpreter_directive_sigil); - + self.load_options(); self.handle_repl_loop(); - self.save_before_exit(); - println!("Exiting..."); } + fn load_options(&mut self) { + self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(()); + match SaveOptions::load_from_file(OPTIONS_SAVE_FILE) { + Ok(options) => { + self.debug_asks = options.debug_asks; + }, + Err(()) => () + }; + } + fn handle_repl_loop(&mut self) { use linefeed::ReadResult::*; @@ -83,20 +93,10 @@ impl Repl { fn save_before_exit(&self) { self.line_reader.save_history(HISTORY_SAVE_FILE).unwrap_or(()); - /* - self.save_options() { - let ref options = self.options; - let read = File::create(OPTIONS_SAVE_FILE) - .and_then(|mut file| { - let buf = ::serde_json::to_string(options).unwrap(); - file.write_all(buf.as_bytes()) - }); - - if let Err(err) = read { - println!("Error saving {} file {}", OPTIONS_SAVE_FILE, err); - } - } - */ + let options = SaveOptions { + debug_asks: self.debug_asks.clone() + }; + options.save_to_file(OPTIONS_SAVE_FILE); } fn get_function_from_directives<'a>(directives: &'a CommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> { @@ -311,38 +311,6 @@ impl Repl { } } -/* --------------------------------------------- */ - - -/* - fn get_options() -> EvalOptions { - File::open(OPTIONS_SAVE_FILE) - .and_then(|mut file| { - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - Ok(contents) - }) - .and_then(|contents| { - let options: EvalOptions = ::serde_json::from_str(&contents)?; - Ok(options) - }).unwrap_or(EvalOptions::default()) - } - - fn save_options(&self) { - let ref options = self.options; - let read = File::create(OPTIONS_SAVE_FILE) - .and_then(|mut file| { - let buf = ::serde_json::to_string(options).unwrap(); - file.write_all(buf.as_bytes()) - }); - - if let Err(err) = read { - println!("Error saving {} file {}", OPTIONS_SAVE_FILE, err); - } - } - */ - - struct TabCompleteHandler { sigil: char, top_level_commands: CommandTree, diff --git a/schala-repl/src/repl/repl_options.rs b/schala-repl/src/repl/repl_options.rs new file mode 100644 index 0000000..1aacc9a --- /dev/null +++ b/schala-repl/src/repl/repl_options.rs @@ -0,0 +1,38 @@ +use serde::{Serialize, Deserialize}; +use crate::language::DebugAsk; + +use std::io::{Read, Write}; +use std::collections::HashSet; +use std::fs::File; + +#[derive(Serialize, Deserialize)] +pub struct SaveOptions { + pub debug_asks: HashSet +} + +impl SaveOptions { + pub fn save_to_file(&self, filename: &str) { + let res = File::create(filename) + .and_then(|mut file| { + let buf = crate::serde_json::to_string(self).unwrap(); + file.write_all(buf.as_bytes()) + }); + if let Err(err) = res { + println!("Error saving {} file {}", filename, err); + } + } + + pub fn load_from_file(filename: &str) -> Result { + File::open(filename) + .and_then(|mut file| { + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) + }) + .and_then(|contents| { + let output: SaveOptions = crate::serde_json::from_str(&contents)?; + Ok(output) + }) + .map_err(|_| ()) + } +}