Skip to content

Commit

Permalink
Migrate the JSON literal report to proper types
Browse files Browse the repository at this point in the history
This makes it more plausible for this format to be made available in a subcrate to allow other
libraries/tools to easily deserialize it. Also makes it more obvious the places where we're
being weirdly inconsistent or exposing random internals because I was lazy/hasty.

A weird side-effect of this is that the order of the fields got shuffled around. It seems
the json macro will sort fields by name, while proper derives use the decl order. The latter
seems preferred anyway, but unfortunately we take a huge churn to the snapshots, obfuscating that this is a semantic noop.
  • Loading branch information
Gankra committed Oct 31, 2022
1 parent a07f817 commit 09e14c2
Show file tree
Hide file tree
Showing 65 changed files with 5,422 additions and 5,303 deletions.
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,9 @@ pub enum FetchMode {

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum OutputFormat {
/// Print output in a human-readable form.
Human,
/// Print output in a machine-readable form with minimal extra context.
Json,
}

Expand Down
77 changes: 77 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Details of the file formats used by cargo vet
use crate::resolver::{DiffRecommendation, ViolationConflict};
use crate::serialization::spanned::Spanned;
use crate::{flock::Filesystem, serialization};
use core::{cmp, fmt};
Expand Down Expand Up @@ -532,3 +533,79 @@ pub struct CommandHistory {
#[serde(flatten)]
pub last_fetch: Option<FetchCommand>,
}

////////////////////////////////////////////////////////////////////////////////////
// //
// //
// //
// <json report output> //
// //
// //
// //
////////////////////////////////////////////////////////////////////////////////////

/// A string of the form "package:version"
pub type PackageAndVersion = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonPackage {
pub name: PackageName,
pub version: Version,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonReport {
#[serde(flatten)]
pub conclusion: JsonReportConclusion,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "conclusion")]
pub enum JsonReportConclusion {
#[serde(rename = "success")]
Success(JsonReportSuccess),
#[serde(rename = "fail (violation)")]
FailForViolationConflict(JsonReportFailForViolationConflict),
#[serde(rename = "fail (vetting)")]
FailForVet(JsonReportFailForVet),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonReportSuccess {
pub vetted_fully: Vec<JsonPackage>,
pub vetted_partially: Vec<JsonPackage>,
pub vetted_with_exemptions: Vec<JsonPackage>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonReportFailForViolationConflict {
pub violations: SortedMap<PackageAndVersion, Vec<ViolationConflict>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonReportFailForVet {
pub failures: Vec<JsonVetFailure>,
pub suggest: Option<JsonSuggest>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonSuggest {
pub suggestions: Vec<JsonSuggestItem>,
pub suggest_by_criteria: SortedMap<CriteriaName, Vec<JsonSuggestItem>>,
pub total_lines: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonVetFailure {
pub name: PackageName,
pub version: Version,
pub missing_criteria: Vec<CriteriaName>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonSuggestItem {
pub name: PackageName,
pub notable_parents: String,
pub suggested_criteria: Vec<CriteriaName>,
pub suggested_diff: DiffRecommendation,
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ fn cmd_suggest(
OutputFormat::Human => report
.print_suggest_human(out, cfg, suggest.as_ref())
.into_diagnostic()?,
OutputFormat::Json => report.print_json(out, cfg, suggest.as_ref())?,
OutputFormat::Json => report.print_json(out, suggest.as_ref())?,
}

Ok(())
Expand Down Expand Up @@ -1514,7 +1514,7 @@ fn cmd_check(out: &Arc<dyn Out>, cfg: &Config, sub_args: &CheckArgs) -> Result<(
OutputFormat::Human => report
.print_human(out, cfg, suggest.as_ref())
.into_diagnostic()?,
OutputFormat::Json => report.print_json(out, cfg, suggest.as_ref())?,
OutputFormat::Json => report.print_json(out, suggest.as_ref())?,
}

// Only save imports if we succeeded, to avoid any modifications on error.
Expand Down
154 changes: 97 additions & 57 deletions src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ use cargo_metadata::{DependencyKind, Metadata, Node, PackageId, Version};
use core::fmt;
use futures_util::future::join_all;
use miette::IntoDiagnostic;
use serde::Serialize;
use serde_json::json;
use serde::{Deserialize, Serialize};
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::mem;
Expand All @@ -76,7 +75,9 @@ use tracing::{error, trace, trace_span};
use crate::errors::{RegenerateExemptionsError, SuggestError};
use crate::format::{
self, AuditKind, AuditsFile, CriteriaName, CriteriaStr, Delta, DependencyCriteria, DiffStat,
ExemptedDependency, ImportName, PackageName, PackageStr, PolicyEntry, RemoteImport,
ExemptedDependency, ImportName, JsonPackage, JsonReport, JsonReportConclusion,
JsonReportFailForVet, JsonReportFailForViolationConflict, JsonReportSuccess, JsonSuggest,
JsonSuggestItem, JsonVetFailure, PackageName, PackageStr, PolicyEntry, RemoteImport,
SAFE_TO_DEPLOY, SAFE_TO_RUN,
};
use crate::format::{FastMap, FastSet, SortedMap, SortedSet};
Expand Down Expand Up @@ -137,7 +138,7 @@ pub struct FailForVet {
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ViolationConflict {
UnauditedConflict {
violation_source: CriteriaNamespace,
Expand Down Expand Up @@ -167,7 +168,7 @@ pub struct SuggestItem {
pub notable_parents: String,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffRecommendation {
pub from: Option<Version>,
pub to: Version,
Expand All @@ -192,7 +193,7 @@ pub struct CriteriaFailureSet {
pub all: CriteriaSet,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum CriteriaNamespace {
Local,
Foreign(ImportName),
Expand Down Expand Up @@ -2813,61 +2814,100 @@ impl<'a> ResolveReport<'a> {
pub fn print_json(
&self,
out: &Arc<dyn Out>,
_cfg: &Config,
suggest: Option<&Suggest>,
) -> Result<(), miette::Report> {
let result = match &self.conclusion {
Conclusion::Success(success) => {
let json_package = |pkgidx: &PackageIdx| {
let package = &self.graph.nodes[*pkgidx];
json!({
"name": package.name,
"version": package.version,
let result = JsonReport {
conclusion: match &self.conclusion {
Conclusion::Success(success) => {
let json_package = |pkgidx: &PackageIdx| {
let package = &self.graph.nodes[*pkgidx];
JsonPackage {
name: package.name.to_owned(),
version: package.version.clone(),
}
};
JsonReportConclusion::Success(JsonReportSuccess {
vetted_fully: success.vetted_fully.iter().map(json_package).collect(),
vetted_partially: success
.vetted_partially
.iter()
.map(json_package)
.collect(),
vetted_with_exemptions: success
.vetted_with_exemptions
.iter()
.map(json_package)
.collect(),
})
};
json!({
"conclusion": "success",
"vetted_fully": success.vetted_fully.iter().map(json_package).collect::<Vec<_>>(),
"vetted_partially": success.vetted_partially.iter().map(json_package).collect::<Vec<_>>(),
"vetted_with_exemptions": success.vetted_with_exemptions.iter().map(json_package).collect::<Vec<_>>(),
})
}
Conclusion::FailForViolationConflict(fail) => json!({
"conclusion": "fail (violation)",
"violations": fail.violations.iter().map(|(pkgidx, violations)| {
let package = &self.graph.nodes[*pkgidx];
let key = format!("{}:{}", package.name, package.version);
(key, violations)
}).collect::<SortedMap<_,_>>(),
}),
Conclusion::FailForVet(fail) => {
// FIXME: How to report confidence for suggested criteria?
let json_suggest_item = |item: &SuggestItem| {
let package = &self.graph.nodes[item.package];
json!({
"name": package.name,
"notable_parents": item.notable_parents,
"suggested_criteria": self.criteria_mapper.all_criteria_names(&item.suggested_criteria).collect::<Vec<_>>(),
"suggested_diff": item.suggested_diff,
}
Conclusion::FailForViolationConflict(fail) => {
JsonReportConclusion::FailForViolationConflict(
JsonReportFailForViolationConflict {
violations: fail
.violations
.iter()
.map(|(pkgidx, violations)| {
let package = &self.graph.nodes[*pkgidx];
let key = format!("{}:{}", package.name, package.version);
(key, violations.clone())
})
.collect(),
},
)
}
Conclusion::FailForVet(fail) => {
// FIXME: How to report confidence for suggested criteria?
let json_suggest_item = |item: &SuggestItem| {
let package = &self.graph.nodes[item.package];
JsonSuggestItem {
name: package.name.to_owned(),
notable_parents: item.notable_parents.to_owned(),
suggested_criteria: self
.criteria_mapper
.all_criteria_names(&item.suggested_criteria)
.map(|s| s.to_owned())
.collect(),
suggested_diff: item.suggested_diff.clone(),
}
};
JsonReportConclusion::FailForVet(JsonReportFailForVet {
failures: fail
.failures
.iter()
.map(|(&pkgidx, audit_fail)| {
let package = &self.graph.nodes[pkgidx];
JsonVetFailure {
name: package.name.to_owned(),
version: package.version.clone(),
missing_criteria: self
.criteria_mapper
.all_criteria_names(&audit_fail.criteria_failures)
.map(|s| s.to_owned())
.collect(),
}
})
.collect(),
suggest: suggest.as_ref().map(|suggest| JsonSuggest {
suggestions: suggest
.suggestions
.iter()
.map(json_suggest_item)
.collect(),
suggest_by_criteria: suggest
.suggestions_by_criteria
.iter()
.map(|(criteria, items)| {
(
criteria.to_owned(),
items.iter().map(json_suggest_item).collect::<Vec<_>>(),
)
})
.collect(),
total_lines: suggest.total_lines,
}),
})
};
json!({
"conclusion": "fail (vetting)",
"failures": fail.failures.iter().map(|(&pkgidx, audit_fail)| {
let package = &self.graph.nodes[pkgidx];
json!({
"name": package.name,
"version": package.version,
"missing_criteria": self.criteria_mapper.all_criteria_names(&audit_fail.criteria_failures).collect::<Vec<_>>(),
})
}).collect::<Vec<_>>(),
"suggest": suggest.map(|suggest| json!({
"suggestions": suggest.suggestions.iter().map(json_suggest_item).collect::<Vec<_>>(),
"suggest_by_criteria": suggest.suggestions_by_criteria.iter().map(|(criteria, items)| (criteria, items.iter().map(json_suggest_item).collect::<Vec<_>>())).collect::<SortedMap<_,_>>(),
"total_lines": suggest.total_lines,
})),
})
}
}
},
};

serde_json::to_writer_pretty(&**out, &result).into_diagnostic()?;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ fn get_reports(metadata: &Metadata, report: ResolveReport) -> (String, String) {
.unwrap();
let json_output = BasicTestOutput::new();
report
.print_json(&json_output.clone().as_dyn(), &cfg, suggest.as_ref())
.print_json(&json_output.clone().as_dyn(), suggest.as_ref())
.unwrap();
(human_output.to_string(), json_output.to_string())
}
Expand Down
Loading

0 comments on commit 09e14c2

Please sign in to comment.