2019-06-24 23:45:30 -04:00
|
|
|
use std::borrow::Cow;
|
2019-06-22 11:12:54 -04:00
|
|
|
use std::io::{self, Write};
|
|
|
|
|
2019-08-07 09:16:22 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize)]
|
2019-07-16 21:16:54 -04:00
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum Message<'m> {
|
|
|
|
BinaryFile(BinaryFile<'m>),
|
|
|
|
Correction(Correction<'m>),
|
2019-07-18 22:20:45 -04:00
|
|
|
FilenameCorrection(FilenameCorrection<'m>),
|
2019-07-16 21:16:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> From<BinaryFile<'m>> for Message<'m> {
|
|
|
|
fn from(msg: BinaryFile<'m>) -> Self {
|
|
|
|
Message::BinaryFile(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> From<Correction<'m>> for Message<'m> {
|
|
|
|
fn from(msg: Correction<'m>) -> Self {
|
|
|
|
Message::Correction(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 22:20:45 -04:00
|
|
|
impl<'m> From<FilenameCorrection<'m>> for Message<'m> {
|
|
|
|
fn from(msg: FilenameCorrection<'m>) -> Self {
|
|
|
|
Message::FilenameCorrection(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 09:16:22 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize)]
|
2019-07-16 21:16:54 -04:00
|
|
|
pub struct BinaryFile<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
#[serde(skip)]
|
|
|
|
pub(crate) non_exhaustive: (),
|
|
|
|
}
|
|
|
|
|
2019-08-07 09:16:22 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize)]
|
2019-07-16 21:16:54 -04:00
|
|
|
pub struct Correction<'m> {
|
2019-01-24 10:24:20 -05:00
|
|
|
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-06-24 23:45:30 -04:00
|
|
|
pub correction: Cow<'m, str>,
|
2019-01-24 10:24:20 -05:00
|
|
|
#[serde(skip)]
|
|
|
|
pub(crate) non_exhaustive: (),
|
|
|
|
}
|
|
|
|
|
2019-08-07 09:16:22 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize)]
|
2019-07-18 22:20:45 -04:00
|
|
|
pub struct FilenameCorrection<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
pub typo: &'m str,
|
|
|
|
pub correction: Cow<'m, str>,
|
|
|
|
#[serde(skip)]
|
|
|
|
pub(crate) non_exhaustive: (),
|
|
|
|
}
|
|
|
|
|
2019-01-24 10:24:20 -05:00
|
|
|
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-07-16 21:16:54 -04:00
|
|
|
match msg {
|
|
|
|
Message::BinaryFile(msg) => {
|
|
|
|
println!("Skipping binary file {}", msg.path.display(),);
|
|
|
|
}
|
|
|
|
Message::Correction(msg) => {
|
|
|
|
println!(
|
|
|
|
"{}:{}:{}: {} -> {}",
|
|
|
|
msg.path.display(),
|
|
|
|
msg.line_num,
|
|
|
|
msg.col_num,
|
|
|
|
msg.typo,
|
|
|
|
msg.correction
|
|
|
|
);
|
|
|
|
}
|
2019-07-18 22:20:45 -04:00
|
|
|
Message::FilenameCorrection(msg) => {
|
|
|
|
println!("{}: {} -> {}", msg.path.display(), msg.typo, msg.correction);
|
|
|
|
}
|
2019-07-16 21:16:54 -04:00
|
|
|
}
|
2019-01-24 10:24:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_long(msg: Message) {
|
2019-07-16 21:16:54 -04:00
|
|
|
match msg {
|
|
|
|
Message::BinaryFile(msg) => {
|
|
|
|
println!("Skipping binary file {}", msg.path.display(),);
|
|
|
|
}
|
|
|
|
Message::Correction(msg) => print_long_correction(msg),
|
2019-07-18 22:20:45 -04:00
|
|
|
Message::FilenameCorrection(msg) => {
|
|
|
|
println!(
|
|
|
|
"{}: error: `{}` should be `{}`",
|
|
|
|
msg.path.display(),
|
|
|
|
msg.typo,
|
|
|
|
msg.correction
|
|
|
|
);
|
|
|
|
}
|
2019-07-16 21:16:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_long_correction(msg: Correction) {
|
2019-01-24 10:24:20 -05:00
|
|
|
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
|
|
|
|
2019-06-22 11:18:03 -04: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();
|
2019-06-22 11:18:03 -04:00
|
|
|
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());
|
|
|
|
}
|