From 708adfc6bd494d5ca8be9d7826a967d02c5d82b9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 29 Jun 2021 10:00:43 +0200 Subject: Refactor: Outsource condition-checking and parsing to helper fn Signed-off-by: Matthias Beyer --- src/package/dag.rs | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/src/package/dag.rs b/src/package/dag.rs index fd31132..c805121 100644 --- a/src/package/dag.rs +++ b/src/package/dag.rs @@ -51,6 +51,19 @@ impl Dag { conditional_data: &ConditionData<'_>, // required for selecting packages with conditional dependencies ) -> Result { + /// helper fn with bad name to check the dependency condition of a dependency and parse the dependency into a tuple of + /// name and version for further processing + fn process(d: &D, conditional_data: &ConditionData<'_>) + -> Result<(bool, PackageName, PackageVersionConstraint)> + { + // Check whether the condition of the dependency matches our data + let take = d.check_condition(conditional_data)?; + let (name, version) = d.parse_as_name_and_version()?; + + // (dependency check result, name of the dependency, version of the dependency) + Ok((take, name, version)) + } + /// Helper fn to get the dependencies of a package /// /// This function helps getting the dependencies of a package as an iterator over @@ -61,32 +74,17 @@ impl Dag { fn get_package_dependencies<'a>(package: &'a Package, conditional_data: &'a ConditionData<'_>) -> impl Iterator> + 'a { - let build_dependencies = package.dependencies() - .build() - .iter() - .map(move |d| { - // Check whether the condition of the dependency matches our data - let take = d.check_condition(conditional_data)?; - let (name, version) = d.parse_as_name_and_version()?; - // (dependency check result, name of the dependency, version of the dependency) - Ok((take, name, version)) - }); - - let runtime_dependencies = package.dependencies() - .runtime() + package.dependencies() + .build() .iter() - .map(move |d| { - // Check whether the condition of the dependency matches our data - let take = d.check_condition(conditional_data)?; - let (name, version) = d.parse_as_name_and_version()?; - - // (dependency check result, name of the dependency, version of the dependency) - Ok((take, name, version)) - }); - - build_dependencies - .chain(runtime_dependencies) + .map(move |d| process(d, conditional_data)) + .chain({ + package.dependencies() + .runtime() + .iter() + .map(move |d| process(d, conditional_data)) + }) // Now filter out all dependencies where their condition did not match our // `conditional_data`. -- cgit v1.2.3