1use std::ops::Range;
2use std::fs;
3
4const LIB_PATH: &str = "/usr/share/gtk-ui";
5
6pub fn check_error(result: Result<(), (String, Range<usize>)>, file: &String, file_content: &String) {
7 if let Err(err) = result {
8 if err.1.start > err.1.end {
9 println!("\x1b[1;31mError:\x1b[0m {} (in {})", err.0, file);
10 } else {
11 match get_position_from_char_index(err.1.start, file_content) {
12 Ok((line, char)) => {
13 println!("\x1b[1;31mError:\x1b[0m {} (line {}, char {}, in {})", err.0, line, char, file);
14 },
15 Err(message) => println!("\x1b[1;31mError:\x1b[0m {}", message)
16 }
17 }
18 std::process::exit(1);
19 }
20}
21
22pub fn get_position_from_char_index(char_index: usize, file_content: &String) -> Result<(usize, usize), &str> {
23 if char_index >= file_content.len() {
25 Err("character index bigger than file content (something horrible must have gone wrong)")
26 } else {
27 let mut line_count = 1;
28 let mut char_count = 1;
29 for i in 0..char_index {
30 char_count += 1;
31 if file_content.chars().nth(i).unwrap() == '\n' {
32 line_count += 1;
33 char_count = 1;
34 }
35 }
36 Ok((line_count, char_count))
37 }
38}
39
40pub fn get_include_path(path: &String) -> Option<String> {
41 let lib_file_path = format!("{LIB_PATH}/{path}.gui");
42 if fs::metadata(&lib_file_path).is_ok() {
43 Some(lib_file_path)
44 } else if fs::metadata(&path).is_ok() {
45 Some(path.clone())
46 } else {
47 None
48 }
49}