mirror of
https://github.com/crate-ci/typos.git
synced 2024-12-22 23:52:12 -05:00
refactor: Don't special case --files
This commit is contained in:
parent
628c011f77
commit
b7700fa214
2 changed files with 198 additions and 163 deletions
|
@ -149,6 +149,10 @@ impl TyposSettings {
|
|||
binary: self.binary,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_files(&self) -> Files {
|
||||
Files {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TyposSettings {
|
||||
|
@ -161,6 +165,123 @@ impl Default for TyposSettings {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Typos {
|
||||
check_filenames: bool,
|
||||
check_files: bool,
|
||||
binary: bool,
|
||||
}
|
||||
|
||||
impl Check for Typos {
|
||||
fn check_str(
|
||||
&self,
|
||||
buffer: &str,
|
||||
parser: &tokens::Parser,
|
||||
dictionary: &dyn Dictionary,
|
||||
reporter: &dyn report::Report,
|
||||
) -> Result<bool, crate::Error> {
|
||||
let mut typos_found = false;
|
||||
|
||||
for ident in parser.parse_str(buffer) {
|
||||
match dictionary.correct_ident(ident) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = ident.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
||||
byte_offset,
|
||||
typo: ident.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {
|
||||
for word in ident.split() {
|
||||
match dictionary.correct_word(word) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = word.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
||||
byte_offset,
|
||||
typo: word.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(typos_found)
|
||||
}
|
||||
|
||||
fn check_bytes(
|
||||
&self,
|
||||
buffer: &[u8],
|
||||
parser: &tokens::Parser,
|
||||
dictionary: &dyn Dictionary,
|
||||
reporter: &dyn report::Report,
|
||||
) -> Result<bool, crate::Error> {
|
||||
let mut typos_found = false;
|
||||
|
||||
for ident in parser.parse_bytes(buffer) {
|
||||
match dictionary.correct_ident(ident) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = ident.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer),
|
||||
byte_offset,
|
||||
typo: ident.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {
|
||||
for word in ident.split() {
|
||||
match dictionary.correct_word(word) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = word.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer),
|
||||
byte_offset,
|
||||
typo: word.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(typos_found)
|
||||
}
|
||||
|
||||
fn check_filenames(&self) -> bool {
|
||||
self.check_filenames
|
||||
}
|
||||
|
||||
fn check_files(&self) -> bool {
|
||||
self.check_files
|
||||
}
|
||||
|
||||
fn binary(&self) -> bool {
|
||||
self.binary
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParseIdentifiers {
|
||||
check_filenames: bool,
|
||||
|
@ -294,119 +415,68 @@ impl Check for ParseWords {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Typos {
|
||||
check_filenames: bool,
|
||||
check_files: bool,
|
||||
binary: bool,
|
||||
}
|
||||
pub struct Files {}
|
||||
|
||||
impl Check for Typos {
|
||||
impl Check for Files {
|
||||
fn check_str(
|
||||
&self,
|
||||
buffer: &str,
|
||||
parser: &tokens::Parser,
|
||||
dictionary: &dyn Dictionary,
|
||||
reporter: &dyn report::Report,
|
||||
_buffer: &str,
|
||||
_parser: &tokens::Parser,
|
||||
_dictionary: &dyn Dictionary,
|
||||
_reporter: &dyn report::Report,
|
||||
) -> Result<bool, crate::Error> {
|
||||
let mut typos_found = false;
|
||||
|
||||
for ident in parser.parse_str(buffer) {
|
||||
match dictionary.correct_ident(ident) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = ident.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
||||
byte_offset,
|
||||
typo: ident.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {
|
||||
for word in ident.split() {
|
||||
match dictionary.correct_word(word) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = word.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()),
|
||||
byte_offset,
|
||||
typo: word.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let typos_found = false;
|
||||
Ok(typos_found)
|
||||
}
|
||||
|
||||
fn check_bytes(
|
||||
&self,
|
||||
buffer: &[u8],
|
||||
parser: &tokens::Parser,
|
||||
dictionary: &dyn Dictionary,
|
||||
reporter: &dyn report::Report,
|
||||
_buffer: &[u8],
|
||||
_parser: &tokens::Parser,
|
||||
_dictionary: &dyn Dictionary,
|
||||
_reporter: &dyn report::Report,
|
||||
) -> Result<bool, crate::Error> {
|
||||
let mut typos_found = false;
|
||||
|
||||
for ident in parser.parse_bytes(buffer) {
|
||||
match dictionary.correct_ident(ident) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = ident.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer),
|
||||
byte_offset,
|
||||
typo: ident.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {
|
||||
for word in ident.split() {
|
||||
match dictionary.correct_word(word) {
|
||||
Some(Status::Valid) => {}
|
||||
Some(corrections) => {
|
||||
let byte_offset = word.offset();
|
||||
let msg = report::Typo {
|
||||
context: report::Context::None,
|
||||
buffer: std::borrow::Cow::Borrowed(buffer),
|
||||
byte_offset,
|
||||
typo: word.token(),
|
||||
corrections,
|
||||
};
|
||||
typos_found |= reporter.report(msg.into());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let typos_found = false;
|
||||
Ok(typos_found)
|
||||
}
|
||||
|
||||
fn check_filenames(&self) -> bool {
|
||||
self.check_filenames
|
||||
true
|
||||
}
|
||||
|
||||
fn check_files(&self) -> bool {
|
||||
self.check_files
|
||||
true
|
||||
}
|
||||
|
||||
fn binary(&self) -> bool {
|
||||
self.binary
|
||||
true
|
||||
}
|
||||
|
||||
fn check_filename(
|
||||
&self,
|
||||
_path: &std::path::Path,
|
||||
_parser: &tokens::Parser,
|
||||
_dictionary: &dyn Dictionary,
|
||||
_reporter: &dyn report::Report,
|
||||
) -> Result<bool, crate::Error> {
|
||||
let typos_found = false;
|
||||
Ok(typos_found)
|
||||
}
|
||||
|
||||
fn check_file(
|
||||
&self,
|
||||
path: &std::path::Path,
|
||||
_explicit: bool,
|
||||
_parser: &tokens::Parser,
|
||||
_dictionary: &dyn Dictionary,
|
||||
reporter: &dyn report::Report,
|
||||
) -> Result<bool, crate::Error> {
|
||||
let typos_found = false;
|
||||
|
||||
let msg = report::File::new(path);
|
||||
reporter.report(msg.into());
|
||||
|
||||
Ok(typos_found)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
105
src/main.rs
105
src/main.rs
|
@ -3,7 +3,6 @@
|
|||
extern crate clap;
|
||||
|
||||
use std::io::Write;
|
||||
use std::sync::atomic;
|
||||
|
||||
use structopt::StructOpt;
|
||||
|
||||
|
@ -91,77 +90,43 @@ fn run() -> Result<i32, anyhow::Error> {
|
|||
reporter = &replace_reporter;
|
||||
}
|
||||
|
||||
if args.files {
|
||||
if single_threaded {
|
||||
for entry in walk.build() {
|
||||
match entry {
|
||||
Ok(entry) => {
|
||||
let msg = typos::report::File::new(entry.path());
|
||||
reporter.report(msg.into());
|
||||
}
|
||||
Err(err) => {
|
||||
let msg = typos::report::Error::new(err.to_string());
|
||||
reporter.report(msg.into());
|
||||
errors_found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let atomic_errors = atomic::AtomicBool::new(errors_found);
|
||||
walk.build_parallel().run(|| {
|
||||
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
|
||||
match entry {
|
||||
Ok(entry) => {
|
||||
let msg = typos::report::File::new(entry.path());
|
||||
reporter.report(msg.into());
|
||||
}
|
||||
Err(err) => {
|
||||
let msg = typos::report::Error::new(err.to_string());
|
||||
reporter.report(msg.into());
|
||||
atomic_errors.store(true, atomic::Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
ignore::WalkState::Continue
|
||||
})
|
||||
});
|
||||
errors_found = atomic_errors.into_inner();
|
||||
}
|
||||
let (files, identifier_parser, word_parser, checks);
|
||||
let selected_checks: &dyn typos::checks::Check = if args.files {
|
||||
files = settings.build_files();
|
||||
&files
|
||||
} else if args.identifiers {
|
||||
identifier_parser = settings.build_identifier_parser();
|
||||
&identifier_parser
|
||||
} else if args.words {
|
||||
word_parser = settings.build_word_parser();
|
||||
&word_parser
|
||||
} else {
|
||||
let (identifier_parser, word_parser, checks);
|
||||
let selected_checks: &dyn typos::checks::Check = if args.identifiers {
|
||||
identifier_parser = settings.build_identifier_parser();
|
||||
&identifier_parser
|
||||
} else if args.words {
|
||||
word_parser = settings.build_word_parser();
|
||||
&word_parser
|
||||
} else {
|
||||
checks = settings.build_typos();
|
||||
&checks
|
||||
};
|
||||
checks = settings.build_typos();
|
||||
&checks
|
||||
};
|
||||
|
||||
let (cur_typos, cur_errors) = if single_threaded {
|
||||
checks::check_path(
|
||||
walk.build(),
|
||||
selected_checks,
|
||||
&parser,
|
||||
&dictionary,
|
||||
reporter,
|
||||
)
|
||||
} else {
|
||||
checks::check_path_parallel(
|
||||
walk.build_parallel(),
|
||||
selected_checks,
|
||||
&parser,
|
||||
&dictionary,
|
||||
reporter,
|
||||
)
|
||||
};
|
||||
if cur_typos {
|
||||
typos_found = true;
|
||||
}
|
||||
if cur_errors {
|
||||
errors_found = true;
|
||||
}
|
||||
let (cur_typos, cur_errors) = if single_threaded {
|
||||
checks::check_path(
|
||||
walk.build(),
|
||||
selected_checks,
|
||||
&parser,
|
||||
&dictionary,
|
||||
reporter,
|
||||
)
|
||||
} else {
|
||||
checks::check_path_parallel(
|
||||
walk.build_parallel(),
|
||||
selected_checks,
|
||||
&parser,
|
||||
&dictionary,
|
||||
reporter,
|
||||
)
|
||||
};
|
||||
if cur_typos {
|
||||
typos_found = true;
|
||||
}
|
||||
if cur_errors {
|
||||
errors_found = true;
|
||||
}
|
||||
|
||||
if args.write_changes {
|
||||
|
|
Loading…
Reference in a new issue