#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::path::{Path, PathBuf};
use serde_json::Value;
mod module_scan;
mod projection;
pub use projection::{constitution_json, constitution_text, report_json};
mod cargo_metadata;
pub(crate) use cargo_metadata::*;
mod crate_check;
use crate_check::check_crate_boundary;
mod coverage;
pub use coverage::Coverage;
use coverage::coverage_from;
mod errors;
mod finding;
use errors::unreadable_workspace_error;
#[cfg(test)]
use errors::{
confine_external_crate_on_crate_error, inline_empty_prefix_error, inline_empty_verbs_error,
inline_module_target_error, inline_narrow_and_strict_error,
must_not_be_imported_by_on_crate_error, must_only_be_imported_by_on_crate_error,
restrict_imports_to_on_crate_error, unknown_module_error,
};
mod module_check;
use module_check::check_module_boundary;
mod model;
pub use model::*;
pub use xuanji::{
Baseline, BaselineEntry, BoundaryKind, Finding, FindingKey, Outcome, Polarity, Report,
Severity, Violation, ViolationId, apply_baseline,
};
pub fn check(constitution: &Constitution, manifest_path: &Path) -> Outcome {
match cargo_metadata(manifest_path) {
Ok(metadata) => evaluate(constitution, &metadata),
Err(err) => Outcome::ConstitutionError(unreadable_workspace_error(manifest_path, &err)),
}
}
fn evaluate(constitution: &Constitution, metadata: &Value) -> Outcome {
let workspace = workspace_member_names(metadata);
let mut violations = Vec::new();
for boundary in constitution.boundaries() {
match boundary {
Boundary::Crate(crate_boundary) => {
if let Err(error) =
check_crate_boundary(metadata, &workspace, crate_boundary, &mut violations)
{
return Outcome::ConstitutionError(error);
}
}
Boundary::Module(module_boundary) => {
if let Err(error) =
check_module_boundary(metadata, module_boundary, &mut violations)
{
return Outcome::ConstitutionError(error);
}
}
}
}
let mut deduped: Vec<Violation> = Vec::new();
for violation in std::mem::take(&mut violations) {
match deduped.iter_mut().find(|kept| kept.id() == violation.id()) {
Some(kept) => {
if kept.severity == Severity::Warn && violation.severity == Severity::Enforce {
*kept = violation;
}
}
None => deduped.push(violation),
}
}
violations = deduped;
if violations.is_empty() {
Outcome::Clean
} else {
Outcome::Violations(Report::new(violations))
}
}
pub fn check_and_cover(
constitution: &Constitution,
manifest_path: &Path,
) -> (Outcome, Option<Coverage>) {
let metadata = match cargo_metadata(manifest_path) {
Ok(metadata) => metadata,
Err(err) => {
return (
Outcome::ConstitutionError(unreadable_workspace_error(manifest_path, &err)),
None,
);
}
};
let coverage = coverage_from(workspace_member_names(&metadata), constitution);
(evaluate(constitution, &metadata), Some(coverage))
}
pub fn workspace_member_src_dirs(manifest_path: &Path) -> Result<Vec<PathBuf>, String> {
match cargo_metadata(manifest_path) {
Ok(metadata) => Ok(member_src_dirs(&metadata)),
Err(err) => Err(unreadable_workspace_error(manifest_path, &err)),
}
}
#[cfg(test)]
mod tests;