typos/typos/src/report.rs

273 lines
6.7 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
2019-06-22 11:12:54 -04:00
use std::io::{self, Write};
2019-08-07 09:20:18 -04:00
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum Message<'m> {
BinaryFile(BinaryFile<'m>),
Correction(Correction<'m>),
2020-03-30 20:43:38 -04:00
PathCorrection(PathCorrection<'m>),
File(File<'m>),
Parse(Parse<'m>),
PathError(PathError<'m>),
Error(Error),
}
#[derive(Clone, Debug, serde::Serialize, derive_more::Display, derive_setters::Setters)]
#[display(fmt = "Skipping binary file {}", "path.display()")]
#[non_exhaustive]
pub struct BinaryFile<'m> {
pub path: &'m std::path::Path,
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
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 byte_offset: 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
}
impl<'m> Default for Correction<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
line: b"",
line_num: 0,
byte_offset: 0,
typo: "",
correction: Cow::Borrowed(""),
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
2020-03-30 20:43:38 -04:00
pub struct PathCorrection<'m> {
2019-07-18 22:20:45 -04:00
pub path: &'m std::path::Path,
pub byte_offset: usize,
2019-07-18 22:20:45 -04:00
pub typo: &'m str,
pub correction: Cow<'m, str>,
}
impl<'m> Default for PathCorrection<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
byte_offset: 0,
typo: "",
correction: Cow::Borrowed(""),
}
}
2019-07-18 22:20:45 -04:00
}
#[derive(Copy, Clone, Debug, serde::Serialize)]
#[non_exhaustive]
pub enum ParseKind {
Identifier,
Word,
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct File<'m> {
pub path: &'m std::path::Path,
}
impl<'m> File<'m> {
pub fn new(path: &'m std::path::Path) -> Self {
Self { path }
}
}
impl<'m> Default for File<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Parse<'m> {
pub path: &'m std::path::Path,
pub kind: ParseKind,
pub data: Vec<&'m str>,
}
impl<'m> Default for Parse<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
kind: ParseKind::Identifier,
data: vec![],
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct PathError<'m> {
pub path: &'m std::path::Path,
pub msg: String,
}
impl<'m> Default for PathError<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
msg: "".to_owned(),
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Error {
pub msg: String,
}
impl Error {
pub fn new(msg: String) -> Self {
Self { msg }
}
}
impl Default for Error {
fn default() -> Self {
Self { msg: "".to_owned() }
}
}
2020-03-23 19:21:29 -04:00
pub trait Report: Send + Sync {
fn report(&self, msg: Message);
}
2019-01-24 10:24:20 -05:00
2020-03-23 21:27:37 -04:00
#[derive(Copy, Clone, Debug)]
2020-03-23 19:21:29 -04:00
pub struct PrintSilent;
2019-01-24 10:24:20 -05:00
2020-03-23 19:21:29 -04:00
impl Report for PrintSilent {
fn report(&self, _msg: Message) {}
}
2020-03-23 21:27:37 -04:00
#[derive(Copy, Clone, Debug)]
2020-03-23 19:21:29 -04:00
pub struct PrintBrief;
impl Report for PrintBrief {
fn report(&self, msg: Message) {
match msg {
Message::BinaryFile(msg) => {
println!("{}", msg);
}
Message::Correction(msg) => {
println!(
"{}:{}:{}: {} -> {}",
msg.path.display(),
msg.line_num,
msg.byte_offset,
2020-03-23 19:21:29 -04:00
msg.typo,
msg.correction
);
}
2020-03-30 20:43:38 -04:00
Message::PathCorrection(msg) => {
2020-03-23 19:21:29 -04:00
println!("{}: {} -> {}", msg.path.display(), msg.typo, msg.correction);
}
Message::File(msg) => {
println!("{}", msg.path.display());
}
Message::Parse(msg) => {
println!("{}", itertools::join(msg.data.iter(), " "));
}
Message::PathError(msg) => {
println!("{}: {}", msg.path.display(), msg.msg);
}
Message::Error(msg) => {
println!("{}", msg.msg);
}
}
}
2019-01-24 10:24:20 -05:00
}
2020-03-23 21:27:37 -04:00
#[derive(Copy, Clone, Debug)]
2020-03-23 19:21:29 -04:00
pub struct PrintLong;
impl Report for PrintLong {
fn report(&self, msg: Message) {
match msg {
Message::BinaryFile(msg) => {
println!("{}", msg);
}
Message::Correction(msg) => print_long_correction(msg),
2020-03-30 20:43:38 -04:00
Message::PathCorrection(msg) => {
2020-03-23 19:21:29 -04:00
println!(
"{}: error: `{}` should be `{}`",
msg.path.display(),
msg.typo,
msg.correction
);
}
Message::File(msg) => {
println!("{}", msg.path.display());
}
Message::Parse(msg) => {
println!("{}", itertools::join(msg.data.iter(), " "));
}
Message::PathError(msg) => {
println!("{}: {}", msg.path.display(), msg.msg);
}
Message::Error(msg) => {
println!("{}", msg.msg);
}
}
}
}
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.byte_offset).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.byte_offset
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
}
2020-03-23 21:27:37 -04:00
#[derive(Copy, Clone, Debug)]
2020-03-23 19:21:29 -04:00
pub struct PrintJson;
impl Report for PrintJson {
fn report(&self, msg: Message) {
println!("{}", serde_json::to_string(&msg).unwrap());
}
2019-01-24 10:24:20 -05:00
}