2020-08-24 10:36:27 -04:00
|
|
|
/// Represents the type of minification error.
|
2020-07-30 06:17:55 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2019-12-25 07:29:18 -05:00
|
|
|
pub enum ErrorType {
|
2020-07-29 10:28:57 -04:00
|
|
|
ClosingTagMismatch { expected: String, got: String },
|
2019-12-25 07:29:18 -05:00
|
|
|
NotFound(&'static str),
|
2019-12-23 06:48:41 -05:00
|
|
|
UnexpectedEnd,
|
2020-07-30 06:17:55 -04:00
|
|
|
UnexpectedClosingTag,
|
2019-12-23 06:48:41 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 02:16:33 -05:00
|
|
|
impl ErrorType {
|
2020-08-24 10:36:27 -04:00
|
|
|
/// Generates an English message describing the error with any additional context.
|
2019-12-30 02:16:33 -05:00
|
|
|
pub fn message(self) -> String {
|
|
|
|
match self {
|
2020-07-29 10:28:57 -04:00
|
|
|
ErrorType::ClosingTagMismatch { expected, got } => {
|
|
|
|
format!("Closing tag name does not match opening tag (expected \"{}\", got \"{}\").", expected, got)
|
2020-01-10 02:30:49 -05:00
|
|
|
}
|
2019-12-30 02:16:33 -05:00
|
|
|
ErrorType::NotFound(exp) => {
|
|
|
|
format!("Expected {}.", exp)
|
|
|
|
}
|
|
|
|
ErrorType::UnexpectedEnd => {
|
|
|
|
format!("Unexpected end of source code.")
|
|
|
|
}
|
2020-07-30 06:17:55 -04:00
|
|
|
ErrorType::UnexpectedClosingTag => {
|
|
|
|
format!("Unexpected closing tag.")
|
|
|
|
}
|
2019-12-30 02:16:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 10:36:27 -04:00
|
|
|
/// Details about a minification failure, including where it occurred and why.
|
2020-07-21 03:57:39 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Error {
|
|
|
|
pub error_type: ErrorType,
|
|
|
|
pub position: usize,
|
|
|
|
}
|
|
|
|
|
2020-08-24 10:36:27 -04:00
|
|
|
|
|
|
|
/// User-friendly details about a minification failure, including an English message description of
|
|
|
|
/// the reason, and generated printable contextual representation of the code where the error
|
|
|
|
/// occurred.
|
2020-07-21 03:57:39 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FriendlyError {
|
|
|
|
pub position: usize,
|
|
|
|
pub message: String,
|
|
|
|
pub code_context: String,
|
|
|
|
}
|
|
|
|
|
2019-12-25 21:47:18 -05:00
|
|
|
pub type ProcessingResult<T> = Result<T, ErrorType>;
|
2020-07-22 04:57:47 -04:00
|
|
|
|
|
|
|
#[inline(always)]
|
2020-07-29 10:28:57 -04:00
|
|
|
fn maybe_mark_indicator(line: &mut Vec<u8>, marker: u8, maybe_pos: isize, lower: usize, upper: usize) -> bool {
|
2020-07-22 04:57:47 -04:00
|
|
|
let pos = maybe_pos as usize;
|
2020-07-29 10:28:57 -04:00
|
|
|
if maybe_pos > -1 && pos >= lower && pos < upper {
|
2020-07-22 04:57:47 -04:00
|
|
|
let pos_in_line = pos - lower;
|
|
|
|
while line.len() <= pos_in_line {
|
|
|
|
line.push(b' ');
|
|
|
|
};
|
|
|
|
line.insert(pos_in_line, if line[pos_in_line] != b' ' { b'B' } else { marker });
|
2020-07-29 10:28:57 -04:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-07-22 04:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pass -1 for read_pos or write_pos to prevent them from being represented.
|
|
|
|
pub fn debug_repr(code: &[u8], read_pos: isize, write_pos: isize) -> String {
|
|
|
|
let mut lines = Vec::<(isize, String)>::new();
|
|
|
|
let mut cur_pos = 0;
|
|
|
|
for (line_no, line) in code.split(|c| *c == b'\n').enumerate() {
|
|
|
|
// Include '\n'. Note that the last line might not have '\n' but that's OK for these calculations.
|
|
|
|
let len = line.len() + 1;
|
|
|
|
let line_as_string = unsafe { String::from_utf8_unchecked(line.to_vec()) };
|
2020-08-24 10:36:27 -04:00
|
|
|
lines.push(((line_no + 1) as isize, line_as_string));
|
2020-07-22 04:57:47 -04:00
|
|
|
let new_pos = cur_pos + len;
|
|
|
|
|
|
|
|
// Rust does lazy allocation by default, so this is not wasteful.
|
|
|
|
let mut indicator_line = Vec::new();
|
|
|
|
maybe_mark_indicator(&mut indicator_line, b'W', write_pos, cur_pos, new_pos);
|
2020-07-29 10:28:57 -04:00
|
|
|
let marked_read = maybe_mark_indicator(&mut indicator_line, b'R', read_pos, cur_pos, new_pos);
|
2020-07-22 04:57:47 -04:00
|
|
|
if !indicator_line.is_empty() {
|
|
|
|
lines.push((-1, unsafe { String::from_utf8_unchecked(indicator_line) }));
|
|
|
|
};
|
|
|
|
cur_pos = new_pos;
|
2020-07-29 10:28:57 -04:00
|
|
|
if marked_read {
|
|
|
|
break;
|
|
|
|
};
|
2020-07-22 04:57:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let line_no_col_width = lines.len().to_string().len();
|
|
|
|
let mut res = String::new();
|
|
|
|
for (line_no, line) in lines {
|
|
|
|
res.push_str(&format!(
|
|
|
|
"{:>indent$}|{}\n",
|
|
|
|
if line_no == -1 { ">".repeat(line_no_col_width) } else { line_no.to_string() },
|
|
|
|
line,
|
|
|
|
indent = line_no_col_width,
|
|
|
|
));
|
|
|
|
};
|
|
|
|
res
|
|
|
|
}
|