pub struct Error {
pub caret: usize,
pub msg: String,
}
impl Error {
pub fn format_error(&self, file: &str, input_text: &[char]) -> String {
let line_number = input_text[..self.caret]
.iter()
.filter(|&&c| c == '\n')
.count()
+ 1;
let line_start = input_text[..self.caret]
.iter()
.rposition(|&c| c == '\n')
.map_or(0, |pos| pos + 1);
let line_end = input_text[self.caret..]
.iter()
.position(|&c| c == '\n')
.map_or(input_text.len(), |pos| self.caret + pos);
let error_line: String = input_text[line_start..line_end].iter().collect();
let column = self.caret - line_start;
let mut result = String::new();
result.push_str(&format!("{}:{}:\n", file, line_number));
result.push_str(&format!("{}\n", error_line));
result.push_str(&format!("{}^\n", " ".repeat(column)));
result.push_str(&self.msg.to_string());
result
}
}