typos/crates/typos-cli/src/report.rs

233 lines
5.4 KiB
Rust
Raw Normal View History

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