Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning anyhow::Result when checking for strictness violations and retrieving packs #155

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 41 additions & 54 deletions src/packs/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::packs::PackSet;

use anyhow::bail;
// External imports
use anyhow::Context;
use rayon::prelude::IntoParallelIterator;
use rayon::prelude::IntoParallelRefIterator;
use rayon::prelude::ParallelIterator;
Expand All @@ -41,40 +42,19 @@ pub struct ViolationIdentifier {
pub fn get_defining_pack<'a>(
violation: &ViolationIdentifier,
packset: &'a PackSet,
) -> &'a Pack {
let defining_pack_result = packset.for_pack(&violation.defining_pack_name);

// For some reason cargo fmt wasn't formatting this closure correctly when passed directly into unwrap_or_else
let error_closure = |_| {
let error_message = format!(
"ViolationIdentifier#defining_pack is {}, but that pack cannot be found in the packset.",
&violation.defining_pack_name
);

panic!("{}", &error_message);
};

defining_pack_result.unwrap_or_else(error_closure)
) -> anyhow::Result<&'a Pack> {
packset.for_pack(&violation.defining_pack_name)
.context(format!("ViolationIdentifier#defining_pack is {}, but that pack cannot be found in the packset.",
&violation.defining_pack_name))
}

pub fn get_referencing_pack<'a>(
violation: &ViolationIdentifier,
packset: &'a PackSet,
) -> &'a Pack {
let referencing_pack_result =
packset.for_pack(&violation.referencing_pack_name);

// For some reason cargo fmt wasn't formatting this closure correctly when passed directly into unwrap_or_else
let error_closure = |_| {
let error_message = format!(
"ViolationIdentifier#referencing_pack is {}, but that pack cannot be found in the packset.",
&violation.referencing_pack_name
);

panic!("{}", &error_message);
};

referencing_pack_result.unwrap_or_else(error_closure)
) -> anyhow::Result<&'a Pack> {
packset.for_pack(&violation.referencing_pack_name)
.context(format!("ViolationIdentifier#referencing_pack is {}, but that pack cannot be found in the packset.",
&violation.referencing_pack_name))
}

#[derive(PartialEq, Clone, Eq, Hash, Debug)]
Expand All @@ -94,7 +74,7 @@ pub(crate) trait CheckerInterface {
&self,
offense: &ViolationIdentifier,
configuration: &Configuration,
) -> bool;
) -> anyhow::Result<bool>;

fn violation_type(&self) -> String;
}
Expand Down Expand Up @@ -182,10 +162,10 @@ impl<'a> CheckAllBuilder<'a> {
}
}

