From 432129fcfe47abe83f5f20d544a4f14686921c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=BC=C3=9F?= Date: Mon, 14 Oct 2019 20:28:54 +0200 Subject: fix warnings as well as issues with the cache dir --- src/lex/mod.rs | 4 ++-- src/main.rs | 49 ++++++++++++++++++++++++++++++------------------- src/readline/mod.rs | 17 ++++++++--------- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/lex/mod.rs b/src/lex/mod.rs index fe9c421..b21bafb 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -131,7 +131,7 @@ pub fn lexer(input: &str, prev_ans: Option) -> Result, CalcError for letter in input.chars() { match letter { - '0'...'9' | '.' => { + '0'..='9' | '.' => { if !char_vec.is_empty() { if FUNCTIONS.get(&char_vec[..]).is_some() { return Err(CalcError::Syntax(format!( @@ -174,7 +174,7 @@ pub fn lexer(input: &str, prev_ans: Option) -> Result, CalcError last_char_is_op = false; result.push(Token::Num(prev_ans.unwrap())); } - 'a'...'z' | 'A'...'Z' => { + 'a'..='z' | 'A'..='Z' => { let parse_num = num_vec.parse::().ok(); if let Some(x) = parse_num { result.push(Token::Num(x)); diff --git a/src/main.rs b/src/main.rs index 38f4837..63b07c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,18 +66,20 @@ fn main() { let mut history_path = PathBuf::from(eva_data_dir); let mut previous_ans_path = PathBuf::from(eva_cache_dir); - match create_dir_all(eva_data_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()); - } - }; + if let Err(_) = create_dir_all(eva_data_dir) { + history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); + } + if let Err(_) = create_dir_all(eva_cache_dir) { + previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); + } + history_path.push("history.txt"); + previous_ans_path.push("previous_ans.txt"); - std::fs::write(&previous_ans_path, "0"); + if let Err(err) = std::fs::write(&previous_ans_path, "0") { + println!("Could not write to previous_ans_path"); + println!("{:?}", err); + std::process::exit(1); + } if rl.load_history(history_path.as_path()).is_err() { println!("No previous history.") @@ -92,18 +94,27 @@ fn main() { let evaled = eval_math_expression(&line[..], prev_ans); match evaled { Ok(ans) => { - use std::io::Write; use std::fs::OpenOptions; - let mut file = OpenOptions::new() + use std::io::Write; + prev_ans = Some(ans); + pprint(ans); + match OpenOptions::new() .write(true) .create(true) .open(&previous_ans_path) - .unwrap(); - - prev_ans = Some(ans); - writeln!(file, "{}", ans); - - pprint(ans); + { + Ok(mut file) => { + if let Err(err) = writeln!(file, "{}", ans) { + println!( + "Error while writing previous answer to file: {}", + err + ) + } + } + Err(err) => { + println!("Error while writing previous answer to file: {}", err) + } + } } Err(e) => println!("{}", handler(e)), }; diff --git a/src/readline/mod.rs b/src/readline/mod.rs index 8eedbfa..ea195ee 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs @@ -20,18 +20,18 @@ pub struct RLHelper { hinter: HistoryHinter, } -struct LineHighlighter { } +struct LineHighlighter {} impl Highlighter for LineHighlighter { fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { Owned(format!("\x1b[90m{}\x1b[0m", hint)) } fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { - use std::io::{ BufReader, BufRead }; use std::fs::OpenOptions; + use std::io::{BufRead, BufReader}; let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); let eva_data_dir = eva_dirs.data_dir(); - let mut previous_ans_path= PathBuf::from(eva_data_dir); + let mut previous_ans_path = PathBuf::from(eva_data_dir); previous_ans_path.push("previous_ans.txt"); let file = OpenOptions::new() @@ -43,11 +43,10 @@ impl Highlighter for LineHighlighter { let rdr = BufReader::new(file); let lines = rdr.lines().map(|l| l.unwrap()); - let prev_ans = lines - .last() - .unwrap() - .parse::() - .ok(); + let prev_ans = match lines.last() { + Some(val) => val.parse::().ok(), + None => None, + }; let op = eval_math_expression(line, prev_ans); match op { Ok(_) => { @@ -114,7 +113,7 @@ pub fn create_readline() -> Editor { let mut rl = Editor::with_config(config); let h = RLHelper { completer: FilenameCompleter::new(), - highlighter: LineHighlighter { }, + highlighter: LineHighlighter {}, hinter: HistoryHinter {}, }; rl.set_helper(Some(h)); -- cgit v1.2.3