Skip to content

Commit

Permalink
refactor: Change error strategy for future thread use
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Oct 27, 2019
1 parent a1a8ba2 commit 0a2f865
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions typos/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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) {}
Expand All @@ -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);
}
}
}

Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 0a2f865

Please sign in to comment.