Skip to content

Commit

Permalink
Rename some types with Error and Result in the name
Browse files Browse the repository at this point in the history
This is confusing since they weren't rust Error or Result types.
  • Loading branch information
autarch committed Sep 3, 2022
1 parent 982a3ee commit 7f660de
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
14 changes: 7 additions & 7 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ impl fmt::Debug for Filter {
}

#[derive(Debug)]
pub struct LintResult {
pub struct LintOutcome {
pub ok: bool,
pub stdout: Option<String>,
pub stderr: Option<String>,
}

pub trait FilterImplementation {
fn tidy(&self, name: &str, path: &Path) -> Result<()>;
fn lint(&self, name: &str, path: &Path) -> Result<LintResult>;
fn lint(&self, name: &str, path: &Path) -> Result<LintOutcome>;
fn filter_key(&self) -> &str;
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl Filter {
Ok(Some(Self::path_was_changed(&full, &info)?))
}

pub fn lint(&self, path: &Path, files: &[PathBuf]) -> Result<Option<LintResult>> {
pub fn lint(&self, path: &Path, files: &[PathBuf]) -> Result<Option<LintOutcome>> {
self.require_is_not_filter_type(FilterType::Tidy)?;

let mut full = self.root.clone();
Expand Down Expand Up @@ -514,7 +514,7 @@ impl FilterImplementation for Command {
}
}

fn lint(&self, name: &str, path: &Path) -> Result<LintResult> {
fn lint(&self, name: &str, path: &Path) -> Result<LintOutcome> {
let mut cmd = self.command_for_path(path, &self.lint_flags);

info!(
Expand All @@ -533,7 +533,7 @@ impl FilterImplementation for Command {
self.expect_stderr,
self.in_dir(path),
) {
Ok(result) => Ok(LintResult {
Ok(result) => Ok(LintOutcome {
ok: !self.lint_failure_exit_codes.contains(&result.exit_code),
stdout: result.stdout,
stderr: result.stderr,
Expand Down Expand Up @@ -583,8 +583,8 @@ mod tests {
Ok(())
}

fn lint(&self, _: &str, _: &Path) -> Result<LintResult> {
Ok(LintResult {
fn lint(&self, _: &str, _: &Path) -> Result<LintOutcome> {
Ok(LintOutcome {
ok: true,
stdout: None,
stderr: None,
Expand Down
66 changes: 33 additions & 33 deletions src/precious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl From<Error> for Exit {
}

#[derive(Debug)]
struct ActionError {
struct ActionFailure {
error: String,
config_key: String,
path: PathBuf,
Expand Down Expand Up @@ -387,7 +387,7 @@ impl<'a> Precious<'a> {
run_filter: R,
) -> Result<Exit>
where
R: Fn(&mut Self, Vec<basepaths::Paths>, &filter::Filter) -> Option<Vec<ActionError>>,
R: Fn(&mut Self, Vec<basepaths::Paths>, &filter::Filter) -> Option<Vec<ActionFailure>>,
{
if filters.is_empty() {
return Err(PreciousError::NoFilters {
Expand All @@ -403,40 +403,40 @@ impl<'a> Precious<'a> {
match self.basepaths()?.paths(cli_paths)? {
None => Ok(self.no_files_exit()),
Some(paths) => {
let mut all_errors: Vec<ActionError> = vec![];
let mut all_failures: Vec<ActionFailure> = vec![];
for f in filters {
if let Some(mut errors) = run_filter(self, paths.clone(), &f) {
all_errors.append(&mut errors);
if let Some(mut failures) = run_filter(self, paths.clone(), &f) {
all_failures.append(&mut failures);
}
}

Ok(self.make_exit(all_errors, action))
Ok(self.make_exit(all_failures, action))
}
}
}

fn make_exit(&self, errors: Vec<ActionError>, action: &str) -> Exit {
let (status, error) = if errors.is_empty() {
fn make_exit(&self, failures: Vec<ActionFailure>, action: &str) -> Exit {
let (status, error) = if failures.is_empty() {
(0, None)
} else {
let red = format!("\x1B[{}m", Color::Red.to_fg_str());
let ansi_off = "\x1B[0m";
let plural = if errors.len() > 1 { 's' } else { '\0' };
let plural = if failures.len() > 1 { 's' } else { '\0' };

let error = format!(
"{}Error{} when {} files:{}\n{}",
red,
plural,
action,
ansi_off,
errors
failures
.iter()
.map(|ae| format!(
.map(|af| format!(
" {} {} [{}]\n {}\n",
self.chars.bullet,
ae.path.display(),
ae.config_key,
ae.error,
af.path.display(),
af.config_key,
af.error,
))
.collect::<Vec<String>>()
.join("")
Expand All @@ -454,9 +454,9 @@ impl<'a> Precious<'a> {
&mut self,
all_paths: Vec<basepaths::Paths>,
t: &filter::Filter,
) -> Option<Vec<ActionError>> {
) -> Option<Vec<ActionFailure>> {
let runner =
|s: &Self, p: &Path, paths: &basepaths::Paths| -> Option<Result<(), ActionError>> {
|s: &Self, p: &Path, paths: &basepaths::Paths| -> Option<Result<(), ActionFailure>> {
match t.tidy(p, &paths.files) {
Ok(Some(true)) => {
if !s.quiet {
Expand Down Expand Up @@ -488,7 +488,7 @@ impl<'a> Precious<'a> {
t.name,
p.display()
);
Some(Err(ActionError {
Some(Err(ActionFailure {
error: format!("{:#}", e),
config_key: t.config_key(),
path: p.to_owned(),
Expand All @@ -504,28 +504,28 @@ impl<'a> Precious<'a> {
&mut self,
all_paths: Vec<basepaths::Paths>,
l: &filter::Filter,
) -> Option<Vec<ActionError>> {
) -> Option<Vec<ActionFailure>> {
let runner = |s: &Self,
p: &Path,
paths: &basepaths::Paths|
-> Option<Result<(), ActionError>> {
-> Option<Result<(), ActionFailure>> {
match l.lint(p, &paths.files) {
Ok(Some(r)) => {
if r.ok {
Ok(Some(lo)) => {
if lo.ok {
if !s.quiet {
println!("{} Passed {}: {}", s.chars.lint_free, l.name, p.display());
}
Some(Ok(()))
} else {
println!("{} Failed {}: {}", s.chars.lint_dirty, l.name, p.display());
if let Some(s) = r.stdout {
if let Some(s) = lo.stdout {
println!("{}", s);
}
if let Some(s) = r.stderr {
if let Some(s) = lo.stderr {
println!("{}", s);
}

Some(Err(ActionError {
Some(Err(ActionFailure {
error: "linting failed".into(),
config_key: l.config_key(),
path: p.to_owned(),
Expand All @@ -540,7 +540,7 @@ impl<'a> Precious<'a> {
l.name,
p.display()
);
Some(Err(ActionError {
Some(Err(ActionFailure {
error: format!("{:#}", e),
config_key: l.config_key(),
path: p.to_owned(),
Expand All @@ -558,20 +558,20 @@ impl<'a> Precious<'a> {
all_paths: Vec<basepaths::Paths>,
f: &filter::Filter,
runner: R,
) -> Option<Vec<ActionError>>
) -> Option<Vec<ActionFailure>>
where
R: Fn(&Self, &Path, &basepaths::Paths) -> Option<Result<(), ActionError>> + Sync,
R: Fn(&Self, &Path, &basepaths::Paths) -> Option<Result<(), ActionFailure>> + Sync,
{
let map = self.path_map(all_paths, f);

let start = Instant::now();
let mut results: Vec<Result<(), ActionError>> = vec![];
let mut results: Vec<Result<(), ActionFailure>> = vec![];
self.thread_pool.install(|| {
results.append(
&mut map
.par_iter()
.filter_map(|(p, paths)| runner(self, p, paths))
.collect::<Vec<Result<(), ActionError>>>(),
.collect::<Vec<Result<(), ActionFailure>>>(),
);
});

Expand All @@ -586,17 +586,17 @@ impl<'a> Precious<'a> {
);
}

let errors = results
let failures = results
.into_iter()
.filter_map(|r| match r {
Ok(_) => None,
Err(e) => Some(e),
})
.collect::<Vec<ActionError>>();
if errors.is_empty() {
.collect::<Vec<ActionFailure>>();
if failures.is_empty() {
None
} else {
Some(errors)
Some(failures)
}
}

Expand Down

0 comments on commit 7f660de

Please sign in to comment.