extern crate regex;
use regex::Regex;
const WHITESPACE: char = ' ';
const NEWLINE: char = '\n';
const FIRST_LINE: usize = 0;
pub fn get_error_text_with_error_code(s: String) -> Vec<String> {
get_error_text(s, true)
}
pub fn get_error_text_without_error_code(s: String) -> Vec<String> {
get_error_text(s, false)
}
fn get_error_text(s: String, with_error: bool) -> Vec<String> {
let mut descriptions: Vec<String> = Vec::new();
let errors = structure_compiler_output(s);
for entry in errors.into_iter() {
let temp: Vec<&str> = entry.split(NEWLINE).collect();
descriptions.push(cut_out(String::from(temp[FIRST_LINE]), with_error));
}
descriptions
}
pub fn structure_compiler_output(output: String) -> Vec<String> {
let mut errors: Vec<String> = Vec::new();
let re = Regex::new(r"(?m)error\[E\d{4}\]:").unwrap();
for entry in output.split("\n\n") {
let stringfied_entry = String::from(entry);
if re.is_match(stringfied_entry.as_str()) {
errors.push(stringfied_entry);
}
}
if errors.is_empty() && re.is_match(output.as_str()) {
return vec![output];
} else {
errors
}
}
fn cut_out(i: String, with_error: bool) -> String {
let mut erg = String::new();
let re = Regex::new(r"(?m)<(?m).*>").unwrap();
let p = re.replace_all(i.as_str(), ""); let mut words = p.split_whitespace().collect::<Vec<&str>>();
let mut error: String = String::from("");
words.pop();
error.push_str(words.remove(0)); error.push(WHITESPACE);
for s in words.into_iter() {
let mut word = s.chars()
.filter(|c| (*c).is_alphabetic())
.collect::<String>();
if !word.is_empty() {
erg.push_str(String::from(word).trim());
erg.push(WHITESPACE);
}
}
if with_error {
return {
let mut x = error;
x.push_str(erg.as_str());
String::from(x.trim())
};
} else {
String::from(erg.trim())
}
}