Skip to content

Commit

Permalink
fix warnings as well as issues with the cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
theSuess committed Oct 14, 2019
1 parent d74ac7e commit 432129f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/lex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, 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!(
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, 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::<f64>().ok();
if let Some(x) = parse_num {
result.push(Token::Num(x));
Expand Down
49 changes: 30 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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)),
};
Expand Down
17 changes: 8 additions & 9 deletions src/readline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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::<f64>()
.ok();
let prev_ans = match lines.last() {
Some(val) => val.parse::<f64>().ok(),
None => None,
};
let op = eval_math_expression(line, prev_ans);
match op {
Ok(_) => {
Expand Down Expand Up @@ -114,7 +113,7 @@ pub fn create_readline() -> Editor<RLHelper> {
let mut rl = Editor::with_config(config);
let h = RLHelper {
completer: FilenameCompleter::new(),
highlighter: LineHighlighter { },
highlighter: LineHighlighter {},
hinter: HistoryHinter {},
};
rl.set_helper(Some(h));
Expand Down

0 comments on commit 432129f

Please sign in to comment.