pub fn build(mut self) -> CheckAllResult {
pub fn build(mut self) -> anyhow::Result<CheckAllResult> {
let recorded_violations = &self.configuration.pack_set.all_violations;

CheckAllResult {
Ok(CheckAllResult {
reportable_violations: self
.build_reportable_violations(recorded_violations)
.into_iter()
Expand All @@ -197,11 +177,11 @@ impl<'a> CheckAllBuilder<'a> {
.cloned()
.collect(),
strict_mode_violations: self
.build_strict_mode_violations(recorded_violations)
.build_strict_mode_violations(recorded_violations)?
.into_iter()
.cloned()
.collect(),
}
})
}

fn build_reportable_violations(
Expand Down Expand Up @@ -258,28 +238,37 @@ impl<'a> CheckAllBuilder<'a> {
fn build_strict_mode_violations(
&mut self,
recorded_violations: &'a HashSet<ViolationIdentifier>,
) -> Vec<&'a ViolationIdentifier> {
let mut indexed_checkers: HashMap<
) -> anyhow::Result<Vec<&'a ViolationIdentifier>> {
let indexed_checkers: HashMap<
String,
&Box<dyn CheckerInterface + Send + Sync>,
> = HashMap::new();
for checker in &self.found_violations.checkers {
indexed_checkers.insert(checker.violation_type(), checker);
}
> = self
.found_violations
.checkers
.iter()
.map(|checker| (checker.violation_type(), checker))
.collect();

let strict_mode_violations: Vec<&'a ViolationIdentifier> =
recorded_violations
.iter()
.filter(|v| {
indexed_checkers
.get(&v.violation_type)
.unwrap()
.is_strict_mode_violation(v, self.configuration)
})
.collect();
strict_mode_violations
recorded_violations
.iter()
.try_fold(vec![], |mut acc, violation| {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent far too long trying to do this with filter_map and transpose. Then I discovered try_fold!

let checker = indexed_checkers
.get(&violation.violation_type)
.context(format!(
"Checker for violation type {} not found",
violation.violation_type
))?;

if checker
.is_strict_mode_violation(violation, self.configuration)?
{
acc.push(violation);
}
Ok(acc)
})
}
}

pub(crate) fn check_all(
configuration: &Configuration,
files: Vec<String>,
Expand All @@ -297,9 +286,7 @@ pub(crate) fn check_all(
absolute_paths,
violations,
};
let check_all_result =
CheckAllBuilder::new(configuration, &found_violations).build();
Ok(check_all_result)
CheckAllBuilder::new(configuration, &found_violations).build()
}

fn validate(configuration: &Configuration) -> Vec<String> {
Expand Down
6 changes: 3 additions & 3 deletions src/packs/checker/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ impl CheckerInterface for Checker {
&self,
violation: &ViolationIdentifier,
configuration: &Configuration,
) -> bool {
) -> anyhow::Result<bool> {
let referencing_pack =
get_referencing_pack(violation, &configuration.pack_set);
get_referencing_pack(violation, &configuration.pack_set)?;

referencing_pack.enforce_architecture().is_strict()
Ok(referencing_pack.enforce_architecture().is_strict())
}

fn violation_type(&self) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/packs/checker/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ impl CheckerInterface for Checker {
&self,
violation: &ViolationIdentifier,
configuration: &Configuration,
) -> bool {
) -> anyhow::Result<bool> {
let referencing_pack =
get_referencing_pack(violation, &configuration.pack_set);
get_referencing_pack(violation, &configuration.pack_set)?;

referencing_pack.enforce_dependencies().is_strict()
Ok(referencing_pack.enforce_dependencies().is_strict())
}

fn violation_type(&self) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/packs/checker/folder_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl CheckerInterface for Checker {
&self,
violation: &ViolationIdentifier,
configuration: &Configuration,
) -> bool {
) -> anyhow::Result<bool> {
let referencing_pack =
get_referencing_pack(violation, &configuration.pack_set);
get_referencing_pack(violation, &configuration.pack_set)?;

referencing_pack.enforce_folder_visibility().is_strict()
Ok(referencing_pack.enforce_folder_visibility().is_strict())
}

fn violation_type(&self) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/packs/checker/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ impl CheckerInterface for Checker {
&self,
violation: &ViolationIdentifier,
configuration: &Configuration,
) -> bool {
) -> anyhow::Result<bool> {
let defining_pack =
get_defining_pack(violation, &configuration.pack_set);
get_defining_pack(violation, &configuration.pack_set)?;

defining_pack.enforce_privacy().is_strict()
Ok(defining_pack.enforce_privacy().is_strict())
}

fn violation_type(&self) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/packs/checker/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ impl CheckerInterface for Checker {
&self,
violation: &ViolationIdentifier,
configuration: &Configuration,
) -> bool {
) -> anyhow::Result<bool> {
let defining_pack =
get_defining_pack(violation, &configuration.pack_set);
get_defining_pack(violation, &configuration.pack_set)?;

defining_pack.enforce_visibility().is_strict()
Ok(defining_pack.enforce_visibility().is_strict())
}

fn violation_type(&self) -> String {
Expand Down
Loading