2020-07-04 21:41:32 -04:00
|
|
|
#![allow(clippy::needless_update)]
|
|
|
|
|
2020-11-04 22:14:59 -05: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>),
|
2020-11-04 22:14:59 -05:00
|
|
|
Typo(Typo<'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-06-30 22:08:38 -04:00
|
|
|
impl<'m> Message<'m> {
|
|
|
|
pub fn is_correction(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Message::BinaryFile(_) => false,
|
2020-11-04 22:14:59 -05:00
|
|
|
Message::Typo(c) => c.corrections.is_correction(),
|
2020-06-30 22:08:38 -04:00
|
|
|
Message::File(_) => false,
|
|
|
|
Message::Parse(_) => false,
|
|
|
|
Message::PathError(_) => false,
|
|
|
|
Message::Error(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_error(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Message::BinaryFile(_) => false,
|
2020-11-04 22:14:59 -05:00
|
|
|
Message::Typo(_) => false,
|
2020-06-30 22:08:38 -04:00
|
|
|
Message::File(_) => false,
|
|
|
|
Message::Parse(_) => false,
|
|
|
|
Message::PathError(_) => true,
|
|
|
|
Message::Error(_) => true,
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 07:01:01 -05:00
|
|
|
|
|
|
|
pub fn context(self, context: Context<'m>) -> Self {
|
|
|
|
match self {
|
|
|
|
Message::Typo(typo) => {
|
|
|
|
let typo = typo.context(context);
|
|
|
|
Message::Typo(typo)
|
|
|
|
}
|
|
|
|
Message::Parse(parse) => {
|
|
|
|
let parse = parse.context(context);
|
|
|
|
Message::Parse(parse)
|
|
|
|
}
|
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 22:08:38 -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]
|
2020-11-04 22:14:59 -05:00
|
|
|
pub struct Typo<'m> {
|
2020-11-10 07:10:23 -05:00
|
|
|
#[serde(flatten)]
|
2020-11-04 22:14:59 -05:00
|
|
|
pub context: Context<'m>,
|
2019-01-24 10:24:20 -05:00
|
|
|
#[serde(skip)]
|
2020-11-04 22:14:59 -05:00
|
|
|
pub buffer: Cow<'m, [u8]>,
|
2020-03-31 20:55:07 -04:00
|
|
|
pub byte_offset: usize,
|
2019-06-23 00:01:27 -04:00
|
|
|
pub typo: &'m str,
|
2020-10-24 22:17:16 -04:00
|
|
|
pub corrections: crate::Status<'m>,
|
2019-01-24 10:24:20 -05:00
|
|
|
}
|
|
|
|
|
2020-11-04 22:14:59 -05:00
|
|
|
impl<'m> Default for Typo<'m> {
|
2020-03-24 07:11:31 -04:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-11-04 22:14:59 -05:00
|
|
|
context: Context::None,
|
|
|
|
buffer: Cow::Borrowed(&[]),
|
2020-03-24 07:11:31 -04:00
|
|
|
byte_offset: 0,
|
|
|
|
typo: "",
|
2020-10-24 22:17:16 -04:00
|
|
|
corrections: crate::Status::Invalid,
|
2020-03-24 07:11:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 22:14:59 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
|
2020-11-10 07:10:23 -05:00
|
|
|
#[serde(untagged)]
|
2020-11-04 22:14:59 -05:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Context<'m> {
|
|
|
|
File(FileContext<'m>),
|
|
|
|
Path(PathContext<'m>),
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> Default for Context<'m> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Context::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> std::fmt::Display for Context<'m> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
match self {
|
|
|
|
Context::File(c) => write!(f, "{}:{}", c.path.display(), c.line_num),
|
|
|
|
Context::Path(c) => write!(f, "{}", c.path.display()),
|
|
|
|
Context::None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 07:11:31 -04:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2020-11-04 22:14:59 -05:00
|
|
|
pub struct FileContext<'m> {
|
2019-07-18 22:20:45 -04:00
|
|
|
pub path: &'m std::path::Path,
|
2020-11-04 22:14:59 -05:00
|
|
|
pub line_num: usize,
|
2020-03-24 07:11:31 -04:00
|
|
|
}
|
|
|
|
|
2020-11-04 22:14:59 -05:00
|
|
|
impl<'m> Default for FileContext<'m> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
path: std::path::Path::new("-"),
|
|
|
|
line_num: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct PathContext<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> Default for PathContext<'m> {
|
2020-03-24 07:11:31 -04:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
path: std::path::Path::new("-"),
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 22:20:45 -04:00
|
|
|
}
|
|
|
|
|
2019-10-30 09:26:59 -04:00
|
|
|
#[derive(Copy, Clone, Debug, serde::Serialize)]
|
2020-11-10 07:10:23 -05:00
|
|
|
#[serde(rename_all = "snake_case")]
|
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> {
|
2020-11-10 07:10:23 -05:00
|
|
|
#[serde(flatten)]
|
2020-11-09 20:35:37 -05:00
|
|
|
pub context: Context<'m>,
|
2019-10-30 09:26:59 -04:00
|
|
|
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 {
|
2020-11-09 20:35:37 -05:00
|
|
|
context: Context::None,
|
2020-03-24 07:11:31 -04:00
|
|
|
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 {
|
2020-06-30 22:08:38 -04:00
|
|
|
fn report(&self, msg: Message) -> bool;
|
2020-03-23 19:21:29 -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 PrintSilent;
|
2019-01-24 10:24:20 -05:00
|
|
|
|
2020-03-23 19:21:29 -04:00
|
|
|
impl Report for PrintSilent {
|
2020-06-30 22:08:38 -04:00
|
|
|
fn report(&self, msg: Message) -> bool {
|
|
|
|
msg.is_correction()
|
|
|
|
}
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-06-30 22:08:38 -04:00
|
|
|
fn report(&self, msg: Message) -> bool {
|
|
|
|
match &msg {
|
2020-03-23 19:21:29 -04:00
|
|
|
Message::BinaryFile(msg) => {
|
2020-04-02 19:14:24 -04:00
|
|
|
log::info!("{}", msg);
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
2020-11-04 22:14:59 -05:00
|
|
|
Message::Typo(msg) => print_brief_correction(msg),
|
2020-03-23 19:21:29 -04:00
|
|
|
Message::File(msg) => {
|
|
|
|
println!("{}", msg.path.display());
|
|
|
|
}
|
|
|
|
Message::Parse(msg) => {
|
|
|
|
println!("{}", itertools::join(msg.data.iter(), " "));
|
|
|
|
}
|
|
|
|
Message::PathError(msg) => {
|
2020-04-02 19:14:24 -04:00
|
|
|
log::error!("{}: {}", msg.path.display(), msg.msg);
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
|
|
|
Message::Error(msg) => {
|
2020-04-02 19:14:24 -04:00
|
|
|
log::error!("{}", msg.msg);
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
2019-10-30 09:26:59 -04:00
|
|
|
}
|
2020-06-30 22:08:38 -04:00
|
|
|
msg.is_correction()
|
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 {
|
2020-06-30 22:08:38 -04:00
|
|
|
fn report(&self, msg: Message) -> bool {
|
|
|
|
match &msg {
|
2020-03-23 19:21:29 -04:00
|
|
|
Message::BinaryFile(msg) => {
|
2020-04-02 19:14:24 -04:00
|
|
|
log::info!("{}", msg);
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
2020-11-04 22:14:59 -05:00
|
|
|
Message::Typo(msg) => print_long_correction(msg),
|
2020-03-23 19:21:29 -04:00
|
|
|
Message::File(msg) => {
|
|
|
|
println!("{}", msg.path.display());
|
|
|
|
}
|
|
|
|
Message::Parse(msg) => {
|
|
|
|
println!("{}", itertools::join(msg.data.iter(), " "));
|
|
|
|
}
|
|
|
|
Message::PathError(msg) => {
|
2020-04-02 19:14:24 -04:00
|
|
|
log::error!("{}: {}", msg.path.display(), msg.msg);
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
|
|
|
Message::Error(msg) => {
|
2020-04-02 19:14:24 -04:00
|
|
|
log::error!("{}", msg.msg);
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
2019-10-30 09:26:59 -04:00
|
|
|
}
|
2020-06-30 22:08:38 -04:00
|
|
|
msg.is_correction()
|
2019-07-16 21:16:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 22:14:59 -05:00
|
|
|
fn print_brief_correction(msg: &Typo) {
|
|
|
|
match &msg.corrections {
|
|
|
|
crate::Status::Valid => {}
|
|
|
|
crate::Status::Invalid => {
|
|
|
|
println!(
|
|
|
|
"{}:{}: {} is disallowed",
|
|
|
|
msg.context, msg.byte_offset, msg.typo,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
crate::Status::Corrections(corrections) => {
|
|
|
|
println!(
|
|
|
|
"{}:{}: {} -> {}",
|
|
|
|
msg.context,
|
|
|
|
msg.byte_offset,
|
|
|
|
msg.typo,
|
|
|
|
itertools::join(corrections.iter(), ", ")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 11:18:03 -04:00
|
|
|
|
2020-11-04 22:14:59 -05:00
|
|
|
fn print_long_correction(msg: &Typo) {
|
2019-06-22 11:12:54 -04:00
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
2020-10-24 22:17:16 -04:00
|
|
|
match &msg.corrections {
|
|
|
|
crate::Status::Valid => {}
|
|
|
|
crate::Status::Invalid => {
|
2020-11-04 22:14:59 -05:00
|
|
|
writeln!(
|
|
|
|
handle,
|
|
|
|
"{}:{}: {} is disallowed",
|
|
|
|
msg.context, msg.byte_offset, msg.typo,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-24 22:17:16 -04:00
|
|
|
}
|
|
|
|
crate::Status::Corrections(corrections) => {
|
|
|
|
writeln!(
|
|
|
|
handle,
|
|
|
|
"error: `{}` should be {}",
|
|
|
|
msg.typo,
|
2020-11-04 22:14:59 -05:00
|
|
|
itertools::join(corrections.iter(), ", ")
|
2020-10-24 22:17:16 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 22:14:59 -05:00
|
|
|
writeln!(handle, " --> {}:{}", msg.context, msg.byte_offset).unwrap();
|
|
|
|
|
|
|
|
if let Context::File(context) = &msg.context {
|
|
|
|
let line_num = context.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();
|
|
|
|
let hl: String = itertools::repeat_n("^", msg.typo.len()).collect();
|
|
|
|
|
|
|
|
let line = String::from_utf8_lossy(msg.buffer.as_ref());
|
|
|
|
let line = line.replace("\t", " ");
|
|
|
|
writeln!(handle, "{} |", line_indent).unwrap();
|
|
|
|
writeln!(handle, "{} | {}", line_num, line.trim_end()).unwrap();
|
|
|
|
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 {
|
2020-06-30 22:08:38 -04:00
|
|
|
fn report(&self, msg: Message) -> bool {
|
2020-03-23 19:21:29 -04:00
|
|
|
println!("{}", serde_json::to_string(&msg).unwrap());
|
2020-06-30 22:08:38 -04:00
|
|
|
msg.is_correction()
|
2020-03-23 19:21:29 -04:00
|
|
|
}
|
2019-01-24 10:24:20 -05:00
|
|
|
}
|