#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
use std::path::{Path, PathBuf};
use serde_json::Value;
mod bounds;
pub use bounds::observation_bounds;
mod observer;
pub use observer::StaticObserver;
mod module_scan;
use module_scan::EvaluationScans;
mod projection;
pub use projection::{
StalePolicy, constitution_json, constitution_text, report_json, report_json_with_stale_policy,
stale_policy,
};
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, BoundDecl, BoundId, BoundaryKind, Defence, Demonstrates, Extent,
FactGranularity, Finding, Observer, Outcome, Owner, Polarity, Reached, Report, RuleKey,
ScanDepth, Severity, StructuredFactIdentity, Subject, 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 {
evaluate_with_scans(constitution, metadata, &EvaluationScans::shared())
}
fn evaluate_with_scans(
constitution: &Constitution,
metadata: &Value,
scans: &EvaluationScans,
) -> Outcome {
let workspace = match workspace_member_names(metadata) {
Members::Read(names) => names,
Members::Unreadable(why) => return Outcome::ConstitutionError(why),
};
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, scans, 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() {
match Subject::of(constitution.boundaries().len(), workspace.len()) {
Some(subject) => Outcome::Clean(subject),
None => Outcome::ConstitutionError(format!(
"{} static boundary/boundaries are declared and this workspace has no member to observe \
them over, so nothing was judged — which is not a clean workspace",
constitution.boundaries().len()
)),
}
} 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,
);
}
};
(
evaluate(constitution, &metadata),
coverage_of(&metadata, constitution),
)
}
fn coverage_of(metadata: &Value, constitution: &Constitution) -> Option<Coverage> {
match workspace_member_names(metadata) {
Members::Read(names) => Some(coverage_from(names, constitution)),
Members::Unreadable(_) => None,
}
}
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;