From e47a2c72416d94c0be9e162f518b902abc3c42e1 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 17 Sep 2017 18:57:47 -0700 Subject: [PATCH] Save REPL config to file --- Cargo.toml | 3 +++ src/language.rs | 2 +- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2383ddd..4537b04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,7 @@ linefeed = "0.2.2" lazy_static = "0.2.8" maplit = "*" colored = "1.5" +serde = "1.0.15" +serde_derive = "1.0.15" +serde_json = "1.0.3" diff --git a/src/language.rs b/src/language.rs index 6f8a833..4aaa8b5 100644 --- a/src/language.rs +++ b/src/language.rs @@ -15,7 +15,7 @@ impl TokenError { pub struct LLVMCodeString(pub String); -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct EvalOptions { pub debug_tokens: bool, pub debug_parse: bool, diff --git a/src/main.rs b/src/main.rs index fb88419..5115f8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,10 @@ extern crate linefeed; extern crate lazy_static; #[macro_use] extern crate maplit; +#[macro_use] +extern crate serde_derive; +extern crate serde; +extern crate serde_json; use std::path::Path; use std::fs::File; @@ -121,14 +125,42 @@ impl Repl { let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap(); reader.set_prompt(">> "); let i = if initial_index < languages.len() { initial_index } else { 0 }; + Repl { - options: EvalOptions::default(), + options: Repl::get_options(), languages: languages, current_language_index: i, interpreter_directive_sigil: '.', reader: reader, } } + + fn get_options() -> EvalOptions { + File::open(".schala_repl") + .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(".schala_repl") + .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 .schala_repl file {}", err); + } + } + fn run(&mut self) { use linefeed::ReadResult::*; println!("MetaInterpreter v 0.05"); @@ -181,7 +213,10 @@ impl Repl { }; match cmd { - "exit" | "quit" => process::exit(0), + "exit" | "quit" => { + self.save_options(); + process::exit(0) + }, "history" => { for item in self.reader.history() { println!("{}", item);