diff --git a/src/dictionary.rs b/src/dictionary.rs index e18f1f6..891c2c7 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -1,5 +1,6 @@ use rusqlite::Connection; use std::fs; +use std::io::ErrorKind; use crate::db::get_db_path; @@ -42,11 +43,27 @@ fn get_sym_pos_or_panic(path: &String, line: &str, sym: &str, line_number: usize fn read_and_parse(path: &String) -> Vec { let mut result: Vec = Vec::new(); - let content = fs::read_to_string(path).unwrap(); + let content = match fs::read_to_string(path) { + Ok(string) => string, + Err(kind) => match kind.kind() { + ErrorKind::NotFound => { + println!("No dictionary file found. Assuming it's empty. Perhaps, you want to download it from my git? https://git.foxarmy.org/leca/learn-hieroglyphs"); + return Vec::new(); + } + ErrorKind::PermissionDenied => { + println!("Permission denied while was trying to access the dictionary.txt file. Assuming it's empty"); + return Vec::new(); + } + _ => { + println!("Unexpected error. Can't load dictionary. Assuming it's empty"); + return Vec::new(); + }, + }, + }; let content: Vec<&str> = content.split('\n').collect(); for (line_number, line) in content.iter().enumerate() { - if line == &"" { + if *line == "" { continue; } let hieroglyph_start = get_sym_pos_or_panic(path, line, "(", line_number) + 1;