Skip to content

Commit 7621108

Browse files
committed
Extract function to build feature map for readability
1 parent 7b54226 commit 7621108

File tree

1 file changed

+61
-50
lines changed

1 file changed

+61
-50
lines changed

src/cargo/core/summary.rs

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,60 +48,12 @@ impl Summary {
4848
)
4949
}
5050
}
51-
let mut features_new = BTreeMap::new();
52-
for (feature, list) in features.iter() {
53-
let mut values = vec![];
54-
for dep in list {
55-
use self::FeatureValue::*;
56-
let val = FeatureValue::build(dep, |fs| (&features).get(fs).is_some());
57-
if let &Feature(_) = &val {
58-
// Return early to avoid doing unnecessary work
59-
values.push(val);
60-
continue;
61-
}
62-
// Find data for the referenced dependency...
63-
let dep_data = {
64-
let dep_name = match &val {
65-
&Feature(_) => "",
66-
&Crate(ref dep_name) | &CrateFeature(ref dep_name, _) => dep_name,
67-
};
68-
dependencies.iter().find(|d| *d.name() == *dep_name)
69-
};
70-
match (&val, dep_data) {
71-
(&Crate(ref dep), Some(d)) => {
72-
if !d.is_optional() {
73-
bail!(
74-
"Feature `{}` depends on `{}` which is not an \
75-
optional dependency.\nConsider adding \
76-
`optional = true` to the dependency",
77-
feature,
78-
dep
79-
)
80-
}
81-
}
82-
(&CrateFeature(ref dep_name, _), None) => bail!(
83-
"Feature `{}` requires a feature of `{}` which is not a \
84-
dependency",
85-
feature,
86-
dep_name
87-
),
88-
(&Crate(ref dep), None) => bail!(
89-
"Feature `{}` includes `{}` which is neither \
90-
a dependency nor another feature",
91-
feature,
92-
dep
93-
),
94-
(&CrateFeature(_, _), Some(_)) | (&Feature(_), _) => {}
95-
}
96-
values.push(val);
97-
}
98-
features_new.insert(feature.clone(), values);
99-
}
51+
let feature_map = build_feature_map(features, &dependencies)?;
10052
Ok(Summary {
10153
inner: Rc::new(Inner {
10254
package_id: pkg_id,
10355
dependencies,
104-
features: features_new,
56+
features: feature_map,
10557
checksum: None,
10658
links: links.map(|l| InternedString::new(&l)),
10759
}),
@@ -172,6 +124,65 @@ impl PartialEq for Summary {
172124
}
173125
}
174126

127+
// Checks features for errors, bailing out a CargoResult:Err if invalid,
128+
// and creates FeatureValues for each feature.
129+
fn build_feature_map(
130+
features: BTreeMap<String, Vec<String>>,
131+
dependencies: &Vec<Dependency>,
132+
) -> CargoResult<FeatureMap> {
133+
use self::FeatureValue::*;
134+
let mut map = BTreeMap::new();
135+
for (feature, list) in features.iter() {
136+
let mut values = vec![];
137+
for dep in list {
138+
let val = FeatureValue::build(dep, |fs| (&features).get(fs).is_some());
139+
if let &Feature(_) = &val {
140+
values.push(val);
141+
continue;
142+
}
143+
144+
// Find data for the referenced dependency...
145+
let dep_data = {
146+
let dep_name = match &val {
147+
&Feature(_) => "",
148+
&Crate(ref dep_name) | &CrateFeature(ref dep_name, _) => dep_name,
149+
};
150+
dependencies.iter().find(|d| *d.name() == *dep_name)
151+
};
152+
153+
match (&val, dep_data) {
154+
(&Crate(ref dep), Some(d)) => {
155+
if !d.is_optional() {
156+
bail!(
157+
"Feature `{}` depends on `{}` which is not an \
158+
optional dependency.\nConsider adding \
159+
`optional = true` to the dependency",
160+
feature,
161+
dep
162+
)
163+
}
164+
}
165+
(&CrateFeature(ref dep_name, _), None) => bail!(
166+
"Feature `{}` requires a feature of `{}` which is not a \
167+
dependency",
168+
feature,
169+
dep_name
170+
),
171+
(&Crate(ref dep), None) => bail!(
172+
"Feature `{}` includes `{}` which is neither \
173+
a dependency nor another feature",
174+
feature,
175+
dep
176+
),
177+
(&CrateFeature(_, _), Some(_)) | (&Feature(_), _) => {}
178+
}
179+
values.push(val);
180+
}
181+
map.insert(feature.clone(), values);
182+
}
183+
Ok(map)
184+
}
185+
175186
/// FeatureValue represents the types of dependencies a feature can have:
176187
///
177188
/// * Another feature

0 commit comments

Comments
 (0)