From 62d3d781b9821a5086a096ef6e33e7c0e8f5e010 Mon Sep 17 00:00:00 2001 From: joshmc Date: Sun, 29 Nov 2020 18:22:51 +0000 Subject: [PATCH] ISSUE-16 - Remove no longer needed mapping functions: * Moving functions which are still required to test parity to code only compiled in tests * Adding a fix for edge case stumbled upon by mistake, where dependencies can be duplicated * Removing explicit use of `cargo_metadata::PackageId` outside of `mapping` module Signed-off-by: joshmc --- cargo-geiger/src/format/display.rs | 3 +- cargo-geiger/src/format/pattern.rs | 5 +- .../src/format/table/handle_text_tree_line.rs | 6 +- cargo-geiger/src/graph.rs | 19 +-- cargo-geiger/src/mapping.rs | 7 +- cargo-geiger/src/mapping/core.rs | 160 ------------------ cargo-geiger/src/mapping/geiger.rs | 4 +- cargo-geiger/src/mapping/metadata.rs | 65 ++++--- cargo-geiger/src/scan.rs | 8 +- cargo-geiger/src/scan/default.rs | 5 +- cargo-geiger/src/scan/default/table.rs | 5 +- cargo-geiger/src/scan/find.rs | 14 +- cargo-geiger/src/scan/forbid.rs | 7 +- cargo-geiger/src/scan/forbid/table.rs | 9 +- cargo-geiger/src/tree.rs | 7 +- cargo-geiger/src/tree/traversal.rs | 5 +- .../src/tree/traversal/dependency_kind.rs | 8 +- .../src/tree/traversal/dependency_node.rs | 33 ++-- 18 files changed, 110 insertions(+), 260 deletions(-) delete mode 100644 cargo-geiger/src/mapping/core.rs diff --git a/cargo-geiger/src/format/display.rs b/cargo-geiger/src/format/display.rs index 127b9855..0ced53d6 100644 --- a/cargo-geiger/src/format/display.rs +++ b/cargo-geiger/src/format/display.rs @@ -7,12 +7,13 @@ use crate::mapping::{ GetRepositoryFromCargoMetadataPackageId, }; +use cargo_metadata::PackageId; use std::fmt; pub struct Display<'a> { pub cargo_metadata_parameters: &'a CargoMetadataParameters<'a>, pub pattern: &'a Pattern, - pub package: &'a cargo_metadata::PackageId, + pub package: &'a PackageId, } impl<'a> fmt::Display for Display<'a> { diff --git a/cargo-geiger/src/format/pattern.rs b/cargo-geiger/src/format/pattern.rs index fbddd682..bbd01dba 100644 --- a/cargo-geiger/src/format/pattern.rs +++ b/cargo-geiger/src/format/pattern.rs @@ -1,9 +1,10 @@ use crate::format::parse::Parser; use crate::format::{Chunk, RawChunk}; +use crate::mapping::CargoMetadataParameters; use super::display::Display; -use crate::mapping::CargoMetadataParameters; +use cargo_metadata::PackageId; use std::error::Error; #[derive(Debug, PartialEq)] @@ -13,7 +14,7 @@ impl Pattern { pub fn display<'a>( &'a self, cargo_metadata_parameters: &'a CargoMetadataParameters, - package: &'a cargo_metadata::PackageId, + package: &'a PackageId, ) -> Display<'a> { Display { cargo_metadata_parameters, diff --git a/cargo-geiger/src/format/table/handle_text_tree_line.rs b/cargo-geiger/src/format/table/handle_text_tree_line.rs index 74ae0470..232256dc 100644 --- a/cargo-geiger/src/format/table/handle_text_tree_line.rs +++ b/cargo-geiger/src/format/table/handle_text_tree_line.rs @@ -8,12 +8,12 @@ use super::total_package_counts::TotalPackageCounts; use super::TableParameters; use super::{table_row, table_row_empty}; -use cargo_metadata::DependencyKind; +use cargo_metadata::{DependencyKind, PackageId}; use std::collections::HashSet; pub struct HandlePackageParameters<'a> { pub total_package_counts: &'a mut TotalPackageCounts, - pub visited_package_ids: &'a mut HashSet, + pub visited_package_ids: &'a mut HashSet, pub warning_count: &'a mut u64, } @@ -36,7 +36,7 @@ pub fn handle_text_tree_line_package( cargo_metadata_parameters: &CargoMetadataParameters, emoji_symbols: &EmojiSymbols, handle_package_parameters: &mut HandlePackageParameters, - package_id: cargo_metadata::PackageId, + package_id: PackageId, table_lines: &mut Vec, table_parameters: &TableParameters, tree_vines: String, diff --git a/cargo-geiger/src/graph.rs b/cargo-geiger/src/graph.rs index 2a84c3dd..c1156e6a 100644 --- a/cargo-geiger/src/graph.rs +++ b/cargo-geiger/src/graph.rs @@ -8,7 +8,7 @@ use cargo::core::Workspace; use cargo::util::interning::InternedString; use cargo::util::CargoResult; use cargo::Config; -use cargo_metadata::DependencyKind; +use cargo_metadata::{DependencyKind, PackageId}; use cargo_platform::{Cfg, Platform}; use petgraph::graph::NodeIndex; use std::collections::hash_map::Entry; @@ -39,11 +39,8 @@ impl ExtraDeps { /// Representation of the package dependency graph pub struct Graph { - pub graph: petgraph::Graph< - cargo_metadata::PackageId, - cargo_metadata::DependencyKind, - >, - pub nodes: HashMap, + pub graph: petgraph::Graph, + pub nodes: HashMap, } // Almost unmodified compared to the original in cargo-tree, should be fairly @@ -53,7 +50,7 @@ pub fn build_graph<'a>( args: &Args, cargo_metadata_parameters: &'a CargoMetadataParameters, config: &Config, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, workspace: &Workspace, ) -> CargoResult { let config_host = config.load_global_rustc(Some(&workspace))?.host; @@ -102,10 +99,10 @@ struct GraphConfiguration<'a> { fn add_graph_node_if_not_present_and_edge( dependency: &cargo_metadata::Dependency, - dependency_package_id: cargo_metadata::PackageId, + dependency_package_id: PackageId, graph: &mut Graph, index: NodeIndex, - pending_packages: &mut Vec, + pending_packages: &mut Vec, ) { let dependency_index = match graph.nodes.entry(dependency_package_id.clone()) { @@ -122,10 +119,10 @@ fn add_graph_node_if_not_present_and_edge( fn add_package_dependencies_to_graph( cargo_metadata_parameters: &CargoMetadataParameters, - package_id: cargo_metadata::PackageId, + package_id: PackageId, graph_configuration: &GraphConfiguration, graph: &mut Graph, - pending_packages: &mut Vec, + pending_packages: &mut Vec, ) { let index = graph.nodes[&package_id]; let package = cargo_metadata_parameters diff --git a/cargo-geiger/src/mapping.rs b/cargo-geiger/src/mapping.rs index 03804d88..db552f2b 100644 --- a/cargo-geiger/src/mapping.rs +++ b/cargo-geiger/src/mapping.rs @@ -1,11 +1,10 @@ -mod core; mod geiger; mod krates; mod metadata; use ::krates::Krates; use cargo::core::dependency::DepKind; -use cargo_metadata::{DependencyKind, Metadata}; +use cargo_metadata::Metadata; use std::collections::HashSet; use std::path::PathBuf; @@ -92,10 +91,6 @@ pub trait ToCargoGeigerSource { ) -> cargo_geiger_serde::Source; } -pub trait ToCargoMetadataDependencyKind { - fn to_cargo_metadata_dependency_kind(&self) -> DependencyKind; -} - pub trait ToCargoMetadataPackage { fn to_cargo_metadata_package( &self, diff --git a/cargo-geiger/src/mapping/core.rs b/cargo-geiger/src/mapping/core.rs deleted file mode 100644 index f39a9087..00000000 --- a/cargo-geiger/src/mapping/core.rs +++ /dev/null @@ -1,160 +0,0 @@ -use super::ToCargoMetadataDependencyKind; - -use crate::mapping::{ToCargoMetadataPackage, ToCargoMetadataPackageId}; - -use cargo::core::dependency::DepKind; -use cargo::core::{Package, PackageId}; -use cargo_metadata::{DependencyKind, Metadata}; - -impl ToCargoMetadataDependencyKind for DepKind { - fn to_cargo_metadata_dependency_kind(&self) -> DependencyKind { - match self { - DepKind::Build => DependencyKind::Build, - DepKind::Development => DependencyKind::Development, - DepKind::Normal => DependencyKind::Normal, - } - } -} - -impl ToCargoMetadataPackage for Package { - fn to_cargo_metadata_package( - &self, - metadata: &Metadata, - ) -> Option { - metadata - .packages - .iter() - .filter(|p| { - p.name == self.name().to_string() - && p.version.major == self.version().major - && p.version.minor == self.version().minor - && p.version.patch == self.version().patch - }) - .cloned() - .collect::>() - .pop() - } -} - -impl ToCargoMetadataPackageId for PackageId { - fn to_cargo_metadata_package_id( - &self, - metadata: &Metadata, - ) -> Option { - metadata - .packages - .iter() - .filter(|p| { - p.name == self.name().to_string() - && p.version.major == self.version().major - && p.version.minor == self.version().minor - && p.version.patch == self.version().patch - }) - .map(|p| p.id.clone()) - .collect::>() - .pop() - } -} - -#[cfg(test)] -mod core_tests { - use super::*; - - use crate::cli::get_workspace; - - use cargo::core::registry::PackageRegistry; - use cargo::core::Workspace; - use cargo::{CargoResult, Config}; - use cargo_metadata::{CargoOpt, Metadata, MetadataCommand}; - use krates::Builder as KratesBuilder; - use krates::Krates; - use rstest::*; - use std::path::PathBuf; - - #[rstest( - input_dep_kind, - expected_dependency_kind, - case(DepKind::Build, DependencyKind::Build), - case(DepKind::Development, DependencyKind::Development), - case(DepKind::Normal, DependencyKind::Normal) - )] - fn to_cargo_metadata_dependency_kind_test( - input_dep_kind: DepKind, - expected_dependency_kind: DependencyKind, - ) { - assert_eq!( - input_dep_kind.to_cargo_metadata_dependency_kind(), - expected_dependency_kind - ); - } - - #[rstest] - fn to_cargo_metadata_package_test() { - let config = Config::default().unwrap(); - let (package, _, _) = - construct_package_registry_workspace_tuple(&config); - - let (_, metadata) = construct_krates_and_metadata(); - - let cargo_metadata_package = - package.to_cargo_metadata_package(&metadata).unwrap(); - - assert_eq!(cargo_metadata_package.name, package.name().to_string()); - assert!( - cargo_metadata_package.version.major == package.version().major - && cargo_metadata_package.version.minor - == package.version().minor - && cargo_metadata_package.version.patch - == package.version().patch - ); - } - - #[rstest] - fn to_cargo_metadata_package_id_test() { - let config = Config::default().unwrap(); - let (package, _, _) = - construct_package_registry_workspace_tuple(&config); - - let (_, metadata) = construct_krates_and_metadata(); - let cargo_metadata_package_id = package - .package_id() - .to_cargo_metadata_package_id(&metadata) - .unwrap(); - - assert!(cargo_metadata_package_id.repr.contains("cargo-geiger")); - } - - fn construct_krates_and_metadata() -> (Krates, Metadata) { - let metadata = MetadataCommand::new() - .manifest_path("./Cargo.toml") - .features(CargoOpt::AllFeatures) - .exec() - .unwrap(); - - let krates = KratesBuilder::new() - .build_with_metadata(metadata.clone(), |_| ()) - .unwrap(); - - (krates, metadata) - } - - fn construct_package_registry_workspace_tuple( - config: &Config, - ) -> (Package, PackageRegistry, Workspace) { - let manifest_path: Option = None; - let workspace = get_workspace(config, manifest_path).unwrap(); - let package = workspace.current().unwrap().clone(); - let registry = get_registry(&config, &package).unwrap(); - - (package, registry, workspace) - } - - fn get_registry<'a>( - config: &'a Config, - package: &Package, - ) -> CargoResult> { - let mut registry = PackageRegistry::new(config)?; - registry.add_sources(Some(package.package_id().source_id()))?; - Ok(registry) - } -} diff --git a/cargo-geiger/src/mapping/geiger.rs b/cargo-geiger/src/mapping/geiger.rs index f6d60291..85b760d3 100644 --- a/cargo-geiger/src/mapping/geiger.rs +++ b/cargo-geiger/src/mapping/geiger.rs @@ -1,6 +1,4 @@ -use super::ToCargoGeigerSource; - -use crate::mapping::ToCargoMetadataPackage; +use super::{ToCargoGeigerSource, ToCargoMetadataPackage}; use cargo_metadata::Metadata; use url::Url; diff --git a/cargo-geiger/src/mapping/metadata.rs b/cargo-geiger/src/mapping/metadata.rs index 1f7cb5cb..6ef242de 100644 --- a/cargo-geiger/src/mapping/metadata.rs +++ b/cargo-geiger/src/mapping/metadata.rs @@ -1,16 +1,14 @@ use super::{ DepsNotReplaced, GetPackageNameFromCargoMetadataPackageId, GetPackageVersionFromCargoMetadataPackageId, GetRoot, - MatchesIgnoringSource, ToCargoCoreDepKind, ToCargoGeigerPackageId, - ToCargoMetadataPackageId, + MatchesIgnoringSource, ToCargoGeigerPackageId, ToCargoMetadataPackageId, }; use crate::mapping::{ ToCargoGeigerDependencyKind, ToCargoGeigerSource, ToCargoMetadataPackage, }; -use cargo::core::dependency::DepKind; -use cargo_metadata::{DependencyKind, Metadata}; +use cargo_metadata::{DependencyKind, Metadata, PackageId}; use krates::Krates; use std::collections::HashSet; use std::path::PathBuf; @@ -24,6 +22,7 @@ impl DepsNotReplaced for cargo_metadata::Metadata { HashSet, )> { let mut cargo_metadata_deps_not_replaced = vec![]; + let mut package_id_hashset = HashSet::::new(); for dep in package_id .to_cargo_metadata_package(self) @@ -31,10 +30,13 @@ impl DepsNotReplaced for cargo_metadata::Metadata { .dependencies { if let Some(package_id) = dep.to_cargo_metadata_package_id(self) { - cargo_metadata_deps_not_replaced.push(( - package_id, - HashSet::::new(), - )) + if !package_id_hashset.contains(&package_id) { + cargo_metadata_deps_not_replaced.push(( + package_id.clone(), + HashSet::::new(), + )); + package_id_hashset.insert(package_id); + } } } @@ -68,17 +70,6 @@ impl MatchesIgnoringSource for cargo_metadata::Dependency { } } -impl ToCargoCoreDepKind for DependencyKind { - fn to_cargo_core_dep_kind(&self) -> DepKind { - match self { - DependencyKind::Build => DepKind::Build, - DependencyKind::Development => DepKind::Development, - DependencyKind::Normal => DepKind::Normal, - _ => panic!("Unknown dependency kind"), - } - } -} - impl ToCargoGeigerDependencyKind for cargo_metadata::DependencyKind { fn to_cargo_geiger_dependency_kind( &self, @@ -146,11 +137,14 @@ impl ToCargoMetadataPackage for cargo_metadata::PackageId { mod metadata_tests { use super::*; - use super::super::GetPackageNameFromCargoMetadataPackageId; + use super::super::{ + GetPackageNameFromCargoMetadataPackageId, ToCargoCoreDepKind, + }; use crate::args::FeaturesArgs; use crate::cli::get_workspace; + use cargo::core::dependency::DepKind; use cargo::core::registry::PackageRegistry; use cargo::core::resolver::ResolveOpts; use cargo::core::{ @@ -348,4 +342,35 @@ mod metadata_tests { )?; Ok((packages, resolve)) } + + impl ToCargoCoreDepKind for DependencyKind { + fn to_cargo_core_dep_kind(&self) -> DepKind { + match self { + DependencyKind::Build => DepKind::Build, + DependencyKind::Development => DepKind::Development, + DependencyKind::Normal => DepKind::Normal, + _ => panic!("Unknown dependency kind"), + } + } + } + + impl ToCargoMetadataPackageId for PackageId { + fn to_cargo_metadata_package_id( + &self, + metadata: &Metadata, + ) -> Option { + metadata + .packages + .iter() + .filter(|p| { + p.name == self.name().to_string() + && p.version.major == self.version().major + && p.version.minor == self.version().minor + && p.version.patch == self.version().patch + }) + .map(|p| p.id.clone()) + .collect::>() + .pop() + } + } } diff --git a/cargo-geiger/src/scan.rs b/cargo-geiger/src/scan.rs index c90a1e6c..6e498516 100644 --- a/cargo-geiger/src/scan.rs +++ b/cargo-geiger/src/scan.rs @@ -19,6 +19,7 @@ use forbid::scan_forbid_unsafe; use cargo::core::Workspace; use cargo::{CliResult, Config}; use cargo_geiger_serde::{CounterBlock, PackageInfo, UnsafeInfo}; +use cargo_metadata::PackageId; use petgraph::visit::EdgeRef; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; @@ -26,8 +27,7 @@ use std::path::PathBuf; /// Provides a more terse and searchable name for the wrapped generic /// collection. pub struct GeigerContext { - pub package_id_to_metrics: - HashMap, + pub package_id_to_metrics: HashMap, } #[derive(Clone, Debug, Default)] @@ -57,7 +57,7 @@ pub fn scan( cargo_metadata_parameters: &CargoMetadataParameters, config: &Config, graph: &Graph, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, workspace: &Workspace, ) -> CliResult { let print_config = PrintConfig::new(args)?; @@ -163,7 +163,7 @@ fn package_metrics( cargo_metadata_parameters: &CargoMetadataParameters, geiger_context: &GeigerContext, graph: &Graph, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, ) -> Vec<(PackageInfo, Option)> { let mut package_metrics = Vec::<(PackageInfo, Option)>::new(); diff --git a/cargo-geiger/src/scan/default.rs b/cargo-geiger/src/scan/default.rs index a714ce22..959c9f03 100644 --- a/cargo-geiger/src/scan/default.rs +++ b/cargo-geiger/src/scan/default.rs @@ -19,11 +19,12 @@ use cargo::core::Workspace; use cargo::ops::CompileOptions; use cargo::{CliError, CliResult, Config}; use cargo_geiger_serde::{ReportEntry, SafetyReport}; +use cargo_metadata::PackageId; pub fn scan_unsafe( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, scan_parameters: &ScanParameters, workspace: &Workspace, ) -> CliResult { @@ -111,7 +112,7 @@ fn scan_to_report( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, output_format: OutputFormat, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, scan_parameters: &ScanParameters, workspace: &Workspace, ) -> CliResult { diff --git a/cargo-geiger/src/scan/default/table.rs b/cargo-geiger/src/scan/default/table.rs index 7c73860e..664151d2 100644 --- a/cargo-geiger/src/scan/default/table.rs +++ b/cargo-geiger/src/scan/default/table.rs @@ -4,6 +4,7 @@ use crate::format::table::{ }; use crate::format::SymbolKind; use crate::graph::Graph; +use crate::mapping::CargoMetadataParameters; use crate::tree::traversal::walk_dependency_tree; use super::super::{ @@ -12,10 +13,10 @@ use super::super::{ }; use super::scan; -use crate::mapping::CargoMetadataParameters; use cargo::core::shell::Verbosity; use cargo::core::Workspace; use cargo::{CliError, CliResult}; +use cargo_metadata::PackageId; use colored::Colorize; use std::error::Error; use std::fmt; @@ -23,7 +24,7 @@ use std::fmt; pub fn scan_to_table( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, scan_parameters: &ScanParameters, workspace: &Workspace, ) -> CliResult { diff --git a/cargo-geiger/src/scan/find.rs b/cargo-geiger/src/scan/find.rs index 8e218f2f..bad189c2 100644 --- a/cargo-geiger/src/scan/find.rs +++ b/cargo-geiger/src/scan/find.rs @@ -10,6 +10,7 @@ use super::{GeigerContext, ScanMode}; use cargo::util::CargoResult; use cargo::{CliError, Config}; +use cargo_metadata::PackageId; use geiger::{find_unsafe_in_file, IncludeTests, RsFileMetrics, ScanFileError}; use std::collections::HashMap; use std::path::Path; @@ -84,7 +85,7 @@ where .map(|(cargo_metadata_package_id, package_metrics)| { (cargo_metadata_package_id.clone(), package_metrics.clone()) }) - .collect::>(); + .collect::>(); GeigerContext { package_id_to_metrics: cargo_core_package_metrics, @@ -145,7 +146,7 @@ fn find_rs_files_in_package(package: &cargo_metadata::Package) -> Vec { fn find_rs_files_in_packages( packages: &[cargo_metadata::Package], -) -> impl Iterator + '_ { +) -> impl Iterator + '_ { packages.iter().flat_map(|package| { find_rs_files_in_package(package) .into_iter() @@ -167,11 +168,8 @@ fn handle_unsafe_in_file_error( fn update_package_id_to_metrics_with_rs_file_metrics( is_entry_point: bool, - package_id: cargo_metadata::PackageId, - package_id_to_metrics: &mut HashMap< - cargo_metadata::PackageId, - PackageMetrics, - >, + package_id: PackageId, + package_id_to_metrics: &mut HashMap, path_buf: PathBuf, rs_file_metrics: RsFileMetrics, ) { @@ -294,7 +292,7 @@ mod find_tests { package: cargo_metadata::Package, ) { let mut package_id_to_metrics = - HashMap::::new(); + HashMap::::new(); let mut rs_files_in_package = find_rs_files_in_package(&package); let rs_file = rs_files_in_package.pop().unwrap(); diff --git a/cargo-geiger/src/scan/forbid.rs b/cargo-geiger/src/scan/forbid.rs index 999c4408..ffd90148 100644 --- a/cargo-geiger/src/scan/forbid.rs +++ b/cargo-geiger/src/scan/forbid.rs @@ -2,20 +2,21 @@ mod table; use crate::format::print_config::{OutputFormat, PrintConfig}; use crate::graph::Graph; +use crate::mapping::CargoMetadataParameters; use super::find::find_unsafe; use super::{package_metrics, ScanMode, ScanParameters}; use table::scan_forbid_to_table; -use crate::mapping::CargoMetadataParameters; use cargo::{CliResult, Config}; use cargo_geiger_serde::{QuickReportEntry, QuickSafetyReport}; +use cargo_metadata::PackageId; pub fn scan_forbid_unsafe( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, scan_parameters: &ScanParameters, ) -> CliResult { match scan_parameters.args.output_format { @@ -43,7 +44,7 @@ fn scan_forbid_to_report( graph: &Graph, output_format: OutputFormat, print_config: &PrintConfig, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, ) -> CliResult { let geiger_context = find_unsafe( cargo_metadata_parameters, diff --git a/cargo-geiger/src/scan/forbid/table.rs b/cargo-geiger/src/scan/forbid/table.rs index e0a76030..f0cddb2f 100644 --- a/cargo-geiger/src/scan/forbid/table.rs +++ b/cargo-geiger/src/scan/forbid/table.rs @@ -4,14 +4,15 @@ use crate::format::print_config::PrintConfig; use crate::format::{get_kind_group_name, SymbolKind}; use crate::graph::Graph; use crate::mapping::CargoMetadataParameters; +use crate::scan::GeigerContext; use crate::tree::traversal::walk_dependency_tree; use crate::tree::TextTreeLine; use super::super::find::find_unsafe; use super::super::ScanMode; -use crate::scan::GeigerContext; use cargo::{CliResult, Config}; +use cargo_metadata::PackageId; use colored::Colorize; pub fn scan_forbid_to_table( @@ -19,7 +20,7 @@ pub fn scan_forbid_to_table( config: &Config, graph: &Graph, print_config: &PrintConfig, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, ) -> CliResult { let mut scan_output_lines = Vec::::new(); let emoji_symbols = EmojiSymbols::new(print_config.charset); @@ -104,7 +105,7 @@ fn construct_key_lines(emoji_symbols: &EmojiSymbols) -> Vec { fn format_package_name( cargo_metadata_parameters: &CargoMetadataParameters, - package_id: &cargo_metadata::PackageId, + package_id: &PackageId, pattern: &Pattern, ) -> String { format!( @@ -117,7 +118,7 @@ fn handle_package_text_tree_line( cargo_metadata_parameters: &CargoMetadataParameters, emoji_symbols: &EmojiSymbols, geiger_ctx: &GeigerContext, - package_id: cargo_metadata::PackageId, + package_id: PackageId, print_config: &PrintConfig, scan_output_lines: &mut Vec, tree_vines: String, diff --git a/cargo-geiger/src/tree.rs b/cargo-geiger/src/tree.rs index 9cb14c2c..2dae271f 100644 --- a/cargo-geiger/src/tree.rs +++ b/cargo-geiger/src/tree.rs @@ -3,17 +3,14 @@ pub mod traversal; use crate::format::print_config::{Prefix, PrintConfig}; use crate::format::Charset; -use cargo_metadata::DependencyKind; +use cargo_metadata::{DependencyKind, PackageId}; /// A step towards decoupling some parts of the table-tree printing from the /// dependency graph traversal. #[derive(Debug, PartialEq)] pub enum TextTreeLine { /// A text line for a package - Package { - id: cargo_metadata::PackageId, - tree_vines: String, - }, + Package { id: PackageId, tree_vines: String }, /// There are extra dependencies coming and we should print a group header, /// eg. "[build-dependencies]". ExtraDepsGroup { diff --git a/cargo-geiger/src/tree/traversal.rs b/cargo-geiger/src/tree/traversal.rs index 0565fe51..90b08d3d 100644 --- a/cargo-geiger/src/tree/traversal.rs +++ b/cargo-geiger/src/tree/traversal.rs @@ -10,13 +10,14 @@ use super::construct_tree_vines_string; use dependency_kind::walk_dependency_kind; use dependency_node::walk_dependency_node; +use cargo_metadata::PackageId; use std::collections::HashSet; pub struct WalkDependencyParameters<'a> { pub graph: &'a Graph, pub levels_continue: &'a mut Vec, pub print_config: &'a PrintConfig, - pub visited_deps: &'a mut HashSet, + pub visited_deps: &'a mut HashSet, } /// Printing the returned TextTreeLines in order is expected to produce a nice @@ -29,7 +30,7 @@ pub fn walk_dependency_tree( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, print_config: &PrintConfig, - root_package_id: cargo_metadata::PackageId, + root_package_id: PackageId, ) -> Vec { let mut visited_deps = HashSet::new(); let mut levels_continue = vec![]; diff --git a/cargo-geiger/src/tree/traversal/dependency_kind.rs b/cargo-geiger/src/tree/traversal/dependency_kind.rs index 7991a6bd..ee1a2b18 100644 --- a/cargo-geiger/src/tree/traversal/dependency_kind.rs +++ b/cargo-geiger/src/tree/traversal/dependency_kind.rs @@ -5,14 +5,14 @@ use crate::tree::{get_tree_symbols, TextTreeLine, TreeSymbols}; use super::dependency_node::walk_dependency_node; -use cargo_metadata::DependencyKind; +use cargo_metadata::{DependencyKind, PackageId}; use std::iter::Peekable; use std::slice::Iter; pub fn walk_dependency_kind( cargo_metadata_parameters: &CargoMetadataParameters, dep_kind: DependencyKind, - deps: &mut Vec, + deps: &mut Vec, walk_dependency_parameters: &mut WalkDependencyParameters, ) -> Vec { if deps.is_empty() { @@ -49,8 +49,8 @@ pub fn walk_dependency_kind( fn handle_walk_dependency_node( cargo_metadata_parameters: &CargoMetadataParameters, - dependency: &cargo_metadata::PackageId, - node_iterator: &mut Peekable>, + dependency: &PackageId, + node_iterator: &mut Peekable>, text_tree_lines: &mut Vec, walk_dependency_parameters: &mut WalkDependencyParameters, ) { diff --git a/cargo-geiger/src/tree/traversal/dependency_node.rs b/cargo-geiger/src/tree/traversal/dependency_node.rs index 4551de71..67fb9f01 100644 --- a/cargo-geiger/src/tree/traversal/dependency_node.rs +++ b/cargo-geiger/src/tree/traversal/dependency_node.rs @@ -7,14 +7,14 @@ use crate::tree::TextTreeLine; use super::construct_tree_vines_string; use super::walk_dependency_kind; -use cargo_metadata::DependencyKind; +use cargo_metadata::{DependencyKind, PackageId}; use petgraph::visit::EdgeRef; use petgraph::EdgeDirection; use std::collections::HashMap; pub fn walk_dependency_node( cargo_metadata_parameters: &CargoMetadataParameters, - package: &cargo_metadata::PackageId, + package: &PackageId, walk_dependency_parameters: &mut WalkDependencyParameters, ) -> Vec { let new = walk_dependency_parameters.print_config.all @@ -57,13 +57,10 @@ pub fn walk_dependency_node( fn construct_dependency_type_nodes_hashmap<'a>( graph: &'a Graph, - package: &cargo_metadata::PackageId, + package: &PackageId, print_config: &PrintConfig, -) -> HashMap> { - let mut dependency_type_nodes: HashMap< - PrivateDepKind, - Vec, - > = [ +) -> HashMap> { + let mut dependency_type_nodes: HashMap> = [ (PrivateDepKind::Build, vec![]), (PrivateDepKind::Development, vec![]), (PrivateDepKind::Normal, vec![]), @@ -176,11 +173,9 @@ mod dependency_node_tests { expected_development_nodes_length: usize, expected_normal_nodes_length: usize, ) { - let mut inner_graph = petgraph::Graph::< - cargo_metadata::PackageId, - cargo_metadata::DependencyKind, - >::new(); - let mut nodes = HashMap::::new(); + let mut inner_graph = + petgraph::Graph::::new(); + let mut nodes = HashMap::::new(); let package_ids = create_cargo_metadata_package_id_vec(7); let print_config = create_print_config(input_edge_direction); @@ -227,9 +222,9 @@ mod dependency_node_tests { fn add_edges_to_graph( directed_edges: &[(usize, usize, DependencyKind)], - graph: &mut petgraph::Graph, - nodes: &HashMap, - package_ids: &[cargo_metadata::PackageId], + graph: &mut petgraph::Graph, + nodes: &HashMap, + package_ids: &[PackageId], ) { for (source_index, target_index, dep_kind) in directed_edges { graph.add_edge( @@ -240,13 +235,11 @@ mod dependency_node_tests { } } - fn create_cargo_metadata_package_id_vec( - count: i32, - ) -> Vec { + fn create_cargo_metadata_package_id_vec(count: i32) -> Vec { let mut package_id_vec = vec![]; for i in 0..count { - package_id_vec.push(cargo_metadata::PackageId { + package_id_vec.push(PackageId { repr: format!("string_repr_{}", i), }); }