use super::*;
use serde_json::Value;
use xuanji::pretty_json;
pub fn report_json(
outcome: &Outcome,
stale: &[ViolationId],
coverage: Option<&Coverage>,
) -> String {
let (label, violations, error) = match outcome {
Outcome::Clean => ("clean", Vec::new(), Value::Null),
Outcome::Violations(report) => (
"violations",
report.violations.iter().map(Violation::to_json).collect(),
Value::Null,
),
Outcome::ConstitutionError(message) => (
"constitution_error",
Vec::new(),
Value::String(message.clone()),
),
_ => ("unknown", Vec::new(), Value::Null),
};
let stale_baseline: Vec<Value> = stale
.iter()
.map(|id| {
serde_json::json!({
"target": id.target,
"rule": id.rule,
"finding": id.finding,
"finding_key": id.finding_key().map(xuanji::FindingKey::to_json),
})
})
.collect();
let mut document = serde_json::json!({
"outcome": label,
"exit_code": outcome.exit_code(),
"violations": violations,
"stale_baseline": stale_baseline,
"error": error,
});
if let Some(coverage) = coverage {
document["coverage"] = serde_json::json!({
"workspace_crates": coverage.total,
"uncovered": coverage.uncovered,
});
}
pretty_json(&document)
}
pub fn constitution_text(constitution: &Constitution) -> String {
let boundaries = constitution.boundaries();
let noun = if boundaries.len() == 1 {
"boundary"
} else {
"boundaries"
};
let mut out = format!(
"Constitution: {} ({} {noun})\n",
constitution.name(),
boundaries.len()
);
for boundary in boundaries {
let (severity, target, rule, reason) = match boundary {
Boundary::Crate(b) => {
let rule = match dependency_kind_label(b.dependency_kind()) {
Some(kind) => format!("{} ({kind} dependencies)", b.rule().text()),
None => b.rule().text(),
};
(
b.severity(),
format!("crate {}", b.target().package),
rule,
b.reason(),
)
}
Boundary::Module(b) => (
b.severity,
format!("module {} in {}", b.module, b.crate_package),
b.rule.text(),
b.reason.as_str(),
),
};
out.push_str(&format!(
"\n[{}] {target}\n rule: {rule}\n reason: {reason}\n",
severity.as_str()
));
}
out
}
pub fn constitution_json(constitution: &Constitution) -> String {
let boundaries: Vec<Value> = constitution
.boundaries()
.iter()
.map(boundary_json)
.collect();
let document = serde_json::json!({
"constitution": constitution.name(),
"boundaries": boundaries,
});
pretty_json(&document)
}
fn boundary_json(boundary: &Boundary) -> Value {
match boundary {
Boundary::Crate(b) => {
let mut object = serde_json::json!({
"kind": "crate",
"target": b.target().package,
"rule": b.rule().label(),
"severity": b.severity().as_str(),
"reason": b.reason(),
});
for (key, value) in b.rule().json_params() {
object[key] = value;
}
if let Some(kind) = dependency_kind_label(b.dependency_kind()) {
object["dependency_kind"] = serde_json::json!(kind);
}
if let Some(anchor) = b.anchor() {
object["anchor"] = serde_json::json!(anchor);
}
object
}
Boundary::Module(b) => {
let mut object = serde_json::json!({
"kind": "module",
"target": b.module,
"crate": b.crate_package,
"rule": b.rule.label(),
"severity": b.severity.as_str(),
"reason": b.reason,
});
for (key, value) in b.rule.json_params() {
object[key] = value;
}
if let Some(anchor) = b.anchor() {
object["anchor"] = serde_json::json!(anchor);
}
object
}
}
}
fn dependency_kind_label(kind: DependencyKind) -> Option<&'static str> {
match kind {
DependencyKind::Normal => None,
DependencyKind::Dev => Some("dev"),
DependencyKind::Build => Some("build"),
}
}