Restore option-saving

This commit is contained in:
greg 2019-05-14 00:40:38 -07:00
parent 4f972f20a7
commit c68c23ed68
4 changed files with 64 additions and 57 deletions

View File

@ -12,9 +12,9 @@ getopts = "0.2.18"
lazy_static = "0.2.8" lazy_static = "0.2.8"
maplit = "*" maplit = "*"
colored = "1.7" colored = "1.7"
serde = "1.0.15" serde = "1.0.91"
serde_derive = "1.0.15" serde_derive = "1.0.91"
serde_json = "1.0.3" serde_json = "1.0.15"
rocket = "0.4.0" rocket = "0.4.0"
rocket_contrib = "0.4.0" rocket_contrib = "0.4.0"
phf = "0.7.12" phf = "0.7.12"

View File

@ -1,3 +1,4 @@
use serde::{Serialize, Deserialize};
use std::time; use std::time;
pub trait ProgrammingLanguageInterface { pub trait ProgrammingLanguageInterface {
@ -32,7 +33,7 @@ pub struct DebugRequest {
pub ask: DebugAsk, pub ask: DebugAsk,
} }
#[derive(Debug, Clone, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Hash, Eq, PartialEq, Deserialize, Serialize)]
pub enum DebugAsk { pub enum DebugAsk {
Timing, Timing,
ByStage { stage_name: String }, ByStage { stage_name: String },

View File

@ -13,6 +13,8 @@ DebugRequest, DebugAsk, DebugResponse};
mod command_tree; mod command_tree;
use self::command_tree::{CommandTree, BoxedCommandFunction}; use self::command_tree::{CommandTree, BoxedCommandFunction};
mod repl_options;
use repl_options::SaveOptions;
const HISTORY_SAVE_FILE: &'static str = ".schala_history"; const HISTORY_SAVE_FILE: &'static str = ".schala_history";
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl"; const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
@ -29,26 +31,34 @@ impl Repl {
use linefeed::Interface; use linefeed::Interface;
let line_reader = Interface::new("schala-repl").unwrap(); let line_reader = Interface::new("schala-repl").unwrap();
let interpreter_directive_sigil = ':'; let interpreter_directive_sigil = ':';
let debug_asks = HashSet::new();
Repl { 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) { pub fn run_repl(&mut self) {
self.line_reader.load_history(HISTORY_SAVE_FILE).unwrap_or(());
println!("Schala MetaInterpreter version {}", crate::VERSION_STRING); println!("Schala MetaInterpreter version {}", crate::VERSION_STRING);
println!("Type {}help for help with the REPL", self.interpreter_directive_sigil); println!("Type {}help for help with the REPL", self.interpreter_directive_sigil);
self.load_options();
self.handle_repl_loop(); self.handle_repl_loop();
self.save_before_exit(); self.save_before_exit();
println!("Exiting..."); 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) { fn handle_repl_loop(&mut self) {
use linefeed::ReadResult::*; use linefeed::ReadResult::*;
@ -83,20 +93,10 @@ impl Repl {
fn save_before_exit(&self) { 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(());
/* let options = SaveOptions {
self.save_options() { debug_asks: self.debug_asks.clone()
let ref options = self.options; };
let read = File::create(OPTIONS_SAVE_FILE) options.save_to_file(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);
}
}
*/
} }
fn get_function_from_directives<'a>(directives: &'a CommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> { 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 { struct TabCompleteHandler {
sigil: char, sigil: char,
top_level_commands: CommandTree, top_level_commands: CommandTree,

View File

@ -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<DebugAsk>
}
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<SaveOptions, ()> {
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(|_| ())
}
}