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:20:18 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
|
2019-07-16 21:16:54 -04:00
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
#[serde(tag = "type")]
|
2020-03-24 07:11:31 -04:00
|
|
|
#[non_exhaustive]
|
2019-07-16 21:16:54 -04:00
|
|
|
pub enum Message<'m> {
|
|
|
|
BinaryFile(BinaryFile<'m>),
|
|
|
|
Correction(Correction<'m>),
|
2020-03-30 20:43:38 -04:00
|
|
|
PathCorrection(PathCorrection<'m>),
|
2019-10-30 09:26:59 -04:00
|
|
|
File(File<'m>),
|
|
|
|
Parse(Parse<'m>),
|
2019-10-26 22:31:10 -04:00
|
|
|
PathError(PathError<'m>),
|
|
|
|
Error(Error),
|
2019-07-16 21:16:54 -04:00
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_more::Display, derive_setters::Setters)]
|
2019-08-07 09:25:55 -04:00
|
|
|
#[display(fmt = "Skipping binary file {}", "path.display()")]
|
2020-03-24 07:11:31 -04:00
|
|
|
#[non_exhaustive]
|
2019-07-16 21:16:54 -04:00
|
|
|
pub struct BinaryFile<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
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,
|
2020-03-31 20:55:07 -04:00
|
|
|
pub byte_offset: 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
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04: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,
|
2020-03-31 20:55:07 -04:00
|
|
|
pub byte_offset: usize,
|
2019-07-18 22:20:45 -04:00
|
|
|
pub typo: &'m str,
|
|
|
|
pub correction: Cow<'m, str>,
|
2020-03-24 07:11:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-30 09:26:59 -04:00
|
|
|
#[derive(Copy, Clone, Debug, serde::Serialize)]
|
2020-03-24 07:11:31 -04:00
|
|
|
#[non_exhaustive]
|
2019-10-30 09:26:59 -04:00
|
|
|
pub enum ParseKind {
|
|
|
|
Identifier,
|
|
|
|
Word,
|
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2019-10-30 09:26:59 -04:00
|
|
|
pub struct File<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> File<'m> {
|
|
|
|
pub fn new(path: &'m std::path::Path) -> Self {
|
2020-03-24 07:11:31 -04:00
|
|
|
Self { path }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> Default for File<'m> {
|
|
|
|
fn default() -> Self {
|
2019-10-30 09:26:59 -04:00
|
|
|
Self {
|
2020-03-24 07:11:31 -04:00
|
|
|
path: std::path::Path::new("-"),
|
2019-10-30 09:26:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2019-10-30 09:26:59 -04:00
|
|
|
pub struct Parse<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
pub kind: ParseKind,
|
|
|
|
pub data: Vec<&'m str>,
|
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
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]
|
2019-10-26 22:31:10 -04:00
|
|
|
pub struct PathError<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
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]
|
2019-10-26 22:31:10 -04:00
|
|
|
pub struct Error {
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
pub fn new(msg: String) -> Self {
|
2020-03-24 07:11:31 -04:00
|
|
|
Self { msg }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Error {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { msg: "".to_owned() }
|
2019-10-26 22:31:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2020-03-31 20:55:07 -04:00
|
|
|
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-10-30 09:26:59 -04:00
|
|
|
}
|
2019-07-16 21:16:54 -04:00
|
|
|
}
|
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);
|
|
|
|
}
|
2019-10-30 09:26:59 -04:00
|
|
|
}
|
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();
|
|
|
|
|
2020-03-31 20:55:07 -04:00
|
|
|
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
|
|
|
|
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,
|
2020-03-31 20:55:07 -04:00
|
|
|
msg.byte_offset
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|