use crate::MatchRange;
use std::cmp::min;
use std::fmt::{self, Display, Formatter};
pub trait Recorder {
fn directive(&mut self, dct: usize);
fn matched_check(&mut self, regex: &str, matched: MatchRange);
fn matched_not(&mut self, regex: &str, matched: MatchRange);
fn missed_check(&mut self, regex: &str, searched: MatchRange);
fn missed_not(&mut self, regex: &str, searched: MatchRange);
fn defined_var(&mut self, varname: &str, value: &str);
}
impl Recorder for () {
fn directive(&mut self, _: usize) {}
fn matched_check(&mut self, _: &str, _: MatchRange) {}
fn matched_not(&mut self, _: &str, _: MatchRange) {}
fn defined_var(&mut self, _: &str, _: &str) {}
fn missed_check(&mut self, _: &str, _: MatchRange) {}
fn missed_not(&mut self, _: &str, _: MatchRange) {}
}
struct Match {
directive: usize,
is_match: bool,
is_not: bool,
regex: String,
range: MatchRange,
}
struct VarDef {
directive: usize,
varname: String,
value: String,
}
pub struct Explainer<'a> {
text: &'a str,
directive: usize,
matches: Vec<Match>,
vardefs: Vec<VarDef>,
}
impl<'a> Explainer<'a> {
pub fn new(text: &'a str) -> Explainer {
Explainer {
text,
directive: 0,
matches: Vec::new(),
vardefs: Vec::new(),
}
}
pub fn finish(&mut self) {
self.matches.sort_by_key(|m| (m.range, m.directive));
self.vardefs.sort_by_key(|v| v.directive);
}
}
impl<'a> Display for Explainer<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut curln = 0;
let mut nextln = 0;
for m in &self.matches {
while nextln <= m.range.0 && nextln < self.text.len() {
let newln = self.text[nextln..]
.find('\n')
.map(|d| nextln + d + 1)
.unwrap_or(self.text.len());
assert!(newln > nextln);
writeln!(f, "> {}", &self.text[nextln..newln - 1])?;
curln = nextln;
nextln = newln;
}
if m.is_match {
write!(f, " ")?;
let mend = min(m.range.1, nextln - 1);
for pos in curln..mend {
if pos < m.range.0 {
write!(f, " ")
} else if pos == m.range.0 {
write!(f, "^")
} else {
write!(f, "~")
}?;
}
writeln!(f)?;
}
writeln!(
f,
"{} #{}{}: {}",
if m.is_match { "Matched" } else { "Missed" },
m.directive,
if m.is_not { " not" } else { "" },
m.regex
)?;
if let Ok(found) = self
.vardefs
.binary_search_by_key(&m.directive, |v| v.directive)
{
let mut first = found;
while first > 0 && self.vardefs[first - 1].directive == m.directive {
first -= 1;
}
for d in &self.vardefs[first..] {
if d.directive != m.directive {
break;
}
writeln!(f, "Define {}={}", d.varname, d.value)?;
}
}
}
for line in self.text[nextln..].lines() {
writeln!(f, "> {}", line)?;
}
Ok(())
}
}
impl<'a> Recorder for Explainer<'a> {
fn directive(&mut self, dct: usize) {
self.directive = dct;
}
fn matched_check(&mut self, regex: &str, matched: MatchRange) {
self.matches.push(Match {
directive: self.directive,
is_match: true,
is_not: false,
regex: regex.to_owned(),
range: matched,
});
}
fn matched_not(&mut self, regex: &str, matched: MatchRange) {
self.matches.push(Match {
directive: self.directive,
is_match: true,
is_not: true,
regex: regex.to_owned(),
range: matched,
});
}
fn missed_check(&mut self, regex: &str, searched: MatchRange) {
self.matches.push(Match {
directive: self.directive,
is_match: false,
is_not: false,
regex: regex.to_owned(),
range: searched,
});
}
fn missed_not(&mut self, regex: &str, searched: MatchRange) {
self.matches.push(Match {
directive: self.directive,
is_match: false,
is_not: true,
regex: regex.to_owned(),
range: searched,
});
}
fn defined_var(&mut self, varname: &str, value: &str) {
self.vardefs.push(VarDef {
directive: self.directive,
varname: varname.to_owned(),
value: value.to_owned(),
});
}
}