diff --git a/cargo-geiger/src/cli.rs b/cargo-geiger/src/cli.rs index b1d855fa..25eabe46 100644 --- a/cargo-geiger/src/cli.rs +++ b/cargo-geiger/src/cli.rs @@ -8,16 +8,11 @@ // TODO: Investigate how cargo-clippy is implemented. Is it using syn? Is is // using rustc? Is it implementing a compiler plugin? -use crate::args::FeaturesArgs; use crate::Args; // TODO: Consider making this a lib.rs (again) and expose a full API, excluding // only the terminal output..? That API would be dependent on cargo. -use cargo::core::package::PackageSet; -use cargo::core::registry::PackageRegistry; -use cargo::core::resolver::ResolveOpts; -use cargo::core::{Package, PackageId, PackageIdSpec, Resolve, Workspace}; -use cargo::ops; +use cargo::core::Workspace; use cargo::util::{self, important_paths, CargoResult}; use cargo::Config; use cargo_metadata::{CargoOpt, Metadata, MetadataCommand}; @@ -88,15 +83,6 @@ pub fn get_krates(cargo_metadata: &Metadata) -> CargoResult { .build_with_metadata(cargo_metadata.clone(), |_| ())?) } -pub 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) -} - pub fn get_workspace( config: &Config, manifest_path: Option, @@ -108,37 +94,6 @@ pub fn get_workspace( Workspace::new(&root, config) } -pub fn resolve<'a, 'cfg>( - args: &FeaturesArgs, - package_id: PackageId, - registry: &mut PackageRegistry<'cfg>, - workspace: &'a Workspace<'cfg>, -) -> CargoResult<(PackageSet<'a>, Resolve)> { - let dev_deps = true; // TODO: Review this. - let uses_default_features = !args.no_default_features; - let opts = ResolveOpts::new( - dev_deps, - &args.features.clone(), - args.all_features, - uses_default_features, - ); - let prev = ops::load_pkg_lockfile(workspace)?; - let resolve = ops::resolve_with_previous( - registry, - workspace, - &opts, - prev.as_ref(), - None, - &[PackageIdSpec::from_package_id(package_id)], - true, - )?; - let packages = ops::get_resolved_packages( - &resolve, - PackageRegistry::new(workspace.config())?, - )?; - Ok((packages, resolve)) -} - // TODO: Make a wrapper type for canonical paths and hide all mutable access. #[cfg(test)] @@ -195,29 +150,6 @@ mod cli_tests { assert!(krates_result.is_ok()); } - #[rstest] - fn get_registry_test() { - let config = Config::default().unwrap(); - let workspace = Workspace::new( - &important_paths::find_root_manifest_for_wd(config.cwd()).unwrap(), - &config, - ) - .unwrap(); - let package = workspace.current().unwrap(); - - let registry_result = get_registry(&config, &package); - - assert!(registry_result.is_ok()); - let registry = registry_result.unwrap(); - - let package_ids = vec![package.package_id()]; - let package_set_result = registry.get(&package_ids); - assert!(package_set_result.is_ok()); - let package_set = package_set_result.unwrap(); - - assert_eq!(package_set.sources().len(), 1); - } - #[rstest] fn get_workspace_test() { let config = Config::default().unwrap(); @@ -233,19 +165,4 @@ mod cli_tests { assert_eq!(package.package_id().name(), "cargo-geiger"); } - - #[rstest] - fn resolve_test() { - let args = FeaturesArgs::default(); - let config = Config::default().unwrap(); - let manifest_path: Option = None; - let workspace = get_workspace(&config, manifest_path).unwrap(); - let package = workspace.current().unwrap(); - let mut registry = get_registry(&config, &package).unwrap(); - - let resolve_cargo_result = - resolve(&args, package.package_id(), &mut registry, &workspace); - - assert!(resolve_cargo_result.is_ok()); - } } diff --git a/cargo-geiger/src/format.rs b/cargo-geiger/src/format.rs index 7920dccc..fd38ed42 100644 --- a/cargo-geiger/src/format.rs +++ b/cargo-geiger/src/format.rs @@ -6,7 +6,7 @@ pub mod table; mod display; mod parse; -use cargo::core::dependency::DepKind; +use cargo_metadata::DependencyKind; use std::fmt; use std::str::{self, FromStr}; use strum_macros::EnumIter; @@ -79,11 +79,12 @@ impl fmt::Display for FormatError { } } -pub fn get_kind_group_name(dep_kind: DepKind) -> Option<&'static str> { +pub fn get_kind_group_name(dep_kind: DependencyKind) -> Option<&'static str> { match dep_kind { - DepKind::Build => Some("[build-dependencies]"), - DepKind::Development => Some("[dev-dependencies]"), - DepKind::Normal => None, + DependencyKind::Build => Some("[build-dependencies]"), + DependencyKind::Development => Some("[dev-dependencies]"), + DependencyKind::Normal => None, + _ => panic!("Unrecognised Dependency Kind"), } } @@ -103,15 +104,15 @@ mod format_tests { #[rstest] fn get_kind_group_name_test() { assert_eq!( - get_kind_group_name(DepKind::Build), + get_kind_group_name(DependencyKind::Build), Some("[build-dependencies]") ); assert_eq!( - get_kind_group_name(DepKind::Development), + get_kind_group_name(DependencyKind::Development), Some("[dev-dependencies]") ); - assert_eq!(get_kind_group_name(DepKind::Normal), None); + assert_eq!(get_kind_group_name(DependencyKind::Normal), None); } } 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 64a7854b..74ae0470 100644 --- a/cargo-geiger/src/format/table/handle_text_tree_line.rs +++ b/cargo-geiger/src/format/table/handle_text_tree_line.rs @@ -8,7 +8,7 @@ use super::total_package_counts::TotalPackageCounts; use super::TableParameters; use super::{table_row, table_row_empty}; -use cargo::core::dependency::DepKind; +use cargo_metadata::DependencyKind; use std::collections::HashSet; pub struct HandlePackageParameters<'a> { @@ -18,7 +18,7 @@ pub struct HandlePackageParameters<'a> { } pub fn handle_text_tree_line_extra_deps_group( - dep_kind: DepKind, + dep_kind: DependencyKind, table_lines: &mut Vec, tree_vines: String, ) { @@ -162,12 +162,18 @@ mod handle_text_tree_line_tests { #[rstest( input_dep_kind, expected_kind_group_name, - case(DepKind::Build, Some(String::from("[build-dependencies]"))), - case(DepKind::Development, Some(String::from("[dev-dependencies]"))), - case(DepKind::Normal, None) + case( + DependencyKind::Build, + Some(String::from("[build-dependencies]")) + ), + case( + DependencyKind::Development, + Some(String::from("[dev-dependencies]")) + ), + case(DependencyKind::Normal, None) )] fn handle_text_tree_line_extra_deps_group_test( - input_dep_kind: DepKind, + input_dep_kind: DependencyKind, expected_kind_group_name: Option, ) { let mut table_lines = Vec::::new(); diff --git a/cargo-geiger/src/graph.rs b/cargo-geiger/src/graph.rs index 0c3f57dd..2a84c3dd 100644 --- a/cargo-geiger/src/graph.rs +++ b/cargo-geiger/src/graph.rs @@ -61,7 +61,7 @@ pub fn build_graph<'a>( &config_host, &args.deps_args, &args.target_args, - )?; + ); let cfgs = get_cfgs(config, &args.target_args.target, &workspace)?; let mut graph = Graph { @@ -88,7 +88,7 @@ pub fn build_graph<'a>( &graph_configuration, &mut graph, &mut pending_packages, - )?; + ); } Ok(graph) @@ -126,7 +126,7 @@ fn add_package_dependencies_to_graph( graph_configuration: &GraphConfiguration, graph: &mut Graph, pending_packages: &mut Vec, -) -> CargoResult<()> { +) { let index = graph.nodes[&package_id]; let package = cargo_metadata_parameters .krates @@ -177,15 +177,13 @@ fn add_package_dependencies_to_graph( ); } } - - Ok(()) } fn build_graph_prerequisites<'a>( config_host: &'a InternedString, deps_args: &'a DepsArgs, target_args: &'a TargetArgs, -) -> CargoResult<(ExtraDeps, Option<&'a str>)> { +) -> (ExtraDeps, Option<&'a str>) { let extra_deps = if deps_args.all_deps { ExtraDeps::All } else if deps_args.build_deps { @@ -202,7 +200,7 @@ fn build_graph_prerequisites<'a>( Some(target_args.target.as_deref().unwrap_or(&config_host)) }; - Ok((extra_deps, target)) + (extra_deps, target) } #[cfg(test)] @@ -279,14 +277,12 @@ mod graph_tests { let config_host = InternedString::new("config_host"); let target_args = TargetArgs::default(); - let result = build_graph_prerequisites( + let (extra_deps, _) = build_graph_prerequisites( &config_host, &input_deps_args, &target_args, ); - assert!(result.is_ok()); - let (extra_deps, _) = result.unwrap(); assert_eq!(extra_deps, expected_extra_deps); } @@ -321,14 +317,12 @@ mod graph_tests { let config_host = InternedString::new("default_config_host"); let deps_args = DepsArgs::default(); - let result = build_graph_prerequisites( + let (_, target) = build_graph_prerequisites( &config_host, &deps_args, &input_target_args, ); - assert!(result.is_ok()); - let (_, target) = result.unwrap(); assert_eq!(target, expected_target); } } diff --git a/cargo-geiger/src/main.rs b/cargo-geiger/src/main.rs index 2304fa17..16f03439 100644 --- a/cargo-geiger/src/main.rs +++ b/cargo-geiger/src/main.rs @@ -19,15 +19,14 @@ mod scan; mod tree; use crate::args::{Args, HELP}; -use crate::cli::{ - get_cargo_metadata, get_krates, get_registry, get_workspace, resolve, -}; +use crate::cli::{get_cargo_metadata, get_krates, get_workspace}; use crate::graph::build_graph; use crate::mapping::{CargoMetadataParameters, QueryResolve}; use crate::scan::scan; use cargo::core::shell::Shell; -use cargo::{CliResult, Config}; +use cargo::util::important_paths; +use cargo::{CliError, CliResult, Config}; const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); @@ -52,21 +51,22 @@ fn real_main(args: &Args, config: &mut Config) -> CliResult { }; let workspace = get_workspace(config, args.manifest_path.clone())?; - let root_package = workspace.current()?; - let mut registry = get_registry(config, &root_package)?; - let cargo_metadata_root_package_id = - cargo_metadata.root_package().unwrap().id.clone(); + let cargo_metadata_root_package_id; - let (package_set, _) = resolve( - &args.features_args, - root_package.package_id(), - &mut registry, - &workspace, - )?; + if let Some(cargo_metadata_root_package) = cargo_metadata.root_package() { + cargo_metadata_root_package_id = cargo_metadata_root_package.id.clone(); + } else { + eprintln!( + "manifest path `{}` is a virtual manifest, but this command requires running against an actual package in this workspace", + match args.manifest_path.clone() { + Some(path) => path, + None => important_paths::find_root_manifest_for_wd(config.cwd())?, + }.as_os_str().to_str().unwrap() + ); - let package_ids = package_set.package_ids().collect::>(); - let package_set = registry.get(&package_ids)?; + return CliResult::Err(CliError::code(1)); + } let graph = build_graph( args, @@ -90,7 +90,6 @@ fn real_main(args: &Args, config: &mut Config) -> CliResult { &cargo_metadata_parameters, config, &graph, - &package_set, query_resolve_root_package_id, &workspace, ) diff --git a/cargo-geiger/src/mapping.rs b/cargo-geiger/src/mapping.rs index 3670ce6d..03804d88 100644 --- a/cargo-geiger/src/mapping.rs +++ b/cargo-geiger/src/mapping.rs @@ -72,6 +72,12 @@ pub trait ToCargoCoreDepKind { fn to_cargo_core_dep_kind(&self) -> DepKind; } +pub trait ToCargoGeigerDependencyKind { + fn to_cargo_geiger_dependency_kind( + &self, + ) -> cargo_geiger_serde::DependencyKind; +} + pub trait ToCargoGeigerPackageId { fn to_cargo_geiger_package_id( &self, diff --git a/cargo-geiger/src/mapping/core.rs b/cargo-geiger/src/mapping/core.rs index 698a938b..f39a9087 100644 --- a/cargo-geiger/src/mapping/core.rs +++ b/cargo-geiger/src/mapping/core.rs @@ -60,11 +60,11 @@ impl ToCargoMetadataPackageId for PackageId { mod core_tests { use super::*; - use crate::cli::{get_registry, get_workspace}; + use crate::cli::get_workspace; use cargo::core::registry::PackageRegistry; use cargo::core::Workspace; - use cargo::Config; + use cargo::{CargoResult, Config}; use cargo_metadata::{CargoOpt, Metadata, MetadataCommand}; use krates::Builder as KratesBuilder; use krates::Krates; @@ -148,4 +148,13 @@ mod core_tests { (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/metadata.rs b/cargo-geiger/src/mapping/metadata.rs index 6d98b282..1f7cb5cb 100644 --- a/cargo-geiger/src/mapping/metadata.rs +++ b/cargo-geiger/src/mapping/metadata.rs @@ -5,7 +5,9 @@ use super::{ ToCargoMetadataPackageId, }; -use crate::mapping::{ToCargoGeigerSource, ToCargoMetadataPackage}; +use crate::mapping::{ + ToCargoGeigerDependencyKind, ToCargoGeigerSource, ToCargoMetadataPackage, +}; use cargo::core::dependency::DepKind; use cargo_metadata::{DependencyKind, Metadata}; @@ -77,6 +79,23 @@ impl ToCargoCoreDepKind for DependencyKind { } } +impl ToCargoGeigerDependencyKind for cargo_metadata::DependencyKind { + fn to_cargo_geiger_dependency_kind( + &self, + ) -> cargo_geiger_serde::DependencyKind { + match self { + DependencyKind::Build => cargo_geiger_serde::DependencyKind::Build, + DependencyKind::Development => { + cargo_geiger_serde::DependencyKind::Development + } + DependencyKind::Normal => { + cargo_geiger_serde::DependencyKind::Normal + } + _ => panic!("Unrecognised Dependency Kind"), + } + } +} + impl ToCargoGeigerPackageId for cargo_metadata::PackageId { fn to_cargo_geiger_package_id( &self, @@ -130,11 +149,14 @@ mod metadata_tests { use super::super::GetPackageNameFromCargoMetadataPackageId; use crate::args::FeaturesArgs; - use crate::cli::{get_registry, get_workspace, resolve}; + use crate::cli::get_workspace; use cargo::core::registry::PackageRegistry; - use cargo::core::{Package, Workspace}; - use cargo::Config; + use cargo::core::resolver::ResolveOpts; + use cargo::core::{ + Package, PackageId, PackageIdSpec, PackageSet, Resolve, Workspace, + }; + use cargo::{ops, CargoResult, Config}; use cargo_metadata::{CargoOpt, Metadata, MetadataCommand}; use krates::Builder as KratesBuilder; use rstest::*; @@ -286,4 +308,44 @@ mod metadata_tests { (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) + } + + fn resolve<'a, 'cfg>( + args: &FeaturesArgs, + package_id: PackageId, + registry: &mut PackageRegistry<'cfg>, + workspace: &'a Workspace<'cfg>, + ) -> CargoResult<(PackageSet<'a>, Resolve)> { + let dev_deps = true; // TODO: Review this. + let uses_default_features = !args.no_default_features; + let opts = ResolveOpts::new( + dev_deps, + &args.features.clone(), + args.all_features, + uses_default_features, + ); + let prev = ops::load_pkg_lockfile(workspace)?; + let resolve = ops::resolve_with_previous( + registry, + workspace, + &opts, + prev.as_ref(), + None, + &[PackageIdSpec::from_package_id(package_id)], + true, + )?; + let packages = ops::get_resolved_packages( + &resolve, + PackageRegistry::new(workspace.config())?, + )?; + Ok((packages, resolve)) + } } diff --git a/cargo-geiger/src/scan.rs b/cargo-geiger/src/scan.rs index 8300b762..c90a1e6c 100644 --- a/cargo-geiger/src/scan.rs +++ b/cargo-geiger/src/scan.rs @@ -7,7 +7,8 @@ use crate::args::Args; use crate::format::print_config::PrintConfig; use crate::graph::Graph; use crate::mapping::{ - CargoMetadataParameters, ToCargoCoreDepKind, ToCargoGeigerPackageId, + CargoMetadataParameters, ToCargoGeigerDependencyKind, + ToCargoGeigerPackageId, }; pub use rs_file::RsFileMetricsWrapper; @@ -15,12 +16,9 @@ pub use rs_file::RsFileMetricsWrapper; use default::scan_unsafe; use forbid::scan_forbid_unsafe; -use cargo::core::dependency::DepKind; -use cargo::core::{PackageSet, Workspace}; +use cargo::core::Workspace; use cargo::{CliResult, Config}; -use cargo_geiger_serde::{ - CounterBlock, DependencyKind, PackageInfo, UnsafeInfo, -}; +use cargo_geiger_serde::{CounterBlock, PackageInfo, UnsafeInfo}; use petgraph::visit::EdgeRef; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; @@ -59,7 +57,6 @@ pub fn scan( cargo_metadata_parameters: &CargoMetadataParameters, config: &Config, graph: &Graph, - package_set: &PackageSet, root_package_id: cargo_metadata::PackageId, workspace: &Workspace, ) -> CliResult { @@ -75,7 +72,6 @@ pub fn scan( scan_forbid_unsafe( cargo_metadata_parameters, &graph, - package_set, root_package_id, &scan_parameters, ) @@ -83,7 +79,6 @@ pub fn scan( scan_unsafe( cargo_metadata_parameters, &graph, - package_set, root_package_id, &scan_parameters, workspace, @@ -193,9 +188,7 @@ fn package_metrics( package.add_dependency( dep, - from_cargo_dependency_kind( - edge.weight().to_cargo_core_dep_kind(), - ), + edge.weight().to_cargo_geiger_dependency_kind(), ); } match geiger_context.package_id_to_metrics.get(&package_id) { @@ -213,14 +206,6 @@ fn package_metrics( package_metrics } -fn from_cargo_dependency_kind(kind: DepKind) -> DependencyKind { - match kind { - DepKind::Normal => DependencyKind::Normal, - DepKind::Development => DependencyKind::Development, - DepKind::Build => DependencyKind::Build, - } -} - #[cfg(test)] mod scan_tests { use super::*; diff --git a/cargo-geiger/src/scan/default.rs b/cargo-geiger/src/scan/default.rs index d09140a8..a714ce22 100644 --- a/cargo-geiger/src/scan/default.rs +++ b/cargo-geiger/src/scan/default.rs @@ -15,7 +15,7 @@ use super::{ use table::scan_to_table; use cargo::core::compiler::CompileMode; -use cargo::core::{PackageSet, Workspace}; +use cargo::core::Workspace; use cargo::ops::CompileOptions; use cargo::{CliError, CliResult, Config}; use cargo_geiger_serde::{ReportEntry, SafetyReport}; @@ -23,7 +23,6 @@ use cargo_geiger_serde::{ReportEntry, SafetyReport}; pub fn scan_unsafe( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, - package_set: &PackageSet, root_package_id: cargo_metadata::PackageId, scan_parameters: &ScanParameters, workspace: &Workspace, @@ -33,7 +32,6 @@ pub fn scan_unsafe( cargo_metadata_parameters, graph, output_format, - package_set, root_package_id, scan_parameters, workspace, @@ -41,7 +39,6 @@ pub fn scan_unsafe( None => scan_to_table( cargo_metadata_parameters, graph, - package_set, root_package_id, scan_parameters, workspace, @@ -89,7 +86,6 @@ fn build_compile_options<'a>( fn scan( cargo_metadata_parameters: &CargoMetadataParameters, - package_set: &PackageSet, scan_parameters: &ScanParameters, workspace: &Workspace, ) -> Result { @@ -103,7 +99,6 @@ fn scan( cargo_metadata_parameters, scan_parameters.config, ScanMode::Full, - package_set, scan_parameters.print_config, )?; Ok(ScanDetails { @@ -116,7 +111,6 @@ fn scan_to_report( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, output_format: OutputFormat, - package_set: &PackageSet, root_package_id: cargo_metadata::PackageId, scan_parameters: &ScanParameters, workspace: &Workspace, @@ -124,12 +118,7 @@ fn scan_to_report( let ScanDetails { rs_files_used, geiger_context, - } = scan( - cargo_metadata_parameters, - package_set, - scan_parameters, - workspace, - )?; + } = scan(cargo_metadata_parameters, scan_parameters, workspace)?; let mut report = SafetyReport::default(); for (package, package_metrics_option) in package_metrics( cargo_metadata_parameters, diff --git a/cargo-geiger/src/scan/default/table.rs b/cargo-geiger/src/scan/default/table.rs index 2a11351a..7c73860e 100644 --- a/cargo-geiger/src/scan/default/table.rs +++ b/cargo-geiger/src/scan/default/table.rs @@ -14,7 +14,7 @@ use super::scan; use crate::mapping::CargoMetadataParameters; use cargo::core::shell::Verbosity; -use cargo::core::{PackageSet, Workspace}; +use cargo::core::Workspace; use cargo::{CliError, CliResult}; use colored::Colorize; use std::error::Error; @@ -23,7 +23,6 @@ use std::fmt; pub fn scan_to_table( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, - package_set: &PackageSet, root_package_id: cargo_metadata::PackageId, scan_parameters: &ScanParameters, workspace: &Workspace, @@ -33,12 +32,7 @@ pub fn scan_to_table( let ScanDetails { rs_files_used, geiger_context, - } = scan( - cargo_metadata_parameters, - package_set, - scan_parameters, - workspace, - )?; + } = scan(cargo_metadata_parameters, scan_parameters, workspace)?; if scan_parameters.print_config.verbosity == Verbosity::Verbose { let mut rs_files_used_lines = diff --git a/cargo-geiger/src/scan/find.rs b/cargo-geiger/src/scan/find.rs index 9256d756..8e218f2f 100644 --- a/cargo-geiger/src/scan/find.rs +++ b/cargo-geiger/src/scan/find.rs @@ -1,7 +1,5 @@ use crate::format::print_config::PrintConfig; -use crate::mapping::{ - CargoMetadataParameters, GetRoot, ToCargoMetadataPackage, -}; +use crate::mapping::{CargoMetadataParameters, GetRoot}; use crate::scan::rs_file::{ into_is_entry_point_and_path_buf, into_rs_code_file, into_target_kind, is_file_with_ext, RsFile, RsFileMetricsWrapper, @@ -10,7 +8,6 @@ use crate::scan::PackageMetrics; use super::{GeigerContext, ScanMode}; -use cargo::core::package::PackageSet; use cargo::util::CargoResult; use cargo::{CliError, Config}; use geiger::{find_unsafe_in_file, IncludeTests, RsFileMetrics, ScanFileError}; @@ -23,7 +20,6 @@ pub fn find_unsafe( cargo_metadata_parameters: &CargoMetadataParameters, config: &Config, mode: ScanMode, - package_set: &PackageSet, print_config: &PrintConfig, ) -> Result { let mut progress = cargo::util::Progress::new("Scanning", config); @@ -32,7 +28,6 @@ pub fn find_unsafe( cargo_metadata_parameters, print_config.include_tests, mode, - package_set, |i, count| -> CargoResult<()> { progress.tick(i, count) }, ); progress.clear(); @@ -45,22 +40,13 @@ fn find_unsafe_in_packages( cargo_metadata_parameters: &CargoMetadataParameters, include_tests: IncludeTests, mode: ScanMode, - package_set: &PackageSet, mut progress_step: F, ) -> GeigerContext where F: FnMut(usize, usize) -> CargoResult<()>, { let mut package_id_to_metrics = HashMap::new(); - let packages = package_set - .get_many(package_set.package_ids()) - .unwrap() - .iter() - .map(|p| { - p.to_cargo_metadata_package(cargo_metadata_parameters.metadata) - .unwrap() - }) - .collect::>(); + let packages = cargo_metadata_parameters.metadata.packages.to_vec(); let package_code_files: Vec<_> = find_rs_files_in_packages(&packages).collect(); let package_code_file_count = package_code_files.len(); diff --git a/cargo-geiger/src/scan/forbid.rs b/cargo-geiger/src/scan/forbid.rs index 63ac01d7..999c4408 100644 --- a/cargo-geiger/src/scan/forbid.rs +++ b/cargo-geiger/src/scan/forbid.rs @@ -9,14 +9,12 @@ use super::{package_metrics, ScanMode, ScanParameters}; use table::scan_forbid_to_table; use crate::mapping::CargoMetadataParameters; -use cargo::core::PackageSet; use cargo::{CliResult, Config}; use cargo_geiger_serde::{QuickReportEntry, QuickSafetyReport}; pub fn scan_forbid_unsafe( cargo_metadata_parameters: &CargoMetadataParameters, graph: &Graph, - package_set: &PackageSet, root_package_id: cargo_metadata::PackageId, scan_parameters: &ScanParameters, ) -> CliResult { @@ -26,7 +24,6 @@ pub fn scan_forbid_unsafe( scan_parameters.config, graph, output_format, - package_set, scan_parameters.print_config, root_package_id, ), @@ -34,7 +31,6 @@ pub fn scan_forbid_unsafe( cargo_metadata_parameters, scan_parameters.config, graph, - package_set, scan_parameters.print_config, root_package_id, ), @@ -46,7 +42,6 @@ fn scan_forbid_to_report( config: &Config, graph: &Graph, output_format: OutputFormat, - package_set: &PackageSet, print_config: &PrintConfig, root_package_id: cargo_metadata::PackageId, ) -> CliResult { @@ -54,7 +49,6 @@ fn scan_forbid_to_report( cargo_metadata_parameters, config, ScanMode::EntryPointsOnly, - package_set, print_config, )?; let mut report = QuickSafetyReport::default(); diff --git a/cargo-geiger/src/scan/forbid/table.rs b/cargo-geiger/src/scan/forbid/table.rs index a5ce6dd5..e0a76030 100644 --- a/cargo-geiger/src/scan/forbid/table.rs +++ b/cargo-geiger/src/scan/forbid/table.rs @@ -11,7 +11,6 @@ use super::super::find::find_unsafe; use super::super::ScanMode; use crate::scan::GeigerContext; -use cargo::core::PackageSet; use cargo::{CliResult, Config}; use colored::Colorize; @@ -19,7 +18,6 @@ pub fn scan_forbid_to_table( cargo_metadata_parameters: &CargoMetadataParameters, config: &Config, graph: &Graph, - package_set: &PackageSet, print_config: &PrintConfig, root_package_id: cargo_metadata::PackageId, ) -> CliResult { @@ -55,7 +53,6 @@ pub fn scan_forbid_to_table( cargo_metadata_parameters, config, ScanMode::EntryPointsOnly, - package_set, print_config, )?; @@ -67,7 +64,7 @@ pub fn scan_forbid_to_table( print_config, &mut scan_output_lines, tree_vines, - )?; + ); } } } @@ -124,7 +121,7 @@ fn handle_package_text_tree_line( print_config: &PrintConfig, scan_output_lines: &mut Vec, tree_vines: String, -) -> CliResult { +) { let sym_lock = emoji_symbols.emoji(SymbolKind::Lock); let sym_qmark = emoji_symbols.emoji(SymbolKind::QuestionMark); @@ -148,8 +145,6 @@ fn handle_package_text_tree_line( (&sym_qmark, name.red()) }; scan_output_lines.push(format!("{} {}{}", symbol, tree_vines, name)); - - Ok(()) } #[cfg(test)] diff --git a/cargo-geiger/src/tree.rs b/cargo-geiger/src/tree.rs index e5ddac36..9cb14c2c 100644 --- a/cargo-geiger/src/tree.rs +++ b/cargo-geiger/src/tree.rs @@ -3,7 +3,7 @@ pub mod traversal; use crate::format::print_config::{Prefix, PrintConfig}; use crate::format::Charset; -use cargo::core::dependency::DepKind; +use cargo_metadata::DependencyKind; /// A step towards decoupling some parts of the table-tree printing from the /// dependency graph traversal. @@ -16,7 +16,10 @@ pub enum TextTreeLine { }, /// There are extra dependencies coming and we should print a group header, /// eg. "[build-dependencies]". - ExtraDepsGroup { kind: DepKind, tree_vines: String }, + ExtraDepsGroup { + kind: DependencyKind, + tree_vines: String, + }, } #[derive(Debug, PartialEq)] diff --git a/cargo-geiger/src/tree/traversal/dependency_kind.rs b/cargo-geiger/src/tree/traversal/dependency_kind.rs index 6cd8a8de..7991a6bd 100644 --- a/cargo-geiger/src/tree/traversal/dependency_kind.rs +++ b/cargo-geiger/src/tree/traversal/dependency_kind.rs @@ -1,17 +1,17 @@ use crate::format::print_config::Prefix; +use crate::mapping::CargoMetadataParameters; use crate::tree::traversal::WalkDependencyParameters; use crate::tree::{get_tree_symbols, TextTreeLine, TreeSymbols}; use super::dependency_node::walk_dependency_node; -use crate::mapping::CargoMetadataParameters; -use cargo::core::dependency::DepKind; +use cargo_metadata::DependencyKind; use std::iter::Peekable; use std::slice::Iter; pub fn walk_dependency_kind( cargo_metadata_parameters: &CargoMetadataParameters, - dep_kind: DepKind, + dep_kind: DependencyKind, deps: &mut Vec, walk_dependency_parameters: &mut WalkDependencyParameters, ) -> Vec { @@ -66,13 +66,13 @@ fn handle_walk_dependency_node( } fn push_extra_deps_group_text_tree_line_for_non_normal_dependencies( - dep_kind: DepKind, + dep_kind: DependencyKind, levels_continue: &[bool], tree_symbols: &TreeSymbols, text_tree_lines: &mut Vec, ) { match dep_kind { - DepKind::Normal => (), + DependencyKind::Normal => (), _ => { let mut tree_vines = String::new(); for &continues in &*levels_continue { @@ -101,21 +101,21 @@ mod traversal_tests { input_levels_continue, expected_text_tree_lines, case( - DepKind::Build, + DependencyKind::Build, vec![], vec![ ExtraDepsGroup { - kind: DepKind::Build, + kind: DependencyKind::Build, tree_vines: String::from("") } ] ), case( - DepKind::Build, + DependencyKind::Build, vec![false, true], vec![ ExtraDepsGroup { - kind: DepKind::Build, + kind: DependencyKind::Build, tree_vines: format!( " {} ", get_tree_symbols(Charset::Utf8).down @@ -124,11 +124,11 @@ mod traversal_tests { ] ), case( - DepKind::Development, + DependencyKind::Development, vec![true], vec![ ExtraDepsGroup { - kind: DepKind::Development, + kind: DependencyKind::Development, tree_vines: format!( "{} ", get_tree_symbols(Charset::Utf8).down @@ -137,23 +137,23 @@ mod traversal_tests { ] ), case( - DepKind::Development, + DependencyKind::Development, vec![false], vec![ ExtraDepsGroup { - kind: DepKind::Development, + kind: DependencyKind::Development, tree_vines: String::from(" ") } ] ), case( - DepKind::Normal, + DependencyKind::Normal, vec![], vec![] ) )] fn push_extra_deps_group_text_tree_line_for_non_normal_dependencies_test( - input_dep_kind: DepKind, + input_dep_kind: DependencyKind, input_levels_continue: Vec, expected_text_tree_lines: Vec, ) { diff --git a/cargo-geiger/src/tree/traversal/dependency_node.rs b/cargo-geiger/src/tree/traversal/dependency_node.rs index ef7f22a8..4551de71 100644 --- a/cargo-geiger/src/tree/traversal/dependency_node.rs +++ b/cargo-geiger/src/tree/traversal/dependency_node.rs @@ -1,13 +1,13 @@ use crate::format::print_config::PrintConfig; use crate::graph::Graph; -use crate::mapping::{CargoMetadataParameters, ToCargoCoreDepKind}; +use crate::mapping::CargoMetadataParameters; use crate::tree::traversal::WalkDependencyParameters; use crate::tree::TextTreeLine; use super::construct_tree_vines_string; use super::walk_dependency_kind; -use cargo::core::dependency::DepKind; +use cargo_metadata::DependencyKind; use petgraph::visit::EdgeRef; use petgraph::EdgeDirection; use std::collections::HashMap; @@ -44,7 +44,7 @@ pub fn walk_dependency_node( for (dep_kind, nodes) in dependency_type_nodes.iter_mut() { let mut dep_kind_out = walk_dependency_kind( cargo_metadata_parameters, - *dep_kind, + dep_kind.to_dependency_kind(), nodes, walk_dependency_parameters, ); @@ -59,14 +59,14 @@ fn construct_dependency_type_nodes_hashmap<'a>( graph: &'a Graph, package: &cargo_metadata::PackageId, print_config: &PrintConfig, -) -> HashMap> { +) -> HashMap> { let mut dependency_type_nodes: HashMap< - DepKind, + PrivateDepKind, Vec, > = [ - (DepKind::Build, vec![]), - (DepKind::Development, vec![]), - (DepKind::Normal, vec![]), + (PrivateDepKind::Build, vec![]), + (PrivateDepKind::Development, vec![]), + (PrivateDepKind::Normal, vec![]), ] .iter() .cloned() @@ -82,7 +82,7 @@ fn construct_dependency_type_nodes_hashmap<'a>( }; dependency_type_nodes - .get_mut(&edge.weight().to_cargo_core_dep_kind()) + .get_mut(&PrivateDepKind::from_dependency_kind(&edge.weight())) .unwrap() .push(dependency.clone()); } @@ -90,6 +90,36 @@ fn construct_dependency_type_nodes_hashmap<'a>( dependency_type_nodes } +// cargo_metadata::DependencyKind doesn't implement Eq or Hash, and so can't +// be used in a HashMap +#[derive(Clone, Eq, Hash, PartialEq)] +enum PrivateDepKind { + Build, + Development, + Normal, +} + +impl PrivateDepKind { + fn from_dependency_kind( + dependency_kind: &DependencyKind, + ) -> PrivateDepKind { + match dependency_kind { + DependencyKind::Build => PrivateDepKind::Build, + DependencyKind::Development => PrivateDepKind::Development, + DependencyKind::Normal => PrivateDepKind::Normal, + _ => panic!("Unrecognised DependencyKind"), + } + } + + fn to_dependency_kind(&self) -> DependencyKind { + match self { + PrivateDepKind::Build => DependencyKind::Build, + PrivateDepKind::Development => DependencyKind::Development, + PrivateDepKind::Normal => DependencyKind::Normal, + } + } +} + #[cfg(test)] mod dependency_node_tests { use super::*; @@ -182,15 +212,15 @@ mod dependency_node_tests { ); assert_eq!( - dependency_type_nodes_hashmap[&DepKind::Build].len(), + dependency_type_nodes_hashmap[&PrivateDepKind::Build].len(), expected_build_nodes_length ); assert_eq!( - dependency_type_nodes_hashmap[&DepKind::Development].len(), + dependency_type_nodes_hashmap[&PrivateDepKind::Development].len(), expected_development_nodes_length ); assert_eq!( - dependency_type_nodes_hashmap[&DepKind::Normal].len(), + dependency_type_nodes_hashmap[&PrivateDepKind::Normal].len(), expected_normal_nodes_length ); } diff --git a/cargo-geiger/tests/snapshots/r#mod__test5_workspace_with_virtual_manifest.stderr.snap b/cargo-geiger/tests/snapshots/r#mod__test5_workspace_with_virtual_manifest.stderr.snap index be7ebdbb..1f4e36f5 100644 --- a/cargo-geiger/tests/snapshots/r#mod__test5_workspace_with_virtual_manifest.stderr.snap +++ b/cargo-geiger/tests/snapshots/r#mod__test5_workspace_with_virtual_manifest.stderr.snap @@ -1,8 +1,6 @@ --- -created: "2019-11-05T20:25:00.366323Z" -creator: insta@0.10.1 source: cargo-geiger/tests/mod.rs expression: stderr --- -error: manifest path `{MANIFEST_PATH}` is a virtual manifest, but this command requires running against an actual package in this workspace +manifest path `{MANIFEST_PATH}` is a virtual manifest, but this command requires running against an actual package in this workspace