2020-07-04 20:41:32 -05:00
|
|
|
#![allow(clippy::needless_update)]
|
|
|
|
|
2020-11-04 21:14:59 -06:00
|
|
|
use std::borrow::Cow;
|
2021-05-04 21:54:25 -05:00
|
|
|
|
|
|
|
pub trait Report: Send + Sync {
|
2024-04-26 21:14:01 -05:00
|
|
|
fn report(&self, msg: Message<'_>) -> Result<(), std::io::Error>;
|
2024-07-23 03:19:02 +08:00
|
|
|
|
|
|
|
fn generate_final_result(&self) -> Result<(), std::io::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-05-04 21:54:25 -05:00
|
|
|
}
|
2019-06-22 09:12:54 -06:00
|
|
|
|
2019-08-07 08:20:18 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
|
2019-07-16 19:16:54 -06:00
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
#[serde(tag = "type")]
|
2020-03-24 06:11:31 -05:00
|
|
|
#[non_exhaustive]
|
2019-07-16 19:16:54 -06:00
|
|
|
pub enum Message<'m> {
|
|
|
|
BinaryFile(BinaryFile<'m>),
|
2020-11-04 21:14:59 -06:00
|
|
|
Typo(Typo<'m>),
|
2023-01-13 20:59:49 -06:00
|
|
|
FileType(FileType<'m>),
|
2019-10-30 07:26:59 -06:00
|
|
|
File(File<'m>),
|
|
|
|
Parse(Parse<'m>),
|
2020-11-23 11:20:12 -06:00
|
|
|
Error(Error<'m>),
|
2019-07-16 19:16:54 -06:00
|
|
|
}
|
|
|
|
|
2020-06-30 21:08:38 -05:00
|
|
|
impl<'m> Message<'m> {
|
2023-08-09 08:21:53 -05:00
|
|
|
pub fn is_typo(&self) -> bool {
|
2020-06-30 21:08:38 -05:00
|
|
|
match self {
|
|
|
|
Message::BinaryFile(_) => false,
|
2023-08-09 08:21:53 -05:00
|
|
|
Message::Typo(c) => !c.corrections.is_valid(),
|
2023-01-13 20:59:49 -06:00
|
|
|
Message::FileType(_) => false,
|
2020-06-30 21:08:38 -05:00
|
|
|
Message::File(_) => false,
|
|
|
|
Message::Parse(_) => false,
|
|
|
|
Message::Error(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_error(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Message::BinaryFile(_) => false,
|
2020-11-04 21:14:59 -06:00
|
|
|
Message::Typo(_) => false,
|
2023-01-13 20:59:49 -06:00
|
|
|
Message::FileType(_) => false,
|
2020-06-30 21:08:38 -05:00
|
|
|
Message::File(_) => false,
|
|
|
|
Message::Parse(_) => false,
|
|
|
|
Message::Error(_) => true,
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 06:01:01 -06:00
|
|
|
|
2020-11-11 12:19:22 -06:00
|
|
|
pub fn context(self, context: Option<Context<'m>>) -> Self {
|
2020-11-10 06:01:01 -06:00
|
|
|
match self {
|
|
|
|
Message::Typo(typo) => {
|
|
|
|
let typo = typo.context(context);
|
|
|
|
Message::Typo(typo)
|
|
|
|
}
|
|
|
|
Message::Parse(parse) => {
|
|
|
|
let parse = parse.context(context);
|
|
|
|
Message::Parse(parse)
|
|
|
|
}
|
2020-11-23 11:20:12 -06:00
|
|
|
Message::Error(error) => {
|
|
|
|
let error = error.context(context);
|
|
|
|
Message::Error(error)
|
|
|
|
}
|
2020-11-10 06:01:01 -06:00
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 21:08:38 -05:00
|
|
|
}
|
|
|
|
|
2020-03-24 06:11:31 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_more::Display, derive_setters::Setters)]
|
2024-08-18 07:24:32 +08:00
|
|
|
#[display("Skipping binary file {}", path.display())]
|
2020-03-24 06:11:31 -05:00
|
|
|
#[non_exhaustive]
|
2019-07-16 19:16:54 -06:00
|
|
|
pub struct BinaryFile<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:11:31 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2020-11-04 21:14:59 -06:00
|
|
|
pub struct Typo<'m> {
|
2020-11-10 06:10:23 -06:00
|
|
|
#[serde(flatten)]
|
2020-11-11 12:19:22 -06:00
|
|
|
pub context: Option<Context<'m>>,
|
2019-01-24 08:24:20 -07:00
|
|
|
#[serde(skip)]
|
2020-11-04 21:14:59 -06:00
|
|
|
pub buffer: Cow<'m, [u8]>,
|
2020-03-31 19:55:07 -05:00
|
|
|
pub byte_offset: usize,
|
2019-06-22 22:01:27 -06:00
|
|
|
pub typo: &'m str,
|
2020-12-30 19:41:08 -06:00
|
|
|
pub corrections: typos::Status<'m>,
|
2019-01-24 08:24:20 -07:00
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for Typo<'_> {
|
2020-03-24 06:11:31 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-11-11 12:19:22 -06:00
|
|
|
context: None,
|
2020-11-04 21:14:59 -06:00
|
|
|
buffer: Cow::Borrowed(&[]),
|
2020-03-24 06:11:31 -05:00
|
|
|
byte_offset: 0,
|
|
|
|
typo: "",
|
2020-12-30 19:41:08 -06:00
|
|
|
corrections: typos::Status::Invalid,
|
2020-03-24 06:11:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:14:59 -06:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
|
2020-11-10 06:10:23 -06:00
|
|
|
#[serde(untagged)]
|
2020-11-04 21:14:59 -06:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Context<'m> {
|
|
|
|
File(FileContext<'m>),
|
|
|
|
Path(PathContext<'m>),
|
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl std::fmt::Display for Context<'_> {
|
2024-04-26 21:14:01 -05:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
2020-11-04 21:14:59 -06:00
|
|
|
match self {
|
|
|
|
Context::File(c) => write!(f, "{}:{}", c.path.display(), c.line_num),
|
|
|
|
Context::Path(c) => write!(f, "{}", c.path.display()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:11:31 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2020-11-04 21:14:59 -06:00
|
|
|
pub struct FileContext<'m> {
|
2019-07-18 20:20:45 -06:00
|
|
|
pub path: &'m std::path::Path,
|
2020-11-04 21:14:59 -06:00
|
|
|
pub line_num: usize,
|
2020-03-24 06:11:31 -05:00
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for FileContext<'_> {
|
2020-11-04 21:14:59 -06:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for PathContext<'_> {
|
2020-03-24 06:11:31 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
path: std::path::Path::new("-"),
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 20:20:45 -06:00
|
|
|
}
|
|
|
|
|
2019-10-30 07:26:59 -06:00
|
|
|
#[derive(Copy, Clone, Debug, serde::Serialize)]
|
2020-11-10 06:10:23 -06:00
|
|
|
#[serde(rename_all = "snake_case")]
|
2020-03-24 06:11:31 -05:00
|
|
|
#[non_exhaustive]
|
2019-10-30 07:26:59 -06:00
|
|
|
pub enum ParseKind {
|
|
|
|
Identifier,
|
|
|
|
Word,
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:59:49 -06:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct FileType<'m> {
|
|
|
|
pub path: &'m std::path::Path,
|
|
|
|
pub file_type: Option<&'m str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'m> FileType<'m> {
|
|
|
|
pub fn new(path: &'m std::path::Path, file_type: Option<&'m str>) -> Self {
|
|
|
|
Self { path, file_type }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for FileType<'_> {
|
2023-01-13 20:59:49 -06:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
path: std::path::Path::new("-"),
|
|
|
|
file_type: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:11:31 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2019-10-30 07:26:59 -06: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 06:11:31 -05:00
|
|
|
Self { path }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for File<'_> {
|
2020-03-24 06:11:31 -05:00
|
|
|
fn default() -> Self {
|
2019-10-30 07:26:59 -06:00
|
|
|
Self {
|
2020-03-24 06:11:31 -05:00
|
|
|
path: std::path::Path::new("-"),
|
2019-10-30 07:26:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 06:11:31 -05:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2019-10-30 07:26:59 -06:00
|
|
|
pub struct Parse<'m> {
|
2020-11-10 06:10:23 -06:00
|
|
|
#[serde(flatten)]
|
2020-11-11 12:19:22 -06:00
|
|
|
pub context: Option<Context<'m>>,
|
2019-10-30 07:26:59 -06:00
|
|
|
pub kind: ParseKind,
|
2020-12-30 18:58:35 -06:00
|
|
|
pub data: &'m str,
|
2019-10-30 07:26:59 -06:00
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for Parse<'_> {
|
2020-03-24 06:11:31 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-11-11 12:19:22 -06:00
|
|
|
context: None,
|
2020-03-24 06:11:31 -05:00
|
|
|
kind: ParseKind::Identifier,
|
2020-12-30 18:58:35 -06:00
|
|
|
data: "",
|
2020-03-24 06:11:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
|
|
|
|
#[non_exhaustive]
|
2020-11-23 11:20:12 -06:00
|
|
|
pub struct Error<'m> {
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub context: Option<Context<'m>>,
|
2019-10-26 20:31:10 -06:00
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Error<'_> {
|
2019-10-26 20:31:10 -06:00
|
|
|
pub fn new(msg: String) -> Self {
|
2020-11-23 11:20:12 -06:00
|
|
|
Self { context: None, msg }
|
2020-03-24 06:11:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-02 11:33:06 -06:00
|
|
|
impl Default for Error<'_> {
|
2020-03-24 06:11:31 -05:00
|
|
|
fn default() -> Self {
|
2020-11-23 11:20:12 -06:00
|
|
|
Self {
|
|
|
|
context: None,
|
|
|
|
msg: "".to_owned(),
|
|
|
|
}
|
2019-10-26 20:31:10 -06:00
|
|
|
}
|
|
|
|
}
|