typos/src/report.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2019-06-22 11:12:54 -04:00
use std::io::{self, Write};
2019-06-14 09:04:58 -04:00
#[derive(Copy, Clone, Debug, Serialize)]
2019-01-24 10:24:20 -05:00
pub struct Message<'m> {
pub path: &'m std::path::Path,
#[serde(skip)]
pub line: &'m [u8],
pub line_num: usize,
pub col_num: usize,
2019-06-23 00:01:27 -04:00
pub typo: &'m str,
2019-01-24 10:24:20 -05:00
pub correction: &'m str,
#[serde(skip)]
pub(crate) non_exhaustive: (),
}
pub type Report = fn(msg: Message);
2019-06-14 08:43:21 -04:00
pub fn print_silent(_: Message) {}
2019-01-24 10:24:20 -05:00
pub fn print_brief(msg: Message) {
2019-06-14 08:43:21 -04:00
println!(
"{}:{}:{}: {} -> {}",
msg.path.display(),
msg.line_num,
msg.col_num,
2019-06-23 00:01:27 -04:00
msg.typo,
2019-06-14 08:43:21 -04:00
msg.correction
);
2019-01-24 10:24:20 -05:00
}
pub fn print_long(msg: Message) {
let line_num = msg.line_num.to_string();
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
let hl_indent: String = itertools::repeat_n(" ", msg.col_num).collect();
2019-06-23 00:01:27 -04:00
let hl: String = itertools::repeat_n("^", msg.typo.len()).collect();
2019-01-24 10:24:20 -05:00
let line = String::from_utf8_lossy(msg.line);
let line = line.replace("\t", " ");
2019-06-22 11:12:54 -04:00
let stdout = io::stdout();
let mut handle = stdout.lock();
writeln!(
handle,
"error: `{}` should be `{}`",
2019-06-23 00:01:27 -04:00
msg.typo, msg.correction
2019-06-22 11:12:54 -04:00
)
.unwrap();
writeln!(
handle,
2019-06-14 08:43:21 -04:00
" --> {}:{}:{}",
msg.path.display(),
msg.line_num,
msg.col_num
2019-06-22 11:12:54 -04:00
)
.unwrap();
writeln!(handle, "{} |", line_indent).unwrap();
writeln!(handle, "{} | {}", msg.line_num, line.trim_end()).unwrap();
2019-06-22 11:12:54 -04:00
writeln!(handle, "{} | {}{}", line_indent, hl_indent, hl).unwrap();
writeln!(handle, "{} |", line_indent).unwrap();
2019-01-24 10:24:20 -05:00
}
pub fn print_json(msg: Message) {
println!("{}", serde_json::to_string(&msg).unwrap());
}