Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - refactor mapping module to use traits #197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions cargo-geiger/src/format/display.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::format::pattern::Pattern;
use crate::format::Chunk;
use crate::mapping::{
CargoMetadataParameters, GetLicenceFromCargoMetadataPackageId,
GetPackageNameAndVersionFromCargoMetadataPackageId,
GetRepositoryFromCargoMetadataPackageId,
};
use crate::mapping::{CargoMetadataParameters, GetPackageIdInformation};

use cargo_metadata::PackageId;
use std::fmt;
Expand All @@ -20,32 +16,30 @@ impl<'a> fmt::Display for Display<'a> {
for chunk in &self.pattern.0 {
match *chunk {
Chunk::License => {
if let Some(ref license) = self
.cargo_metadata_parameters
.krates
.get_licence_from_cargo_metadata_package_id(
self.package,
if let Some(ref license) =
self.package.get_package_id_licence(
self.cargo_metadata_parameters.krates,
)
{
(write!(fmt, "{}", license))?
}
}
Chunk::Package => {
if let Some((package_name, package_version)) = self.cargo_metadata_parameters
.krates
.get_package_name_and_version_from_cargo_metadata_package_id(self.package) {
if let Some((package_name, package_version)) =
self.package.get_package_id_name_and_version(
self.cargo_metadata_parameters.krates,
)
{
(write!(fmt, "{} {}", package_name, package_version))?
} else {
eprintln!("Failed to format Package: {}", self.package)
}
}
Chunk::Raw(ref s) => (fmt.write_str(s))?,
Chunk::Repository => {
if let Some(ref repository) = self
.cargo_metadata_parameters
.krates
.get_repository_from_cargo_metadata_package_id(
self.package,
if let Some(ref repository) =
self.package.get_package_id_repository(
self.cargo_metadata_parameters.krates,
)
{
(write!(fmt, "{}", repository))?
Expand Down
58 changes: 7 additions & 51 deletions cargo-geiger/src/graph.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub mod extra_deps;

use extra_deps::ExtraDeps;

use crate::args::{Args, DepsArgs, TargetArgs};
use crate::cli::get_cfgs;
use crate::mapping::{
Expand All @@ -14,31 +18,9 @@ use petgraph::graph::NodeIndex;
use std::collections::hash_map::Entry;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum ExtraDeps {
All,
Build,
Dev,
NoMore,
}

impl ExtraDeps {
// This clippy recommendation is valid, but makes this function much harder to read
#[allow(clippy::match_like_matches_macro)]
pub fn allows(&self, dep: DependencyKind) -> bool {
match (self, dep) {
(_, DependencyKind::Normal) => true,
(ExtraDeps::All, _) => true,
(ExtraDeps::Build, DependencyKind::Build) => true,
(ExtraDeps::Dev, DependencyKind::Development) => true,
_ => false,
}
}
}

/// Representation of the package dependency graph
pub struct Graph {
pub graph: petgraph::Graph<PackageId, cargo_metadata::DependencyKind>,
pub graph: petgraph::Graph<PackageId, DependencyKind>,
pub nodes: HashMap<PackageId, NodeIndex>,
}

Expand Down Expand Up @@ -130,7 +112,7 @@ fn add_package_dependencies_to_graph(

let dep_not_replaced_option = cargo_metadata_parameters
.metadata
.deps_not_replaced(package_id.clone());
.deps_not_replaced(&package_id);

match (krates_node_option, dep_not_replaced_option) {
(Some(krates_node), Some(dependencies)) => {
Expand Down Expand Up @@ -197,7 +179,7 @@ fn filter_dependencies<'a>(
.filter(|d| {
d.matches_ignoring_source(
cargo_metadata_parameters.krates,
dependency_package_id.clone(),
dependency_package_id,
)
.unwrap_or(false)
})
Expand All @@ -223,32 +205,6 @@ mod graph_tests {
use super::*;
use rstest::*;

#[rstest(
input_extra_deps,
input_dependency_kind,
expected_allows,
case(ExtraDeps::All, DependencyKind::Normal, true),
case(ExtraDeps::Build, DependencyKind::Normal, true),
case(ExtraDeps::Dev, DependencyKind::Normal, true),
case(ExtraDeps::NoMore, DependencyKind::Normal, true),
case(ExtraDeps::All, DependencyKind::Build, true),
case(ExtraDeps::All, DependencyKind::Development, true),
case(ExtraDeps::Build, DependencyKind::Build, true),
case(ExtraDeps::Build, DependencyKind::Development, false),
case(ExtraDeps::Dev, DependencyKind::Build, false),
case(ExtraDeps::Dev, DependencyKind::Development, true)
)]
fn extra_deps_allows_test(
input_extra_deps: ExtraDeps,
input_dependency_kind: DependencyKind,
expected_allows: bool,
) {
assert_eq!(
input_extra_deps.allows(input_dependency_kind),
expected_allows
);
}

#[rstest(
input_deps_args,
expected_extra_deps,
Expand Down
55 changes: 55 additions & 0 deletions cargo-geiger/src/graph/extra_deps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use cargo_metadata::DependencyKind;

#[derive(Debug, PartialEq)]
pub enum ExtraDeps {
All,
Build,
Dev,
NoMore,
}

impl ExtraDeps {
// This clippy recommendation is valid, but makes this function much harder to read
#[allow(clippy::match_like_matches_macro)]
pub fn allows(&self, dependency_kind: DependencyKind) -> bool {
match (self, dependency_kind) {
(_, DependencyKind::Normal) => true,
(ExtraDeps::All, _) => true,
(ExtraDeps::Build, DependencyKind::Build) => true,
(ExtraDeps::Dev, DependencyKind::Development) => true,
_ => false,
}
}
}

#[cfg(test)]
mod extra_deps_tests {
use super::*;
use rstest::*;

#[rstest(
input_extra_deps,
input_dependency_kind,
expected_allows,
case(ExtraDeps::All, DependencyKind::Normal, true),
case(ExtraDeps::Build, DependencyKind::Normal, true),
case(ExtraDeps::Dev, DependencyKind::Normal, true),
case(ExtraDeps::NoMore, DependencyKind::Normal, true),
case(ExtraDeps::All, DependencyKind::Build, true),
case(ExtraDeps::All, DependencyKind::Development, true),
case(ExtraDeps::Build, DependencyKind::Build, true),
case(ExtraDeps::Build, DependencyKind::Development, false),
case(ExtraDeps::Dev, DependencyKind::Build, false),
case(ExtraDeps::Dev, DependencyKind::Development, true)
)]
fn extra_deps_allows_test(
input_extra_deps: ExtraDeps,
input_dependency_kind: DependencyKind,
expected_allows: bool,
) {
assert_eq!(
input_extra_deps.allows(input_dependency_kind),
expected_allows
);
}
}
Loading