-
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathresult_formatting.rs
More file actions
347 lines (330 loc) · 12.9 KB
/
result_formatting.rs
File metadata and controls
347 lines (330 loc) · 12.9 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use anyhow::anyhow;
use colored::Colorize;
use console::style;
use core::fmt;
use log::info;
use marzano_core::api::{
AllDone, AnalysisLog, CreateFile, DoneFile, FileMatchResult, InputFile, Match, MatchResult,
PatternInfo, RemoveFile, Rewrite,
};
use marzano_messenger::output_mode::OutputMode;
use std::fmt::Display;
use std::fs::read_to_string;
use std::{
io::Write,
sync::{Arc, Mutex},
};
use crate::ux::{format_result_diff, indent};
use marzano_messenger::emit::Messager;
#[derive(Debug)]
pub enum FormattedResult {
AnalysisLog(AnalysisLog),
Match(Match),
InputFile(InputFile),
DoneFile(DoneFile),
AllDone(AllDone),
PatternInfo(PatternInfo),
Rewrite(Rewrite),
CreateFile(CreateFile),
RemoveFile(RemoveFile),
Compact(MatchResult),
}
impl FormattedResult {
pub fn new(result: MatchResult, compact: bool) -> Option<FormattedResult> {
if compact {
return Some(FormattedResult::Compact(result));
}
match result {
MatchResult::AnalysisLog(log) => Some(FormattedResult::AnalysisLog(log)),
MatchResult::Match(m) => Some(FormattedResult::Match(m)),
MatchResult::InputFile(f) => Some(FormattedResult::InputFile(f)),
MatchResult::DoneFile(f) => Some(FormattedResult::DoneFile(f)),
MatchResult::AllDone(f) => Some(FormattedResult::AllDone(f)),
MatchResult::PatternInfo(f) => Some(FormattedResult::PatternInfo(f)),
MatchResult::Rewrite(r) => Some(FormattedResult::Rewrite(r)),
MatchResult::CreateFile(r) => Some(FormattedResult::CreateFile(r)),
MatchResult::RemoveFile(r) => Some(FormattedResult::RemoveFile(r)),
}
}
}
fn print_file_ranges<T: FileMatchResult>(item: &mut T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = item.file_name().bold();
for range in item.ranges() {
writeln!(
f,
"{}:{}:{} - {}",
name,
range.start.line,
range.start.column,
T::action()
)?;
}
Ok(())
}
fn print_all_done(item: &AllDone, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Processed {} files and found {} matches",
item.processed, item.found
)
}
fn print_error_log(log: &AnalysisLog, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let file_prefix = if log.file.is_empty() {
"".to_owned()
} else {
format!("{}: ", log.file)
};
if log.level >= 500 {
let msg = format!("{}DEBUG - {}", file_prefix, log.message).dimmed();
writeln!(f, "{}", msg)
} else if log.level == 310 {
style(log.message.to_string()).dim().fmt(f)
} else if log.level == 441 {
if log.file.is_empty() {
writeln!(f, "{}", log.message)?;
} else {
let title = format!("Log in {}", log.file).bold();
writeln!(f, "{}: {}", title, log.message)?;
}
let empty_string = String::new();
let range = match log.range.as_ref() {
Some(range) => indent(
&format!(
"- Range: {}:{} - {}:{}",
range.start.line, range.start.column, range.end.line, range.end.column
),
2,
),
None => indent("- Range:", 2).to_owned(),
};
writeln!(f, "{}", range)?;
let source = log.source.as_ref().unwrap_or(&empty_string);
let source = source.dimmed();
let source = indent(&format!("- Source:\n{}", source), 2);
writeln!(f, "{}", source)?;
let syntax_tree = log.syntax_tree.as_ref().unwrap_or(&empty_string).dimmed();
let syntax_tree = indent(&format!("- Syntax tree:\n{}\n", syntax_tree), 2);
write!(f, "{}", syntax_tree)
} else {
write!(
f,
"{}ERROR (code: {}) - {}",
file_prefix, log.level, log.message
)
}
}
/// Implement some log overrides to make CLI usage more friendly
fn humanize_log(log: &mut AnalysisLog, input_pattern: &str) {
if log.level == 299
&& log.file.is_empty()
&& (input_pattern.ends_with(".yaml") || input_pattern.ends_with(".yml"))
{
log.message = format!(
"{} is a config file, not a pattern. Try applying the pattern by name.",
input_pattern.bold().red()
);
}
}
/// Get a friendly, contextual error for an error log
pub fn get_human_error(mut log: AnalysisLog, input_pattern: &str) -> String {
humanize_log(&mut log, input_pattern);
let formatted = FormattedResult::AnalysisLog(log);
let result = format!("{}", formatted);
result
}
impl fmt::Display for FormattedResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FormattedResult::Compact(result) => {
match result {
MatchResult::AnalysisLog(log) => {
print_error_log(log, f)?;
}
MatchResult::InputFile(_)
| MatchResult::PatternInfo(_)
| MatchResult::DoneFile(_) => {
// These are not shown in compact mode
}
MatchResult::AllDone(r) => {
print_all_done(r, f)?;
}
MatchResult::Match(r) => print_file_ranges(&mut r.clone(), f)?,
MatchResult::Rewrite(r) => print_file_ranges(&mut r.clone(), f)?,
MatchResult::CreateFile(r) => print_file_ranges(&mut r.clone(), f)?,
MatchResult::RemoveFile(r) => print_file_ranges(&mut r.clone(), f)?,
}
Ok(())
}
FormattedResult::AnalysisLog(log) => {
print_error_log(log, f)?;
Ok(())
}
FormattedResult::Match(m) => {
let path_title = m.file_name().bold();
writeln!(f, "{}", path_title)?;
let source = read_to_string(m.file_name());
match source {
Err(e) => {
writeln!(f, "Could not read file: {}", e)?;
return Ok(());
}
Ok(source) => {
let ranges = &mut m.ranges.iter();
// Iterate through the lines of the file
let mut line_number = 0;
let mut next_range = ranges.next();
let lines = source.lines();
for line in lines {
line_number += 1;
if let Some(range) = next_range {
if line_number <= range.end.line {
let overlap =
range.get_line_range(line_number, line.len() as u32);
match overlap {
None => {}
Some((start_col, end_col)) => {
// This line is part of the match
let prefix = &line[0..start_col];
let highlight = &line[start_col..end_col];
let suffix = &line[end_col..];
writeln!(
f,
"{:6} {}{}{}",
line_number,
prefix.dimmed(),
highlight.blue().bold(),
suffix.dimmed()
)?;
}
}
} else {
// We are beyond the current range, search for a new range that is after or on this line
while let Some(range) = next_range {
if range.end.line >= line_number {
break;
}
next_range = ranges.next();
}
}
}
}
}
}
Ok(())
}
FormattedResult::InputFile(item) => {
write!(f, "Parsed input file: {}", item.source_file)
}
FormattedResult::AllDone(item) => print_all_done(item, f),
FormattedResult::DoneFile(_) => Ok(()),
FormattedResult::Rewrite(item) => {
let path_name = if item.original.source_file == item.rewritten.source_file {
item.file_name().to_string()
} else {
format!(
"{} -> {}",
item.original.source_file, item.rewritten.source_file
)
};
let path_title = path_name.bold();
writeln!(f, "{}", path_title)?;
let result: MatchResult = item.clone().into();
let diff = format_result_diff(&result, None);
write!(f, "{}", diff)?;
Ok(())
}
FormattedResult::CreateFile(item) => {
let path_title = item.file_name().bold();
let result: MatchResult = item.clone().into();
writeln!(f, "{}", path_title)?;
let diff = format_result_diff(&result, None);
write!(f, "{}", diff)?;
Ok(())
}
FormattedResult::RemoveFile(item) => {
let path_title = item.file_name().bold();
writeln!(f, "{}", path_title)?;
let result: MatchResult = item.clone().into();
let diff = format_result_diff(&result, None);
write!(f, "{}", diff)?;
Ok(())
}
fallback => {
write!(f, "Unsupported result type: {:?}", fallback)
}
}
}
}
pub struct FormattedMessager<'a> {
writer: Option<Arc<Mutex<Box<dyn Write + Send + 'a>>>>,
mode: OutputMode,
interactive: bool,
total_accepted: usize,
total_rejected: usize,
total_supressed: usize,
input_pattern: String,
}
impl<'a> FormattedMessager<'_> {
pub fn new(
writer: Option<Box<dyn Write + Send + 'a>>,
mode: OutputMode,
interactive: bool,
input_pattern: String,
) -> FormattedMessager<'a> {
FormattedMessager {
writer: writer.map(|w| Arc::new(Mutex::new(w))),
mode,
interactive,
total_accepted: 0,
total_rejected: 0,
total_supressed: 0,
input_pattern,
}
}
}
impl Messager for FormattedMessager<'_> {
fn raw_emit(&mut self, message: &MatchResult) -> anyhow::Result<()> {
if self.interactive && !(self.mode == OutputMode::None) {
if let MatchResult::AllDone(item) = message {
info!(
"Processed {} files. Accepted {} rewrites, rejected {} rewrites and suppressed {} rewrites.",
item.processed, self.total_accepted, self.total_rejected, self.total_supressed
);
return Ok(());
}
}
match self.mode {
OutputMode::None => {}
OutputMode::Standard | OutputMode::Compact => {
let mut message = message.clone();
// Override the message if the pattern name is a file name
if let MatchResult::AnalysisLog(ref mut log) = message {
humanize_log(log, &self.input_pattern);
}
let formatted = FormattedResult::new(message, self.mode == OutputMode::Compact);
if let Some(formatted) = formatted {
if let Some(writer) = &mut self.writer {
let mut writer =
writer.lock().map_err(|_| anyhow!("Output lock poisoned"))?;
writeln!(writer, "{}", formatted)?;
} else {
info!("{}", formatted);
}
}
}
}
Ok(())
}
fn track_accept(&mut self, _accepted: &MatchResult) -> anyhow::Result<()> {
self.total_accepted += 1;
Ok(())
}
fn track_reject(&mut self, _rejected: &MatchResult) -> anyhow::Result<()> {
self.total_rejected += 1;
Ok(())
}
fn track_supress(&mut self, _supressed: &MatchResult) -> anyhow::Result<()> {
self.total_supressed += 1;
Ok(())
}
}