minify_html_onepass/
err.rs

1use std::fmt::Display;
2
3/// Represents the type of minification error.
4#[derive(Debug, Eq, PartialEq)]
5pub enum ErrorType {
6  ClosingTagMismatch { expected: String, got: String },
7  NotFound(&'static str),
8  UnexpectedEnd,
9  UnexpectedClosingTag,
10}
11
12impl ErrorType {
13  /// Generates an English message describing the error with any additional context.
14  pub fn message(&self) -> String {
15    match self {
16      ErrorType::ClosingTagMismatch { expected, got } => {
17        format!(
18          "Closing tag name does not match opening tag (expected \"{}\", got \"{}\").",
19          expected, got
20        )
21      }
22      ErrorType::NotFound(exp) => {
23        format!("Expected {}.", exp)
24      }
25      ErrorType::UnexpectedEnd => "Unexpected end of source code.".to_string(),
26      ErrorType::UnexpectedClosingTag => "Unexpected closing tag.".to_string(),
27    }
28  }
29}
30
31/// Details about a minification failure, including where it occurred and why.
32#[derive(Debug)]
33pub struct Error {
34  pub error_type: ErrorType,
35  pub position: usize,
36}
37
38impl Display for Error {
39  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40    write!(
41      f,
42      "{} [position {}]",
43      self.error_type.message(),
44      self.position
45    )
46  }
47}
48
49impl std::error::Error for Error {}
50
51/// User-friendly details about a minification failure, including an English message description of
52/// the reason, and generated printable contextual representation of the code where the error
53/// occurred.
54#[derive(Debug)]
55pub struct FriendlyError {
56  pub position: usize,
57  pub message: String,
58  pub code_context: String,
59}
60
61pub type ProcessingResult<T> = Result<T, ErrorType>;
62
63#[inline(always)]
64fn maybe_mark_indicator(
65  line: &mut Vec<u8>,
66  marker: u8,
67  maybe_pos: isize,
68  lower: usize,
69  upper: usize,
70) -> bool {
71  let pos = maybe_pos as usize;
72  if maybe_pos > -1 && pos >= lower && pos < upper {
73    let pos_in_line = pos - lower;
74    while line.len() <= pos_in_line {
75      line.push(b' ');
76    }
77    line.insert(
78      pos_in_line,
79      if line[pos_in_line] != b' ' {
80        b'B'
81      } else {
82        marker
83      },
84    );
85    true
86  } else {
87    false
88  }
89}
90
91// Pass -1 for read_pos or write_pos to prevent them from being represented.
92pub fn debug_repr(code: &[u8], read_pos: isize, write_pos: isize) -> String {
93  let only_one_pos = read_pos == -1 || write_pos == -1;
94  let read_marker = if only_one_pos { b'^' } else { b'R' };
95  let write_marker = if only_one_pos { b'^' } else { b'W' };
96  let mut lines = Vec::<(isize, String)>::new();
97  let mut cur_pos = 0;
98  for (line_no, line) in code.split(|c| *c == b'\n').enumerate() {
99    // Include '\n'. Note that the last line might not have '\n' but that's OK for these calculations.
100    let len = line.len() + 1;
101    let line_as_string = unsafe { String::from_utf8_unchecked(line.to_vec()) };
102    lines.push(((line_no + 1) as isize, line_as_string));
103    let new_pos = cur_pos + len;
104
105    // Rust does lazy allocation by default, so this is not wasteful.
106    let mut indicator_line = Vec::new();
107    maybe_mark_indicator(
108      &mut indicator_line,
109      write_marker,
110      write_pos,
111      cur_pos,
112      new_pos,
113    );
114    let marked_read =
115      maybe_mark_indicator(&mut indicator_line, read_marker, read_pos, cur_pos, new_pos);
116    if !indicator_line.is_empty() {
117      lines.push((-1, unsafe { String::from_utf8_unchecked(indicator_line) }));
118    };
119    cur_pos = new_pos;
120    if marked_read {
121      break;
122    };
123  }
124
125  let line_no_col_width = lines.len().to_string().len();
126  let mut res = String::new();
127  for (line_no, line) in lines {
128    res.push_str(&format!(
129      "{:>indent$}|{}\n",
130      if line_no == -1 {
131        ">".repeat(line_no_col_width)
132      } else {
133        line_no.to_string()
134      },
135      line,
136      indent = line_no_col_width,
137    ));
138  }
139  res
140}
141
142impl Display for FriendlyError {
143  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144    write!(f, "{} [position {}]", self.message, self.position)
145  }
146}
147
148impl std::error::Error for FriendlyError {}