typos/src/report.rs

138 lines
3.6 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
2019-06-22 11:12:54 -04:00
use std::io::{self, Write};
#[derive(Clone, Debug, serde::Serialize)]
#[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>),
}
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)
}
}
#[derive(Clone, Debug, serde::Serialize)]
pub struct BinaryFile<'m> {
pub path: &'m std::path::Path,
#[serde(skip)]
pub(crate) non_exhaustive: (),
}
#[derive(Clone, Debug, serde::Serialize)]
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,
pub correction: Cow<'m, str>,
2019-01-24 10:24:20 -05:00
#[serde(skip)]
pub(crate) non_exhaustive: (),
}
#[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) {
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-01-24 10:24:20 -05:00
}
pub fn print_long(msg: Message) {
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
);
}
}
}
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
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());
}