use std::{
fs::File,
io::{BufRead, BufReader},
process,
};
pub fn parse_queries_file(path: &str) -> Vec<(usize, Vec<i32>)> {
let file = open_file_savely(path);
let lines = BufReader::new(file)
.lines()
.map(|line| line.expect("Unable to read line"));
let mut parsed_queries: Vec<(usize, Vec<i32>)> = Vec::new();
for (line_number, line) in lines.enumerate() {
let res: Vec<i32> = line.split_whitespace()
.map(|elem| elem.parse::<i32>()
.unwrap_or_else(|_| panic!("Unable to parse {elem} into an i32 value while trying to parse the querie file at {path}.\nCheck the help page with \"-h\" or \"--help\" for further information.\n"))
).collect();
parsed_queries.push((line_number, res));
}
parsed_queries
}
pub fn open_file_savely(path: &str) -> File {
match File::open(path) {
Ok(x) => x,
Err(err) => {
eprintln!(
"\x1b[1;38;5;196mERROR: The following error code occured while trying to open the file \"{path}\":\n{err}\nAborting..."
);
process::exit(1);
}
}
}