diff options
author | Dominik Süß <[email protected]> | 2019-10-14 19:28:54 +0100 |
---|---|---|
committer | Dominik Süß <[email protected]> | 2019-10-14 19:28:54 +0100 |
commit | 432129fcfe47abe83f5f20d544a4f14686921c7e (patch) | |
tree | 89bc4262f49967b6b9fe050f9f2e8e506a90c785 /src/main.rs | |
parent | d74ac7e20061cc353590b9b61977ec3aae334aba (diff) |
fix warnings as well as issues with the cache dir
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 49 |
1 files changed, 30 insertions, 19 deletions
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() { | |||
66 | let mut history_path = PathBuf::from(eva_data_dir); | 66 | let mut history_path = PathBuf::from(eva_data_dir); |
67 | let mut previous_ans_path = PathBuf::from(eva_cache_dir); | 67 | let mut previous_ans_path = PathBuf::from(eva_cache_dir); |
68 | 68 | ||
69 | match create_dir_all(eva_data_dir) { | 69 | if let Err(_) = create_dir_all(eva_data_dir) { |
70 | Ok(_) => { | 70 | history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); |
71 | history_path.push("history.txt"); | 71 | } |
72 | previous_ans_path.push("previous_ans.txt"); | 72 | if let Err(_) = create_dir_all(eva_cache_dir) { |
73 | }, | 73 | previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); |
74 | Err(_) => { | 74 | } |
75 | history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); | 75 | history_path.push("history.txt"); |
76 | previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); | 76 | previous_ans_path.push("previous_ans.txt"); |
77 | } | ||
78 | }; | ||
79 | 77 | ||
80 | std::fs::write(&previous_ans_path, "0"); | 78 | if let Err(err) = std::fs::write(&previous_ans_path, "0") { |
79 | println!("Could not write to previous_ans_path"); | ||
80 | println!("{:?}", err); | ||
81 | std::process::exit(1); | ||
82 | } | ||
81 | 83 | ||
82 | if rl.load_history(history_path.as_path()).is_err() { | 84 | if rl.load_history(history_path.as_path()).is_err() { |
83 | println!("No previous history.") | 85 | println!("No previous history.") |
@@ -92,18 +94,27 @@ fn main() { | |||
92 | let evaled = eval_math_expression(&line[..], prev_ans); | 94 | let evaled = eval_math_expression(&line[..], prev_ans); |
93 | match evaled { | 95 | match evaled { |
94 | Ok(ans) => { | 96 | Ok(ans) => { |
95 | use std::io::Write; | ||
96 | use std::fs::OpenOptions; | 97 | use std::fs::OpenOptions; |
97 | let mut file = OpenOptions::new() | 98 | use std::io::Write; |
99 | prev_ans = Some(ans); | ||
100 | pprint(ans); | ||
101 | match OpenOptions::new() | ||
98 | .write(true) | 102 | .write(true) |
99 | .create(true) | 103 | .create(true) |
100 | .open(&previous_ans_path) | 104 | .open(&previous_ans_path) |
101 | .unwrap(); | 105 | { |
102 | 106 | Ok(mut file) => { | |
103 | prev_ans = Some(ans); | 107 | if let Err(err) = writeln!(file, "{}", ans) { |
104 | writeln!(file, "{}", ans); | 108 | println!( |
105 | 109 | "Error while writing previous answer to file: {}", | |
106 | pprint(ans); | 110 | err |
111 | ) | ||
112 | } | ||
113 | } | ||
114 | Err(err) => { | ||
115 | println!("Error while writing previous answer to file: {}", err) | ||
116 | } | ||
117 | } | ||
107 | } | 118 | } |
108 | Err(e) => println!("{}", handler(e)), | 119 | Err(e) => println!("{}", handler(e)), |
109 | }; | 120 | }; |