1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! # Extractor
//!
//! This is a experimental crate for extracting possible inline titles for error searching with
//! [resa](https://github.com/Menkir/resa).
//!
//! The following example will demonstrate the purpose:
//! ```text
//! error[E0382]: use of moved value: 'v'
//! --> examples/fail.rs:4:29
//! |
//! 3 |     let v2 = v;
//! |         -- value moved here
//! 4 |     println!("v[0] is: {}\", v[0]);
//!   |                             ^ value used here after move
//! |
//! = note: move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait"
//! ```
//!
//! A compiler error. The extractor crate will search for the inline title `use of moved value`. To get this sentence
//! the crate need to filter, split and match regular expressions.
//!
//! Based on this title we're able to search e.g on StackOverflow for similar issues.
extern crate regex;
use regex::Regex;


const WHITESPACE: char = ' ';
const NEWLINE: char = '\n';
const FIRST_LINE: usize = 0;

/// Retuns a Vector of error sentences with error code
///
/// # Arguments
/// * `s` - A error message as String
///
/// # Example
/// ```rust
/// assert_eq!(extractor::get_error_text_without_error_code(String::from("error[E0369]: binary operation + cannot be applied to type <T as std::ops::Mul>::Output")),
/// vec!["binary operation cannot be applied to type"]);
/// ```
pub fn get_error_text_with_error_code(s: String) -> Vec<String> {
    get_error_text(s, true)
}

/// Retuns a Vector of error sentences without error code
///
/// # Arguments
/// * `s` - A error message as String
///
/// # Example
/// ```rust
/// assert_eq!(extractor::get_error_text_with_error_code(String::from("error[E0369]: binary operation + cannot be applied to type <T as std::ops::Mul>::Output")),
/// vec!["error[E0369]: binary operation cannot be applied to type"]);
/// ```
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
}

/// Returns a Vector of compiler error entries
///
/// # Arguments
/// * `s` - The whole Compilererror output as String

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()) {
            //check if certain error code is found
            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(), ""); //remove typedefs like <std::ops::...>
    let mut words = p.split_whitespace().collect::<Vec<&str>>();
    let mut error: String = String::from("");
    words.pop();
    error.push_str(words.remove(0)); //remove error code
    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())
    }
}