mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-25 02:20:58 -05:00
refactor: Change error strategy for future thread use
This commit is contained in:
parent
a1a8ba2268
commit
0a2f865d0f
2 changed files with 70 additions and 9 deletions
41
src/main.rs
41
src/main.rs
|
@ -266,6 +266,27 @@ pub fn init_logging(level: Option<log::Level>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_entry(
|
||||
entry: Result<ignore::DirEntry, ignore::Error>,
|
||||
args: &Args,
|
||||
checks: &typos::checks::Checks,
|
||||
) -> Result<bool, failure::Error> {
|
||||
let mut typos_found = false;
|
||||
|
||||
let entry = entry?;
|
||||
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
|
||||
let explicit = entry.depth() == 0;
|
||||
if checks.check_filename(entry.path(), args.format.report())? {
|
||||
typos_found = true;
|
||||
}
|
||||
if checks.check_file(entry.path(), explicit, args.format.report())? {
|
||||
typos_found = true;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(typos_found)
|
||||
}
|
||||
|
||||
fn run() -> Result<i32, failure::Error> {
|
||||
let args = Args::from_args();
|
||||
|
||||
|
@ -279,6 +300,7 @@ fn run() -> Result<i32, failure::Error> {
|
|||
let config = config;
|
||||
|
||||
let mut typos_found = false;
|
||||
let mut errors_found = false;
|
||||
for path in args.path.iter() {
|
||||
let path = path.canonicalize()?;
|
||||
let cwd = if path.is_file() {
|
||||
|
@ -318,20 +340,21 @@ fn run() -> Result<i32, failure::Error> {
|
|||
.git_exclude(config.files.ignore_vcs())
|
||||
.parents(config.files.ignore_parent());
|
||||
for entry in walk.build() {
|
||||
let entry = entry?;
|
||||
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
|
||||
let explicit = entry.depth() == 0;
|
||||
if checks.check_filename(entry.path(), args.format.report())? {
|
||||
typos_found = true;
|
||||
}
|
||||
if checks.check_file(entry.path(), explicit, args.format.report())? {
|
||||
typos_found = true;
|
||||
match check_entry(entry, &args, &checks) {
|
||||
Ok(true) => typos_found = true,
|
||||
Err(err) => {
|
||||
let msg = typos::report::Error::new(err.to_string());
|
||||
args.format.report()(msg.into());
|
||||
errors_found = true
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if typos_found {
|
||||
if errors_found {
|
||||
Ok(2)
|
||||
} else if typos_found {
|
||||
Ok(1)
|
||||
} else {
|
||||
Ok(0)
|
||||
|
|
|
@ -8,6 +8,8 @@ pub enum Message<'m> {
|
|||
BinaryFile(BinaryFile<'m>),
|
||||
Correction(Correction<'m>),
|
||||
FilenameCorrection(FilenameCorrection<'m>),
|
||||
PathError(PathError<'m>),
|
||||
Error(Error),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, derive_more::Display)]
|
||||
|
@ -40,6 +42,30 @@ pub struct FilenameCorrection<'m> {
|
|||
pub(crate) non_exhaustive: (),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize)]
|
||||
pub struct PathError<'m> {
|
||||
pub path: &'m std::path::Path,
|
||||
pub msg: String,
|
||||
#[serde(skip)]
|
||||
pub(crate) non_exhaustive: (),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize)]
|
||||
pub struct Error {
|
||||
pub msg: String,
|
||||
#[serde(skip)]
|
||||
pub(crate) non_exhaustive: (),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn new(msg: String) -> Self {
|
||||
Self {
|
||||
msg,
|
||||
non_exhaustive: (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Report = fn(msg: Message);
|
||||
|
||||
pub fn print_silent(_: Message) {}
|
||||
|
@ -62,6 +88,12 @@ pub fn print_brief(msg: Message) {
|
|||
Message::FilenameCorrection(msg) => {
|
||||
println!("{}: {} -> {}", msg.path.display(), msg.typo, msg.correction);
|
||||
}
|
||||
Message::PathError(msg) => {
|
||||
println!("{}: {}", msg.path.display(), msg.msg);
|
||||
}
|
||||
Message::Error(msg) => {
|
||||
println!("{}", msg.msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,6 +111,12 @@ pub fn print_long(msg: Message) {
|
|||
msg.correction
|
||||
);
|
||||
}
|
||||
Message::PathError(msg) => {
|
||||
println!("{}: {}", msg.path.display(), msg.msg);
|
||||
}
|
||||
Message::Error(msg) => {
|
||||
println!("{}", msg.msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue