Skip to content

Commit

Permalink
refactor: polish strip feature logic
Browse files Browse the repository at this point in the history
  • Loading branch information
baby230211 committed Mar 10, 2024
1 parent 6a22946 commit 5f7d81e
Showing 1 changed file with 46 additions and 57 deletions.
103 changes: 46 additions & 57 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::AlreadyPrintedError;
use anyhow::{anyhow, bail, Context as _};
use cargo_platform::Platform;
use cargo_util::paths;
use cargo_util_schemas::manifest;
use cargo_util_schemas::manifest::RustVersion;
use cargo_util_schemas::manifest::{self};
use cargo_util_schemas::manifest::{RustVersion, TomlManifest};
use itertools::Itertools;
use lazycell::LazyCell;
use pathdiff::diff_paths;
Expand Down Expand Up @@ -317,14 +317,7 @@ pub fn prepare_for_publish(
}
let all = |_d: &manifest::TomlDependency| true;

let target_dep = me.target.as_ref().map(|target_map| {
target_map
.iter()
.filter_map(|(_k, v)| v.dev_dependencies.clone())
.flatten()
.collect::<BTreeMap<_, _>>()
});
return Ok(manifest::TomlManifest {
let mut manifest = manifest::TomlManifest {
package: Some(package),
project: None,
profile: me.profile.clone(),
Expand All @@ -342,11 +335,7 @@ pub fn prepare_for_publish(
dev_dependencies2: None,
build_dependencies: map_deps(gctx, me.build_dependencies(), all)?,
build_dependencies2: None,
features: map_feature(
me.features.as_ref(),
me.dev_dependencies(),
target_dep.as_ref(),
)?,
features: me.features.clone(),
target: match me.target.as_ref().map(|target_map| {
target_map
.iter()
Expand Down Expand Up @@ -378,54 +367,54 @@ pub fn prepare_for_publish(
badges: me.badges.clone(),
cargo_features: me.cargo_features.clone(),
lints: me.lints.clone(),
});

fn map_feature(
features: Option<&BTreeMap<manifest::FeatureName, Vec<String>>>,
dev_deps: Option<&BTreeMap<manifest::PackageName, manifest::InheritableDependency>>,
target_deps: Option<&BTreeMap<manifest::PackageName, manifest::InheritableDependency>>,
) -> CargoResult<Option<BTreeMap<manifest::FeatureName, Vec<String>>>> {
let Some(features) = features else {
return Ok(None);
};
let features = features
.iter()
.map(|(name, feature_deps)| {
let feature_deps = feature_deps
.iter()
.filter(|feature_dep| {
};
strip_features(&mut manifest);
return Ok(manifest);

fn strip_features(manifest: &mut TomlManifest) {
fn is_include_dep(
key: &manifest::PackageName,
deps: Option<&BTreeMap<manifest::PackageName, manifest::InheritableDependency>>,
) -> bool {
deps.and_then(|d| d.get(key)).is_some()
}
let features = manifest.features.as_mut();
match features {
Some(features) => {
for (_name, feature_deps) in features.iter_mut() {
feature_deps.retain(|feature_dep| {
let feature_value = FeatureValue::new(InternedString::new(feature_dep));
match feature_value {
FeatureValue::Dep { dep_name }
| FeatureValue::DepFeature { dep_name, .. } => {
let k = &manifest::PackageName::new(dep_name.to_string()).unwrap();

if let Some(dev_deps) = dev_deps {
if let Some(dep) = dev_deps.get(k) {
if let manifest::InheritableDependency::Value(def) = dep {
return def.is_version_specified();
}
}
};
if let Some(target_deps) = target_deps {
if let Some(dep) = target_deps.get(k) {
if let manifest::InheritableDependency::Value(def) = dep {
return def.is_version_specified();
}
}
}
true
let include = is_include_dep(k, manifest.dependencies.as_ref())
|| is_include_dep(k, manifest.dev_dependencies.as_ref())
|| is_include_dep(k, manifest.build_dependencies.as_ref())
|| manifest.target.as_ref().iter().any(|target_map| {
target_map.iter().any(|(_k, v)| {
let include =
is_include_dep(k, v.dependencies.as_ref())
|| is_include_dep(
k,
v.dev_dependencies.as_ref(),
)
|| is_include_dep(
k,
v.build_dependencies.as_ref(),
);
include
})
});
include
}
_ => true,
}
})
.map(|feature_dep| feature_dep.to_string())
.collect::<Vec<String>>();
(name, feature_deps)
})
.map(|(name, feature_deps)| Ok((name.clone(), feature_deps)))
.collect::<CargoResult<BTreeMap<_, _>>>()?;
Ok(Some(features))
});
}
}
_ => {}
}
}

fn map_deps(
Expand Down Expand Up @@ -836,8 +825,8 @@ pub fn to_real_manifest(
{
d.public = None;
manifest_ctx.warnings.push(format!(
"ignoring `public` on dependency {name}, pass `-Zpublic-dependency` to enable support for it", name = &dep.name_in_toml()
))
"ignoring `public` on dependency {name}, pass `-Zpublic-dependency` to enable support for it", name = &dep.name_in_toml()
))
}
} else {
d.public = None;
Expand Down

0 comments on commit 5f7d81e

Please sign in to comment.