mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-25 02:20:58 -05:00
refactor(report): Use native types
This commit is contained in:
parent
482d320407
commit
7a1fac7fab
3 changed files with 42 additions and 33 deletions
|
@ -94,7 +94,7 @@ struct ReportContext<'m, 'r> {
|
||||||
|
|
||||||
impl<'m, 'r> report::Report for ReportContext<'m, 'r> {
|
impl<'m, 'r> report::Report for ReportContext<'m, 'r> {
|
||||||
fn report(&self, msg: report::Message) -> bool {
|
fn report(&self, msg: report::Message) -> bool {
|
||||||
let msg = msg.context(self.context.clone());
|
let msg = msg.context(Some(self.context.clone()));
|
||||||
self.reporter.report(msg)
|
self.reporter.report(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,7 +188,7 @@ impl Check for Typos {
|
||||||
Some(corrections) => {
|
Some(corrections) => {
|
||||||
let byte_offset = ident.offset();
|
let byte_offset = ident.offset();
|
||||||
let msg = report::Typo {
|
let msg = report::Typo {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
||||||
byte_offset,
|
byte_offset,
|
||||||
typo: ident.token(),
|
typo: ident.token(),
|
||||||
|
@ -203,7 +203,7 @@ impl Check for Typos {
|
||||||
Some(corrections) => {
|
Some(corrections) => {
|
||||||
let byte_offset = word.offset();
|
let byte_offset = word.offset();
|
||||||
let msg = report::Typo {
|
let msg = report::Typo {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
||||||
byte_offset,
|
byte_offset,
|
||||||
typo: word.token(),
|
typo: word.token(),
|
||||||
|
@ -236,7 +236,7 @@ impl Check for Typos {
|
||||||
Some(corrections) => {
|
Some(corrections) => {
|
||||||
let byte_offset = ident.offset();
|
let byte_offset = ident.offset();
|
||||||
let msg = report::Typo {
|
let msg = report::Typo {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
buffer: std::borrow::Cow::Borrowed(buffer),
|
buffer: std::borrow::Cow::Borrowed(buffer),
|
||||||
byte_offset,
|
byte_offset,
|
||||||
typo: ident.token(),
|
typo: ident.token(),
|
||||||
|
@ -251,7 +251,7 @@ impl Check for Typos {
|
||||||
Some(corrections) => {
|
Some(corrections) => {
|
||||||
let byte_offset = word.offset();
|
let byte_offset = word.offset();
|
||||||
let msg = report::Typo {
|
let msg = report::Typo {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
buffer: std::borrow::Cow::Borrowed(buffer),
|
buffer: std::borrow::Cow::Borrowed(buffer),
|
||||||
byte_offset,
|
byte_offset,
|
||||||
typo: word.token(),
|
typo: word.token(),
|
||||||
|
@ -300,7 +300,7 @@ impl Check for ParseIdentifiers {
|
||||||
let typos_found = false;
|
let typos_found = false;
|
||||||
|
|
||||||
let msg = report::Parse {
|
let msg = report::Parse {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
kind: report::ParseKind::Identifier,
|
kind: report::ParseKind::Identifier,
|
||||||
data: parser.parse_str(buffer).map(|i| i.token()).collect(),
|
data: parser.parse_str(buffer).map(|i| i.token()).collect(),
|
||||||
};
|
};
|
||||||
|
@ -321,7 +321,7 @@ impl Check for ParseIdentifiers {
|
||||||
let typos_found = false;
|
let typos_found = false;
|
||||||
|
|
||||||
let msg = report::Parse {
|
let msg = report::Parse {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
kind: report::ParseKind::Identifier,
|
kind: report::ParseKind::Identifier,
|
||||||
data: parser.parse_bytes(buffer).map(|i| i.token()).collect(),
|
data: parser.parse_bytes(buffer).map(|i| i.token()).collect(),
|
||||||
};
|
};
|
||||||
|
@ -363,7 +363,7 @@ impl Check for ParseWords {
|
||||||
let typos_found = false;
|
let typos_found = false;
|
||||||
|
|
||||||
let msg = report::Parse {
|
let msg = report::Parse {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
kind: report::ParseKind::Word,
|
kind: report::ParseKind::Word,
|
||||||
data: parser
|
data: parser
|
||||||
.parse_str(buffer)
|
.parse_str(buffer)
|
||||||
|
@ -387,7 +387,7 @@ impl Check for ParseWords {
|
||||||
let typos_found = false;
|
let typos_found = false;
|
||||||
|
|
||||||
let msg = report::Parse {
|
let msg = report::Parse {
|
||||||
context: report::Context::None,
|
context: None,
|
||||||
kind: report::ParseKind::Word,
|
kind: report::ParseKind::Word,
|
||||||
data: parser
|
data: parser
|
||||||
.parse_bytes(buffer)
|
.parse_bytes(buffer)
|
||||||
|
|
|
@ -39,7 +39,7 @@ impl<'m> Message<'m> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn context(self, context: Context<'m>) -> Self {
|
pub fn context(self, context: Option<Context<'m>>) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Message::Typo(typo) => {
|
Message::Typo(typo) => {
|
||||||
let typo = typo.context(context);
|
let typo = typo.context(context);
|
||||||
|
@ -65,7 +65,7 @@ pub struct BinaryFile<'m> {
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct Typo<'m> {
|
pub struct Typo<'m> {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub context: Context<'m>,
|
pub context: Option<Context<'m>>,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub buffer: Cow<'m, [u8]>,
|
pub buffer: Cow<'m, [u8]>,
|
||||||
pub byte_offset: usize,
|
pub byte_offset: usize,
|
||||||
|
@ -76,7 +76,7 @@ pub struct Typo<'m> {
|
||||||
impl<'m> Default for Typo<'m> {
|
impl<'m> Default for Typo<'m> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
context: Context::None,
|
context: None,
|
||||||
buffer: Cow::Borrowed(&[]),
|
buffer: Cow::Borrowed(&[]),
|
||||||
byte_offset: 0,
|
byte_offset: 0,
|
||||||
typo: "",
|
typo: "",
|
||||||
|
@ -91,13 +91,6 @@ impl<'m> Default for Typo<'m> {
|
||||||
pub enum Context<'m> {
|
pub enum Context<'m> {
|
||||||
File(FileContext<'m>),
|
File(FileContext<'m>),
|
||||||
Path(PathContext<'m>),
|
Path(PathContext<'m>),
|
||||||
None,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'m> Default for Context<'m> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Context::None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'m> std::fmt::Display for Context<'m> {
|
impl<'m> std::fmt::Display for Context<'m> {
|
||||||
|
@ -105,7 +98,6 @@ impl<'m> std::fmt::Display for Context<'m> {
|
||||||
match self {
|
match self {
|
||||||
Context::File(c) => write!(f, "{}:{}", c.path.display(), c.line_num),
|
Context::File(c) => write!(f, "{}:{}", c.path.display(), c.line_num),
|
||||||
Context::Path(c) => write!(f, "{}", c.path.display()),
|
Context::Path(c) => write!(f, "{}", c.path.display()),
|
||||||
Context::None => Ok(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,7 +164,7 @@ impl<'m> Default for File<'m> {
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct Parse<'m> {
|
pub struct Parse<'m> {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub context: Context<'m>,
|
pub context: Option<Context<'m>>,
|
||||||
pub kind: ParseKind,
|
pub kind: ParseKind,
|
||||||
pub data: Vec<&'m str>,
|
pub data: Vec<&'m str>,
|
||||||
}
|
}
|
||||||
|
@ -180,7 +172,7 @@ pub struct Parse<'m> {
|
||||||
impl<'m> Default for Parse<'m> {
|
impl<'m> Default for Parse<'m> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
context: Context::None,
|
context: None,
|
||||||
kind: ParseKind::Identifier,
|
kind: ParseKind::Identifier,
|
||||||
data: vec![],
|
data: vec![],
|
||||||
}
|
}
|
||||||
|
@ -294,13 +286,15 @@ fn print_brief_correction(msg: &Typo) {
|
||||||
crate::Status::Invalid => {
|
crate::Status::Invalid => {
|
||||||
println!(
|
println!(
|
||||||
"{}:{}: {} is disallowed",
|
"{}:{}: {} is disallowed",
|
||||||
msg.context, msg.byte_offset, msg.typo,
|
context_display(&msg.context),
|
||||||
|
msg.byte_offset,
|
||||||
|
msg.typo,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
crate::Status::Corrections(corrections) => {
|
crate::Status::Corrections(corrections) => {
|
||||||
println!(
|
println!(
|
||||||
"{}:{}: {} -> {}",
|
"{}:{}: {} -> {}",
|
||||||
msg.context,
|
context_display(&msg.context),
|
||||||
msg.byte_offset,
|
msg.byte_offset,
|
||||||
msg.typo,
|
msg.typo,
|
||||||
itertools::join(corrections.iter(), ", ")
|
itertools::join(corrections.iter(), ", ")
|
||||||
|
@ -318,7 +312,9 @@ fn print_long_correction(msg: &Typo) {
|
||||||
writeln!(
|
writeln!(
|
||||||
handle,
|
handle,
|
||||||
"{}:{}: {} is disallowed",
|
"{}:{}: {} is disallowed",
|
||||||
msg.context, msg.byte_offset, msg.typo,
|
context_display(&msg.context),
|
||||||
|
msg.byte_offset,
|
||||||
|
msg.typo,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -332,9 +328,15 @@ fn print_long_correction(msg: &Typo) {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeln!(handle, " --> {}:{}", msg.context, msg.byte_offset).unwrap();
|
writeln!(
|
||||||
|
handle,
|
||||||
|
" --> {}:{}",
|
||||||
|
context_display(&msg.context),
|
||||||
|
msg.byte_offset
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
if let Context::File(context) = &msg.context {
|
if let Some(Context::File(context)) = &msg.context {
|
||||||
let line_num = context.line_num.to_string();
|
let line_num = context.line_num.to_string();
|
||||||
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
|
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
|
||||||
|
|
||||||
|
@ -350,6 +352,13 @@ fn print_long_correction(msg: &Typo) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn context_display<'c>(context: &'c Option<Context<'c>>) -> &'c dyn std::fmt::Display {
|
||||||
|
context
|
||||||
|
.as_ref()
|
||||||
|
.map(|c| c as &dyn std::fmt::Display)
|
||||||
|
.unwrap_or(&"")
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct PrintJson;
|
pub struct PrintJson;
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ impl<'r> typos::report::Report for Replace<'r> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match &typo.context {
|
match &typo.context {
|
||||||
typos::report::Context::File(file) => {
|
Some(typos::report::Context::File(file)) => {
|
||||||
let path = file.path.to_owned();
|
let path = file.path.to_owned();
|
||||||
let line_num = file.line_num;
|
let line_num = file.line_num;
|
||||||
let correction =
|
let correction =
|
||||||
|
@ -82,7 +82,7 @@ impl<'r> typos::report::Report for Replace<'r> {
|
||||||
content.push(correction);
|
content.push(correction);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
typos::report::Context::Path(path) => {
|
Some(typos::report::Context::Path(path)) => {
|
||||||
let path = path.path.to_owned();
|
let path = path.path.to_owned();
|
||||||
let correction =
|
let correction =
|
||||||
Correction::new(typo.byte_offset, typo.typo, corrections[0].as_ref());
|
Correction::new(typo.byte_offset, typo.typo, corrections[0].as_ref());
|
||||||
|
@ -209,12 +209,12 @@ mod test {
|
||||||
let replace = Replace::new(&primary);
|
let replace = Replace::new(&primary);
|
||||||
replace.report(
|
replace.report(
|
||||||
typos::report::Typo::default()
|
typos::report::Typo::default()
|
||||||
.context(
|
.context(Some(
|
||||||
typos::report::FileContext::default()
|
typos::report::FileContext::default()
|
||||||
.path(input_file.path())
|
.path(input_file.path())
|
||||||
.line_num(1)
|
.line_num(1)
|
||||||
.into(),
|
.into(),
|
||||||
)
|
))
|
||||||
.buffer(std::borrow::Cow::Borrowed(b"1 foo 2\n3 4 5"))
|
.buffer(std::borrow::Cow::Borrowed(b"1 foo 2\n3 4 5"))
|
||||||
.byte_offset(2)
|
.byte_offset(2)
|
||||||
.typo("foo")
|
.typo("foo")
|
||||||
|
@ -238,11 +238,11 @@ mod test {
|
||||||
let replace = Replace::new(&primary);
|
let replace = Replace::new(&primary);
|
||||||
replace.report(
|
replace.report(
|
||||||
typos::report::Typo::default()
|
typos::report::Typo::default()
|
||||||
.context(
|
.context(Some(
|
||||||
typos::report::PathContext::default()
|
typos::report::PathContext::default()
|
||||||
.path(input_file.path())
|
.path(input_file.path())
|
||||||
.into(),
|
.into(),
|
||||||
)
|
))
|
||||||
.buffer(std::borrow::Cow::Borrowed(b"foo.txt"))
|
.buffer(std::borrow::Cow::Borrowed(b"foo.txt"))
|
||||||
.byte_offset(0)
|
.byte_offset(0)
|
||||||
.typo("foo")
|
.typo("foo")
|
||||||
|
|
Loading…
Reference in a new issue