-use std::collections::BTreeMap;
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use toml_edit::KeyMut;
use super::manifest::str_or_1_len_table;
-use crate::core::FeatureMap;
-use crate::core::FeatureValue;
use crate::core::GitReference;
use crate::core::SourceId;
use crate::core::Summary;
/// If the dependency is renamed, this is the new name for the dependency
/// as a string. None if it is not renamed.
pub rename: Option<String>,
-
- /// Features that are exposed by the dependency
- pub available_features: BTreeMap<String, Vec<String>>,
}
impl Dependency {
source: None,
registry: None,
rename: None,
- available_features: Default::default(),
}
}
self
}
- /// Populate from cargo
- pub fn set_available_features_from_cargo(
- mut self,
- available_features: &FeatureMap,
- ) -> Dependency {
- self.available_features = available_features
- .iter()
- .map(|(k, v)| {
- (
- k.as_str().to_owned(),
- v.iter()
- .filter_map(|v| match v {
- FeatureValue::Feature(f) => Some(f.as_str().to_owned()),
- FeatureValue::Dep { .. } | FeatureValue::DepFeature { .. } => None,
- })
- .collect::<Vec<_>>(),
- )
- })
- .collect();
- self
- }
-
/// Set whether the dependency is optional
#[allow(dead_code)]
pub fn set_optional(mut self, opt: bool) -> Self {
None
};
- let available_features = BTreeMap::default();
-
let optional = table.get("optional").and_then(|v| v.as_bool());
let dep = Self {
registry,
default_features,
features,
- available_features,
optional,
inherited_features: None,
};
} else {
RegistrySource::new(other.version().to_string()).into()
};
- Dependency::new(other.name().as_str())
- .set_source(source)
- .set_available_features_from_cargo(other.features())
+ Dependency::new(other.name().as_str()).set_source(source)
}
}
mod dependency;
mod manifest;
+use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::VecDeque;
use std::path::Path;
use crate::core::dependency::DepKind;
use crate::core::registry::PackageRegistry;
+use crate::core::FeatureMap;
+use crate::core::FeatureValue;
use crate::core::Package;
use crate::core::QueryKind;
use crate::core::Registry;
use crate::core::Shell;
+use crate::core::Summary;
use crate::core::Workspace;
use crate::CargoResult;
use crate::Config;
section: &DepTable,
config: &Config,
registry: &mut PackageRegistry<'_>,
-) -> CargoResult<Dependency> {
+) -> CargoResult<DependencyEx> {
let crate_spec = arg
.crate_spec
.as_deref()
MaybeWorkspace::Other(query) => query,
};
- dependency = populate_available_features(dependency, &query, registry)?;
+ let dependency = populate_available_features(dependency, &query, registry)?;
Ok(dependency)
}
dependency
}
+pub struct DependencyEx {
+ dep: Dependency,
+ available_features: BTreeMap<String, Vec<String>>,
+}
+
+impl DependencyEx {
+ fn new(dep: Dependency) -> Self {
+ Self {
+ dep,
+ available_features: Default::default(),
+ }
+ }
+
+ fn set_available_features(&mut self, available_features: &FeatureMap) {
+ self.available_features = available_features
+ .iter()
+ .map(|(k, v)| {
+ (
+ k.as_str().to_owned(),
+ v.iter()
+ .filter_map(|v| match v {
+ FeatureValue::Feature(f) => Some(f.as_str().to_owned()),
+ FeatureValue::Dep { .. } | FeatureValue::DepFeature { .. } => None,
+ })
+ .collect::<Vec<_>>(),
+ )
+ })
+ .collect();
+ }
+}
+
+impl<'s> From<&'s Summary> for DependencyEx {
+ fn from(other: &'s Summary) -> Self {
+ let dep = Dependency::from(other);
+ let mut dep = Self::new(dep);
+ dep.set_available_features(other.features());
+ dep
+ }
+}
+
+impl std::fmt::Display for DependencyEx {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ self.dep.fmt(f)
+ }
+}
+
+impl std::ops::Deref for DependencyEx {
+ type Target = Dependency;
+
+ fn deref(&self) -> &Self::Target {
+ &self.dep
+ }
+}
+
/// Lookup available features
fn populate_available_features(
- mut dependency: Dependency,
+ dependency: Dependency,
query: &crate::core::dependency::Dependency,
registry: &mut PackageRegistry<'_>,
-) -> CargoResult<Dependency> {
+) -> CargoResult<DependencyEx> {
+ let mut dependency = DependencyEx::new(dependency);
+
if !dependency.available_features.is_empty() {
return Ok(dependency);
}
.ok_or_else(|| {
anyhow::format_err!("the crate `{dependency}` could not be found in registry index.")
})?;
- dependency = dependency.set_available_features_from_cargo(lowest_common_denominator.features());
+ dependency.set_available_features(lowest_common_denominator.features());
Ok(dependency)
}
-fn print_msg(shell: &mut Shell, dep: &Dependency, section: &[String]) -> CargoResult<()> {
+fn print_msg(shell: &mut Shell, dep: &DependencyEx, section: &[String]) -> CargoResult<()> {
use std::fmt::Write;
if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) {