Skip to content

Commit

Permalink
More consistently tidy files before serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
mystor committed Sep 11, 2024
1 parent 0f4db3c commit 9904a29
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
30 changes: 29 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::errors::{StoreVersionParseError, VersionParseError};
use crate::resolver::{DiffRecommendation, ViolationConflict};
use crate::serialization::spanned::Spanned;
use crate::serialization::{spanned::Spanned, Tidyable};
use crate::{flock::Filesystem, serialization};
use core::{cmp, fmt};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
Expand Down Expand Up @@ -235,6 +235,14 @@ pub struct AuditsFile {
pub trusted: TrustedPackages,
}

impl Tidyable for AuditsFile {
fn tidy(&mut self) {
self.audits.tidy();
self.wildcard_audits.tidy();
self.trusted.tidy();
}
}

/// Foreign audits.toml with unparsed entries and audits. Should have the same
/// structure as `AuditsFile`, but with individual audits and criteria unparsed.
#[derive(serde::Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -583,6 +591,12 @@ pub struct ConfigFile {
pub exemptions: SortedMap<PackageName, Vec<ExemptedDependency>>,
}

impl Tidyable for ConfigFile {
fn tidy(&mut self) {
self.exemptions.tidy();
}
}

pub static SAFE_TO_DEPLOY: CriteriaStr = "safe-to-deploy";
pub static SAFE_TO_RUN: CriteriaStr = "safe-to-run";
pub static DEFAULT_CRITERIA: CriteriaStr = SAFE_TO_DEPLOY;
Expand Down Expand Up @@ -1011,6 +1025,16 @@ pub struct ImportsFile {
pub audits: SortedMap<ImportName, AuditsFile>,
}

impl Tidyable for ImportsFile {
fn tidy(&mut self) {
self.unpublished.tidy();
self.publisher.tidy();
for audits_file in self.audits.values_mut() {
audits_file.tidy();
}
}
}

/// Information about who published a specific version of a crate to be cached
/// in imports.lock.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -1073,6 +1097,10 @@ pub enum DiffCache {
},
}

impl Tidyable for DiffCache {
fn tidy(&mut self) {}
}

impl Default for DiffCache {
fn default() -> Self {
DiffCache::V2 {
Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use out::{progress_bar, IncProgressOnDrop};
use reqwest::Url;
use serde::de::Deserialize;
use serialization::spanned::Spanned;
use serialization::Tidyable;
use storage::fetch_registry;
use thiserror::Error;
use tracing::{error, info, trace, warn};
Expand Down Expand Up @@ -2553,6 +2554,9 @@ fn do_aggregate_audits(sources: Vec<(String, AuditsFile)>) -> Result<AuditsFile,
}));
}
}

aggregate.tidy();

if errors.is_empty() {
Ok(aggregate)
} else {
Expand Down
19 changes: 19 additions & 0 deletions src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,25 @@ pub mod audit {
}
}

/// Trait implemented by format data types which may want to be cleaned up
/// before they are serialized.
pub trait Tidyable {
/// Ensure that the data structure is tidy and ready to be serialized.
/// This may remove empty entries from maps, ensure lists are sorted, etc.
fn tidy(&mut self);
}

/// Helper for tidying the common audit data structure, removing empty entries,
/// and sorting audit lists.
impl<K: Ord, E: Ord> Tidyable for SortedMap<K, Vec<E>> {
fn tidy(&mut self) {
self.retain(|_, entries| {
entries.sort();
!entries.is_empty()
});
}
}

/// Inline arrays which have a representation longer than this will be rendered
/// over multiple lines.
const ARRAY_WRAP_THRESHOLD: usize = 80;
Expand Down
21 changes: 7 additions & 14 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
},
network::Network,
out::{progress_bar, IncProgressOnDrop},
serialization::{parse_from_value, spanned::Spanned, to_formatted_toml},
serialization::{parse_from_value, spanned::Spanned, to_formatted_toml, Tidyable},
Config, PackageExt, PartialConfig, CARGO_ENV,
};

Expand Down Expand Up @@ -2992,12 +2992,14 @@ where
}
fn store_toml<T>(
heading: &str,
val: T,
mut val: T,
user_info: Option<&FastMap<CratesUserId, CratesCacheUser>>,
) -> Result<String, StoreTomlError>
where
T: Serialize,
T: Serialize + Tidyable,
{
val.tidy();

let toml_document = to_formatted_toml(val, user_info)?;
Ok(format!("{heading}{toml_document}"))
}
Expand All @@ -3019,25 +3021,16 @@ where
Ok(json_string)
}
fn store_audits(
mut audits: AuditsFile,
audits: AuditsFile,
user_info: &FastMap<CratesUserId, CratesCacheUser>,
) -> Result<String, StoreTomlError> {
let heading = r###"
# cargo-vet audits file
"###;
audits
.audits
.values_mut()
.for_each(|entries| entries.sort());

store_toml(heading, audits, Some(user_info))
}
fn store_config(mut config: ConfigFile) -> Result<String, StoreTomlError> {
config
.exemptions
.values_mut()
.for_each(|entries| entries.sort());

fn store_config(config: ConfigFile) -> Result<String, StoreTomlError> {
let heading = r###"
# cargo-vet config file
"###;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ aggregated-from = "https://source2.example.com/supply_chain/audits.toml"

[[audits.package1]]
criteria = "safe-to-deploy"
version = "10.0.0"
aggregated-from = "https://source1.example.com/supply_chain/audits.toml"
version = "5.0.0"
aggregated-from = "https://source2.example.com/supply_chain/audits.toml"

[[audits.package1]]
criteria = "safe-to-deploy"
version = "5.0.0"
aggregated-from = "https://source2.example.com/supply_chain/audits.toml"
version = "10.0.0"
aggregated-from = "https://source1.example.com/supply_chain/audits.toml"

[[audits.package1]]
criteria = "safe-to-deploy"
Expand Down

0 comments on commit 9904a29

Please sign in to comment.