From 92a5644c9dece71dcf94c72621e7ded12ebdc0b3 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 6 Aug 2019 21:00:19 +0530 Subject: load and store program state --- src/main.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 140150c..ea9e65a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,10 @@ lazy_static! { } fn main() { + env_logger::init(); if !CONFIGURATION.input.is_empty() { // command mode // - let evaled = eval_math_expression(&CONFIGURATION.input[..], 0f64); + let evaled = eval_math_expression(&CONFIGURATION.input[..], Some(0.)); match evaled { Ok(ans) => pprint(ans), Err(e) => { @@ -57,17 +58,26 @@ fn main() { let mut rl = create_readline(); // previous answer - let mut prev_ans = 0f64; + let mut prev_ans = None; // handle history storage let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); let eva_data_dir = eva_dirs.data_dir(); let mut history_path = PathBuf::from(eva_data_dir); + let mut previous_ans_path = PathBuf::from(eva_data_dir); match create_dir_all(eva_data_dir) { - Ok(_) => history_path.push("history.txt"), - Err(_) => history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()), + Ok(_) => { + history_path.push("history.txt"); + previous_ans_path.push("previous_ans.txt"); + }, + Err(_) => { + history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); + previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); + } }; + std::fs::write(&previous_ans_path, "0"); + if rl.load_history(history_path.as_path()).is_err() { println!("No previous history.") }; @@ -81,7 +91,17 @@ fn main() { let evaled = eval_math_expression(&line[..], prev_ans); match evaled { Ok(ans) => { - prev_ans = ans; + use std::io::Write; + use std::fs::OpenOptions; + let mut file = OpenOptions::new() + .write(true) + .create(true) + .open(&previous_ans_path) + .unwrap(); + + prev_ans = Some(ans); + writeln!(file, "{}", ans); + pprint(ans); } Err(e) => println!("{}", handler(e)), @@ -148,7 +168,7 @@ fn parse_arguments() -> Configuration { } } -pub fn eval_math_expression(input: &str, prev_ans: f64) -> Result { +pub fn eval_math_expression(input: &str, prev_ans: Option) -> Result { let input = input.trim(); let input = input.replace(" ", ""); if input.is_empty() { @@ -203,4 +223,9 @@ mod tests { let evaled = eval_math_expression("_ + 9", 9f64).unwrap(); assert_eq!(18.0, evaled); } + #[test] + fn eval_with_zero_prev() { + let evaled = eval_math_expression("9 + _ ", 0f64).unwrap(); + assert_eq!(9., evaled); + } } -- cgit v1.2.3