use crate::{
CargoMetadata, DependencyKind, Error, JsonValue, MetadataCommand, PackageId,
graph::{
BuildTarget, BuildTargetId, BuildTargetImpl, BuildTargetKind, Cycles, DependencyDirection,
OwnedBuildTargetId, PackageIx, PackageQuery, PackageSet, cargo_version_matches,
feature::{FeatureGraphImpl, FeatureId, FeatureLabel, FeatureNode},
},
petgraph_support::{IxBitSet, scc::Sccs, topo::TopoWithCycles},
platform::{EnabledTernary, PlatformSpec, PlatformStatus, PlatformStatusImpl},
};
use ahash::AHashMap;
use camino::{Utf8Path, Utf8PathBuf};
use fixedbitset::FixedBitSet;
use indexmap::{IndexMap, IndexSet};
use once_cell::sync::OnceCell;
use petgraph::{
algo::{DfsSpace, has_path_connecting},
graph::EdgeReference,
prelude::*,
visit::EdgeFiltered,
};
use semver::{Version, VersionReq};
use smallvec::SmallVec;
use std::{
collections::{BTreeMap, HashSet},
fmt,
iter::{self, FromIterator},
};
use super::feature::{FeatureFilter, FeatureSet};
#[derive(Clone, Debug)]
pub struct PackageGraph {
pub(super) dep_graph: Graph<PackageId, PackageLinkImpl, Directed, PackageIx>,
pub(super) sccs: OnceCell<Sccs<PackageIx>>,
pub(super) feature_graph: OnceCell<FeatureGraphImpl>,
pub(super) data: PackageGraphData,
}
#[derive(Clone, Debug)]
pub(super) struct PackageGraphData {
pub(super) packages: AHashMap<PackageId, PackageMetadataImpl>,
pub(super) workspace: WorkspaceImpl,
}
impl PackageGraph {
pub fn from_command(command: &mut MetadataCommand) -> Result<Self, Error> {
command.build_graph()
}
pub fn from_metadata(metadata: CargoMetadata) -> Result<Self, Error> {
Self::build(metadata.0).map_err(|error| *error)
}
pub fn from_json(json: impl AsRef<str>) -> Result<Self, Error> {
let metadata = CargoMetadata::parse_json(json)?;
Self::from_metadata(metadata)
}
#[doc(hidden)]
pub fn verify(&self) -> Result<(), Error> {
let node_count = self.dep_graph.node_count();
let package_count = self.data.packages.len();
if node_count != package_count {
return Err(Error::PackageGraphInternalError(format!(
"number of nodes = {node_count} different from packages = {package_count}",
)));
}
let workspace = self.workspace();
let workspace_ids: HashSet<_> = workspace.member_ids().collect();
for metadata in self.packages() {
let package_id = metadata.id();
match metadata.source().workspace_path() {
Some(workspace_path) => {
let metadata2 = workspace.member_by_path(workspace_path);
let metadata2_id = metadata2.map(|metadata| metadata.id());
if !matches!(metadata2_id, Ok(id) if id == package_id) {
return Err(Error::PackageGraphInternalError(format!(
"package {package_id} has workspace path {workspace_path:?} but query by path returned {metadata2_id:?}",
)));
}
let metadata3 = workspace.member_by_name(metadata.name());
let metadata3_id = metadata3.map(|metadata| metadata.id());
if !matches!(metadata3_id, Ok(id) if id == package_id) {
return Err(Error::PackageGraphInternalError(format!(
"package {} has name {}, but workspace query by name returned {:?}",
package_id,
metadata.name(),
metadata3_id,
)));
}
}
None => {
if workspace_ids.contains(package_id) {
return Err(Error::PackageGraphInternalError(format!(
"package {package_id} has no workspace path but is in workspace",
)));
}
}
}
for build_target in metadata.build_targets() {
match build_target.id() {
BuildTargetId::Library | BuildTargetId::BuildScript => {
build_target.name();
}
BuildTargetId::Binary(name)
| BuildTargetId::Example(name)
| BuildTargetId::Test(name)
| BuildTargetId::Benchmark(name) => {
if name != build_target.name() {
return Err(Error::PackageGraphInternalError(format!(
"package {} has build target name mismatch ({} != {})",
package_id,
name,
build_target.name(),
)));
}
}
}
let id_kind_mismatch = match build_target.id() {
BuildTargetId::Library => match build_target.kind() {
BuildTargetKind::LibraryOrExample(_) | BuildTargetKind::ProcMacro => false,
BuildTargetKind::Binary => true,
},
BuildTargetId::Example(_) => match build_target.kind() {
BuildTargetKind::LibraryOrExample(_) => false,
BuildTargetKind::ProcMacro | BuildTargetKind::Binary => true,
},
BuildTargetId::BuildScript
| BuildTargetId::Binary(_)
| BuildTargetId::Test(_)
| BuildTargetId::Benchmark(_) => match build_target.kind() {
BuildTargetKind::LibraryOrExample(_) | BuildTargetKind::ProcMacro => true,
BuildTargetKind::Binary => false,
},
};
if id_kind_mismatch {
return Err(Error::PackageGraphInternalError(format!(
"package {} has build target id {:?}, which doesn't match kind {:?}",
package_id,
build_target.id(),
build_target.kind(),
)));
}
}
for link in self.dep_links_ixs_directed(metadata.package_ix(), Outgoing) {
let to = link.to();
let to_id = to.id();
let to_version = to.version();
let req = link.version_req();
if !cargo_version_matches(req, to_version) {
return Err(Error::PackageGraphInternalError(format!(
"{package_id} -> {to_id}: version ({to_version}) doesn't match requirement ({req:?})",
)));
}
let is_any = link.normal().is_present()
|| link.build().is_present()
|| link.dev().is_present();
if !is_any {
return Err(Error::PackageGraphInternalError(format!(
"{package_id} -> {to_id}: no edge info found",
)));
}
}
}
self.feature_graph().verify()?;
Ok(())
}
pub fn workspace(&self) -> Workspace<'_> {
Workspace {
graph: self,
inner: &self.data.workspace,
}
}
pub fn package_ids(&self) -> impl ExactSizeIterator<Item = &PackageId> {
self.data.package_ids()
}
pub fn packages(&self) -> impl ExactSizeIterator<Item = PackageMetadata<'_>> {
self.data
.packages
.values()
.map(move |inner| PackageMetadata::new(self, inner))
}
pub fn metadata(&self, package_id: &PackageId) -> Result<PackageMetadata<'_>, Error> {
let inner = self
.data
.metadata_impl(package_id)
.ok_or_else(|| Error::UnknownPackageId(package_id.clone()))?;
Ok(PackageMetadata::new(self, inner))
}
pub fn package_count(&self) -> usize {
self.dep_graph.node_count()
}
pub fn link_count(&self) -> usize {
self.dep_graph.edge_count()
}
pub fn new_depends_cache(&self) -> DependsCache<'_> {
DependsCache::new(self)
}
pub fn depends_on(&self, package_a: &PackageId, package_b: &PackageId) -> Result<bool, Error> {
let mut depends_cache = self.new_depends_cache();
depends_cache.depends_on(package_a, package_b)
}
pub fn directly_depends_on(
&self,
package_a: &PackageId,
package_b: &PackageId,
) -> Result<bool, Error> {
let a_ix = self.package_ix(package_a)?;
let b_ix = self.package_ix(package_b)?;
Ok(self.dep_graph.contains_edge(a_ix, b_ix))
}
pub fn cycles(&self) -> Cycles<'_> {
Cycles::new(self)
}
fn dep_links_ixs_directed(
&self,
package_ix: NodeIndex<PackageIx>,
dir: Direction,
) -> impl Iterator<Item = PackageLink<'_>> {
self.dep_graph
.edges_directed(package_ix, dir)
.map(move |edge| self.edge_ref_to_link(edge))
}
fn link_between_ixs(
&self,
from_ix: NodeIndex<PackageIx>,
to_ix: NodeIndex<PackageIx>,
) -> Option<PackageLink<'_>> {
self.dep_graph
.find_edge(from_ix, to_ix)
.map(|edge_ix| self.edge_ix_to_link(edge_ix))
}
pub(super) fn sccs(&self) -> &Sccs<PackageIx> {
self.sccs.get_or_init(|| {
let edge_filtered =
EdgeFiltered::from_fn(&self.dep_graph, |edge| !edge.weight().dev_only());
let topo = TopoWithCycles::new(&edge_filtered);
Sccs::new(&self.dep_graph, |scc| {
topo.sort_nodes(scc);
})
})
}
#[doc(hidden)]
pub fn invalidate_caches(&mut self) {
self.sccs.take();
self.feature_graph.take();
}
pub(super) fn dep_graph(&self) -> &Graph<PackageId, PackageLinkImpl, Directed, PackageIx> {
&self.dep_graph
}
pub(super) fn edge_ref_to_link<'g>(
&'g self,
edge: EdgeReference<'g, PackageLinkImpl, PackageIx>,
) -> PackageLink<'g> {
PackageLink::new(
self,
edge.source(),
edge.target(),
edge.id(),
Some(edge.weight()),
)
}
pub(super) fn edge_ix_to_link(&self, edge_ix: EdgeIndex<PackageIx>) -> PackageLink<'_> {
let (source_ix, target_ix) = self
.dep_graph
.edge_endpoints(edge_ix)
.expect("valid edge ix");
PackageLink::new(
self,
source_ix,
target_ix,
edge_ix,
self.dep_graph.edge_weight(edge_ix),
)
}
pub(super) fn package_ixs<'g, 'a, B>(
&'g self,
package_ids: impl IntoIterator<Item = &'a PackageId>,
) -> Result<B, Error>
where
B: iter::FromIterator<NodeIndex<PackageIx>>,
{
package_ids
.into_iter()
.map(|package_id| self.package_ix(package_id))
.collect()
}
pub(super) fn package_ix(&self, package_id: &PackageId) -> Result<NodeIndex<PackageIx>, Error> {
Ok(self.metadata(package_id)?.package_ix())
}
}
impl PackageGraphData {
pub fn package_ids(&self) -> impl ExactSizeIterator<Item = &PackageId> {
self.packages.keys()
}
#[inline]
pub(super) fn metadata_impl(&self, package_id: &PackageId) -> Option<&PackageMetadataImpl> {
self.packages.get(package_id)
}
}
#[derive(Clone, Debug)]
pub struct DependsCache<'g> {
package_graph: &'g PackageGraph,
dfs_space: DfsSpace<NodeIndex<PackageIx>, FixedBitSet>,
}
impl<'g> DependsCache<'g> {
pub fn new(package_graph: &'g PackageGraph) -> Self {
Self {
package_graph,
dfs_space: DfsSpace::new(&package_graph.dep_graph),
}
}
pub fn depends_on(
&mut self,
package_a: &PackageId,
package_b: &PackageId,
) -> Result<bool, Error> {
let a_ix = self.package_graph.package_ix(package_a)?;
let b_ix = self.package_graph.package_ix(package_b)?;
Ok(has_path_connecting(
self.package_graph.dep_graph(),
a_ix,
b_ix,
Some(&mut self.dfs_space),
))
}
}
#[derive(Clone, Debug)]
pub struct Workspace<'g> {
graph: &'g PackageGraph,
pub(super) inner: &'g WorkspaceImpl,
}
impl<'g> Workspace<'g> {
pub fn root(&self) -> &'g Utf8Path {
&self.inner.root
}
pub fn target_directory(&self) -> &'g Utf8Path {
&self.inner.target_directory
}
pub fn build_directory(&self) -> Option<&'g Utf8Path> {
self.inner.build_directory.as_deref()
}
pub fn default_member_ids(&self) -> impl ExactSizeIterator<Item = &'g PackageId> + use<'g> {
self.inner.default_members.iter()
}
pub fn default_members(&self) -> impl ExactSizeIterator<Item = PackageMetadata<'g>> + use<'g> {
let graph = self.graph;
self.inner
.default_members
.iter()
.map(move |id| graph.metadata(id).expect("valid package ID"))
}
pub fn member_count(&self) -> usize {
self.inner.members_by_path.len()
}
pub fn contains_name(&self, name: impl AsRef<str>) -> bool {
self.inner.members_by_name.contains_key(name.as_ref())
}
pub fn contains_path(&self, path: impl AsRef<Utf8Path>) -> bool {
self.inner.members_by_path.contains_key(path.as_ref())
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = PackageMetadata<'g>> + use<'g> {
self.iter_by_path().map(|(_, package)| package)
}
pub fn iter_by_path(
&self,
) -> impl ExactSizeIterator<Item = (&'g Utf8Path, PackageMetadata<'g>)> + use<'g> {
let graph = self.graph;
self.inner.members_by_path.iter().map(move |(path, id)| {
(
path.as_path(),
graph.metadata(id).expect("valid package ID"),
)
})
}
pub fn iter_by_name(
&self,
) -> impl ExactSizeIterator<Item = (&'g str, PackageMetadata<'g>)> + use<'g> {
let graph = self.graph;
self.inner
.members_by_name
.iter()
.map(move |(name, id)| (name.as_ref(), graph.metadata(id).expect("valid package ID")))
}
pub fn member_ids(&self) -> impl ExactSizeIterator<Item = &'g PackageId> + use<'g> {
self.inner.members_by_path.values()
}
pub fn member_by_path(&self, path: impl AsRef<Utf8Path>) -> Result<PackageMetadata<'g>, Error> {
let path = path.as_ref();
let id = self
.inner
.members_by_path
.get(path)
.ok_or_else(|| Error::UnknownWorkspacePath(path.to_path_buf()))?;
Ok(self.graph.metadata(id).expect("valid package ID"))
}
pub fn members_by_paths<B>(
&self,
paths: impl IntoIterator<Item = impl AsRef<Utf8Path>>,
) -> Result<B, Error>
where
B: FromIterator<PackageMetadata<'g>>,
{
paths
.into_iter()
.map(|path| self.member_by_path(path.as_ref()))
.collect()
}
pub fn member_by_name(&self, name: impl AsRef<str>) -> Result<PackageMetadata<'g>, Error> {
let name = name.as_ref();
let id = self
.inner
.members_by_name
.get(name)
.ok_or_else(|| Error::UnknownWorkspaceName(name.to_string()))?;
Ok(self.graph.metadata(id).expect("valid package ID"))
}
pub fn members_by_names<B>(
&self,
names: impl IntoIterator<Item = impl AsRef<str>>,
) -> Result<B, Error>
where
B: FromIterator<PackageMetadata<'g>>,
{
names
.into_iter()
.map(|name| self.member_by_name(name.as_ref()))
.collect()
}
pub fn metadata_table(&self) -> &'g JsonValue {
&self.inner.metadata_table
}
}
#[cfg(feature = "rayon1")]
mod workspace_rayon {
use super::*;
use rayon::prelude::*;
impl<'g> Workspace<'g> {
pub fn par_iter(&self) -> impl ParallelIterator<Item = PackageMetadata<'g>> + use<'g> {
self.par_iter_by_path().map(|(_, package)| package)
}
pub fn par_iter_by_path(
&self,
) -> impl ParallelIterator<Item = (&'g Utf8Path, PackageMetadata<'g>)> + use<'g> {
let graph = self.graph;
self.inner
.members_by_path
.par_iter()
.map(move |(path, id)| {
(
path.as_path(),
graph.metadata(id).expect("valid package ID"),
)
})
}
pub fn par_iter_by_name(
&self,
) -> impl ParallelIterator<Item = (&'g str, PackageMetadata<'g>)> + use<'g> {
let graph = self.graph;
self.inner
.members_by_name
.par_iter()
.map(move |(name, id)| {
(name.as_ref(), graph.metadata(id).expect("valid package ID"))
})
}
}
}
#[derive(Clone, Debug)]
pub(super) struct WorkspaceImpl {
pub(super) root: Utf8PathBuf,
pub(super) target_directory: Utf8PathBuf,
pub(super) build_directory: Option<Utf8PathBuf>,
pub(super) metadata_table: JsonValue,
pub(super) members_by_path: BTreeMap<Utf8PathBuf, PackageId>,
pub(super) members_by_name: BTreeMap<Box<str>, PackageId>,
pub(super) default_members: Vec<PackageId>,
#[cfg(feature = "proptest1")]
pub(super) name_list: OnceCell<Vec<Box<str>>>,
}
#[derive(Copy, Clone)]
pub struct PackageMetadata<'g> {
graph: &'g PackageGraph,
inner: &'g PackageMetadataImpl,
}
impl fmt::Debug for PackageMetadata<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PackageMetadata")
.field("package_id", &self.id().repr())
.field("..", &"..")
.finish()
}
}
assert_covariant!(PackageMetadata);
impl<'g> PackageMetadata<'g> {
pub(super) fn new(graph: &'g PackageGraph, inner: &'g PackageMetadataImpl) -> Self {
Self { graph, inner }
}
pub fn id(&self) -> &'g PackageId {
&self.graph.dep_graph[self.inner.package_ix]
}
pub fn graph(&self) -> &'g PackageGraph {
self.graph
}
pub fn to_package_query(&self, direction: DependencyDirection) -> PackageQuery<'g> {
self.graph
.query_from_parts(iter::once(self.inner.package_ix).collect(), direction)
}
pub fn to_package_set(&self) -> PackageSet<'g> {
let included: IxBitSet = iter::once(self.package_ix()).collect();
PackageSet::from_included(self.graph, included)
}
pub fn to_feature_set(&self, features: impl FeatureFilter<'g>) -> FeatureSet<'g> {
self.to_package_set().to_feature_set(features)
}
pub fn direct_links_directed(
&self,
direction: DependencyDirection,
) -> impl Iterator<Item = PackageLink<'g>> + 'g + use<'g> {
self.direct_links_impl(direction.into())
}
pub fn direct_links(&self) -> impl Iterator<Item = PackageLink<'g>> + 'g + use<'g> {
self.direct_links_impl(Outgoing)
}
pub fn reverse_direct_links(&self) -> impl Iterator<Item = PackageLink<'g>> + 'g + use<'g> {
self.direct_links_impl(Incoming)
}
pub fn link_between(
&self,
other: &PackageId,
direction: DependencyDirection,
) -> Result<Option<PackageLink<'g>>, Error> {
self.link_between_impl(other, direction.into())
}
pub fn link_to(&self, to: &PackageId) -> Result<Option<PackageLink<'g>>, Error> {
self.link_between_impl(to, Outgoing)
}
pub fn link_from(&self, from: &PackageId) -> Result<Option<PackageLink<'g>>, Error> {
self.link_between_impl(from, Incoming)
}
pub fn name(&self) -> &'g str {
&self.inner.name
}
pub fn version(&self) -> &'g Version {
&self.inner.version
}
pub fn authors(&self) -> &'g [String] {
&self.inner.authors
}
pub fn description(&self) -> Option<&'g str> {
self.inner.description.as_ref().map(|x| x.as_ref())
}
pub fn license(&self) -> Option<&'g str> {
self.inner.license.as_ref().map(|x| x.as_ref())
}
pub fn license_file(&self) -> Option<&'g Utf8Path> {
self.inner.license_file.as_ref().map(|path| path.as_ref())
}
pub fn source(&self) -> PackageSource<'g> {
PackageSource::new(&self.inner.source)
}
pub fn in_workspace(&self) -> bool {
self.source().is_workspace()
}
pub fn manifest_path(&self) -> &'g Utf8Path {
&self.inner.manifest_path
}
pub fn categories(&self) -> &'g [String] {
&self.inner.categories
}
pub fn keywords(&self) -> &'g [String] {
&self.inner.keywords
}
pub fn readme(&self) -> Option<&'g Utf8Path> {
self.inner.readme.as_ref().map(|path| path.as_ref())
}
pub fn repository(&self) -> Option<&'g str> {
self.inner.repository.as_ref().map(|x| x.as_ref())
}
pub fn homepage(&self) -> Option<&'g str> {
self.inner.homepage.as_ref().map(|x| x.as_ref())
}
pub fn documentation(&self) -> Option<&'g str> {
self.inner.documentation.as_ref().map(|x| x.as_ref())
}
pub fn edition(&self) -> &'g str {
&self.inner.edition
}
pub fn metadata_table(&self) -> &'g JsonValue {
&self.inner.metadata_table
}
pub fn links(&self) -> Option<&'g str> {
self.inner.links.as_ref().map(|x| x.as_ref())
}
pub fn publish(&self) -> PackagePublish<'g> {
PackagePublish::new(&self.inner.publish)
}
pub fn default_run(&self) -> Option<&'g str> {
self.inner.default_run.as_ref().map(|x| x.as_ref())
}
pub fn minimum_rust_version(&self) -> Option<&'g Version> {
self.inner.rust_version.as_ref()
}
#[deprecated(
since = "0.17.1",
note = "use Self::rust_version instead, it returns a Version"
)]
pub fn rust_version(&self) -> Option<&'g VersionReq> {
self.inner.rust_version_req.as_ref()
}
pub fn build_targets(&self) -> impl Iterator<Item = BuildTarget<'g>> + use<'g> {
self.inner.build_targets.iter().map(BuildTarget::new)
}
pub fn build_target(&self, id: &BuildTargetId<'_>) -> Option<BuildTarget<'g>> {
self.inner
.build_targets
.get_key_value(id.as_key())
.map(BuildTarget::new)
}
pub fn is_proc_macro(&self) -> bool {
match self.build_target(&BuildTargetId::Library) {
Some(build_target) => matches!(build_target.kind(), BuildTargetKind::ProcMacro),
None => false,
}
}
pub fn has_build_script(&self) -> bool {
self.build_target(&BuildTargetId::BuildScript).is_some()
}
pub fn has_default_feature(&self) -> bool {
self.inner.has_default_feature
}
pub fn default_feature_id(&self) -> FeatureId<'g> {
if self.inner.has_default_feature {
FeatureId::new(self.id(), FeatureLabel::Named("default"))
} else {
FeatureId::base(self.id())
}
}
pub fn named_features(&self) -> impl Iterator<Item = &'g str> + 'g + use<'g> {
self.named_features_full()
.map(|(_, named_feature, _)| named_feature)
}
#[inline]
pub(super) fn package_ix(&self) -> NodeIndex<PackageIx> {
self.inner.package_ix
}
fn link_between_impl(
&self,
other: &PackageId,
dir: Direction,
) -> Result<Option<PackageLink<'g>>, Error> {
let other_ix = self.graph.package_ix(other)?;
match dir {
Direction::Outgoing => Ok(self.graph.link_between_ixs(self.package_ix(), other_ix)),
Direction::Incoming => Ok(self.graph.link_between_ixs(other_ix, self.package_ix())),
}
}
fn direct_links_impl(
&self,
dir: Direction,
) -> impl Iterator<Item = PackageLink<'g>> + 'g + use<'g> {
self.graph.dep_links_ixs_directed(self.package_ix(), dir)
}
pub(super) fn get_feature_idx(&self, label: FeatureLabel<'_>) -> Option<FeatureIndexInPackage> {
match label {
FeatureLabel::Base => Some(FeatureIndexInPackage::Base),
FeatureLabel::OptionalDependency(dep_name) => self
.inner
.optional_deps
.get_index_of(dep_name)
.map(FeatureIndexInPackage::OptionalDependency),
FeatureLabel::Named(feature_name) => self
.inner
.named_features
.get_index_of(feature_name)
.map(FeatureIndexInPackage::Named),
}
}
pub(super) fn feature_idx_to_label(&self, idx: FeatureIndexInPackage) -> FeatureLabel<'g> {
match idx {
FeatureIndexInPackage::Base => FeatureLabel::Base,
FeatureIndexInPackage::OptionalDependency(idx) => FeatureLabel::OptionalDependency(
self.inner
.optional_deps
.get_index(idx)
.expect("feature idx in optional_deps should be valid")
.as_ref(),
),
FeatureIndexInPackage::Named(idx) => FeatureLabel::Named(
self.inner
.named_features
.get_index(idx)
.expect("feature idx in optional_deps should be valid")
.0
.as_ref(),
),
}
}
#[allow(dead_code)]
pub(super) fn all_feature_nodes(&self) -> impl Iterator<Item = FeatureNode> + 'g + use<'g> {
let package_ix = self.package_ix();
iter::once(FeatureNode::new(
self.package_ix(),
FeatureIndexInPackage::Base,
))
.chain(
(0..self.inner.named_features.len())
.map(move |named_idx| FeatureNode::named_feature(package_ix, named_idx)),
)
.chain(
(0..self.inner.optional_deps.len())
.map(move |dep_idx| FeatureNode::optional_dep(package_ix, dep_idx)),
)
}
pub(super) fn named_features_full(
&self,
) -> impl Iterator<Item = (FeatureIndexInPackage, &'g str, &'g [NamedFeatureDep])> + 'g + use<'g>
{
self.inner
.named_features
.iter()
.enumerate()
.map(|(idx, (feature, deps))| {
(
FeatureIndexInPackage::Named(idx),
feature.as_ref(),
deps.as_slice(),
)
})
}
pub(super) fn optional_deps_full(
&self,
) -> impl Iterator<Item = (FeatureIndexInPackage, &'g str)> + 'g + use<'g> {
self.inner
.optional_deps
.iter()
.enumerate()
.map(|(idx, dep_name)| {
(
FeatureIndexInPackage::OptionalDependency(idx),
dep_name.as_ref(),
)
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) enum FeatureIndexInPackage {
Base,
OptionalDependency(usize),
Named(usize),
}
impl PartialEq for PackageMetadata<'_> {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.graph, other.graph) && self.package_ix() == other.package_ix()
}
}
impl Eq for PackageMetadata<'_> {}
#[derive(Clone, Debug)]
pub(crate) struct PackageMetadataImpl {
pub(super) name: Box<str>,
pub(super) version: Version,
pub(super) authors: Vec<String>,
pub(super) description: Option<Box<str>>,
pub(super) license: Option<Box<str>>,
pub(super) license_file: Option<Box<Utf8Path>>,
pub(super) manifest_path: Box<Utf8Path>,
pub(super) categories: Vec<String>,
pub(super) keywords: Vec<String>,
pub(super) readme: Option<Box<Utf8Path>>,
pub(super) repository: Option<Box<str>>,
pub(super) homepage: Option<Box<str>>,
pub(super) documentation: Option<Box<str>>,
pub(super) edition: Box<str>,
pub(super) metadata_table: JsonValue,
pub(super) links: Option<Box<str>>,
pub(super) publish: PackagePublishImpl,
pub(super) default_run: Option<Box<str>>,
pub(super) rust_version: Option<Version>,
pub(super) rust_version_req: Option<VersionReq>,
pub(super) named_features: IndexMap<Box<str>, SmallVec<[NamedFeatureDep; 4]>>,
pub(super) optional_deps: IndexSet<Box<str>>,
pub(super) package_ix: NodeIndex<PackageIx>,
pub(super) source: PackageSourceImpl,
pub(super) build_targets: BTreeMap<OwnedBuildTargetId, BuildTargetImpl>,
pub(super) has_default_feature: bool,
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum PackageSource<'g> {
Workspace(&'g Utf8Path),
Path(&'g Utf8Path),
External(&'g str),
}
assert_covariant!(PackageSource);
impl<'g> PackageSource<'g> {
pub const CRATES_IO_REGISTRY: &'static str =
"registry+https://github.com/rust-lang/crates.io-index";
pub(super) fn new(inner: &'g PackageSourceImpl) -> Self {
match inner {
PackageSourceImpl::Workspace(path) => PackageSource::Workspace(path),
PackageSourceImpl::Path(path) => PackageSource::Path(path),
PackageSourceImpl::CratesIo => PackageSource::External(Self::CRATES_IO_REGISTRY),
PackageSourceImpl::External(source) => PackageSource::External(source),
}
}
pub fn is_workspace(&self) -> bool {
matches!(self, PackageSource::Workspace(_))
}
pub fn is_path(&self) -> bool {
matches!(self, PackageSource::Path(_))
}
pub fn is_external(&self) -> bool {
matches!(self, PackageSource::External(_))
}
pub fn is_crates_io(&self) -> bool {
matches!(self, PackageSource::External(Self::CRATES_IO_REGISTRY))
}
pub fn is_local(&self) -> bool {
!self.is_external()
}
pub fn workspace_path(&self) -> Option<&'g Utf8Path> {
match self {
PackageSource::Workspace(path) => Some(path),
_ => None,
}
}
pub fn local_path(&self) -> Option<&'g Utf8Path> {
match self {
PackageSource::Path(path) | PackageSource::Workspace(path) => Some(path),
_ => None,
}
}
pub fn external_source(&self) -> Option<&'g str> {
match self {
PackageSource::External(source) => Some(source),
_ => None,
}
}
pub fn parse_external(&self) -> Option<ExternalSource<'g>> {
match self {
PackageSource::External(source) => ExternalSource::new(source),
_ => None,
}
}
}
impl fmt::Display for PackageSource<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PackageSource::Workspace(path) => write!(f, "{path}"),
PackageSource::Path(path) => write!(f, "{path}"),
PackageSource::External(source) => write!(f, "{source}"),
}
}
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum ExternalSource<'g> {
Registry(&'g str),
Sparse(&'g str),
Git {
repository: &'g str,
req: GitReq<'g>,
resolved: &'g str,
},
}
impl<'g> ExternalSource<'g> {
pub const REGISTRY_PLUS: &'static str = "registry+";
pub const SPARSE_PLUS: &'static str = "sparse+";
pub const GIT_PLUS: &'static str = "git+";
pub const BRANCH_EQ: &'static str = "?branch=";
pub const TAG_EQ: &'static str = "?tag=";
pub const REV_EQ: &'static str = "?rev=";
pub const CRATES_IO_URL: &'static str = "https://github.com/rust-lang/crates.io-index";
pub fn new(source: &'g str) -> Option<Self> {
if let Some(registry) = source.strip_prefix(Self::REGISTRY_PLUS) {
Some(ExternalSource::Registry(registry))
} else if let Some(sparse) = source.strip_prefix(Self::SPARSE_PLUS) {
Some(ExternalSource::Sparse(sparse))
} else if let Some(rest) = source.strip_prefix(Self::GIT_PLUS) {
let (rest, resolved) = rest.rsplit_once('#')?;
let (repository, req) = if let Some(idx) = rest.find(Self::BRANCH_EQ) {
(
&rest[..idx],
GitReq::Branch(&rest[idx + Self::BRANCH_EQ.len()..]),
)
} else if let Some(idx) = rest.find(Self::TAG_EQ) {
(&rest[..idx], GitReq::Tag(&rest[idx + Self::TAG_EQ.len()..]))
} else if let Some(idx) = rest.find(Self::REV_EQ) {
(&rest[..idx], GitReq::Rev(&rest[idx + Self::TAG_EQ.len()..]))
} else {
(rest, GitReq::Default)
};
Some(ExternalSource::Git {
repository,
req,
resolved,
})
} else {
None
}
}
}
impl fmt::Display for ExternalSource<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ExternalSource::Registry(url) => write!(f, "{}{}", Self::REGISTRY_PLUS, url),
ExternalSource::Sparse(url) => write!(f, "{}{}", Self::SPARSE_PLUS, url),
ExternalSource::Git {
repository,
req,
resolved,
} => {
write!(f, "{}{}", Self::GIT_PLUS, repository)?;
match req {
GitReq::Branch(branch) => write!(f, "{}{}", Self::BRANCH_EQ, branch)?,
GitReq::Tag(tag) => write!(f, "{}{}", Self::TAG_EQ, tag)?,
GitReq::Rev(rev) => write!(f, "{}{}", Self::REV_EQ, rev)?,
GitReq::Default => {}
};
write!(f, "#{resolved}")
}
}
}
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum GitReq<'g> {
Branch(&'g str),
Tag(&'g str),
Rev(&'g str),
Default,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum PackageSourceImpl {
Workspace(Box<Utf8Path>),
Path(Box<Utf8Path>),
CratesIo,
External(Box<str>),
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum PackagePublish<'g> {
Unrestricted,
Registries(&'g [String]),
}
assert_covariant!(PackagePublish);
impl<'g> PackagePublish<'g> {
pub(super) fn new(inner: &'g PackagePublishImpl) -> Self {
match inner {
PackagePublishImpl::Unrestricted => PackagePublish::Unrestricted,
PackagePublishImpl::Registries(registries) => PackagePublish::Registries(registries),
}
}
pub const CRATES_IO: &'static str = "crates-io";
pub fn is_unrestricted(&self) -> bool {
matches!(self, PackagePublish::Unrestricted)
}
pub fn can_publish_to(&self, registry: impl AsRef<str>) -> bool {
let registry = registry.as_ref();
match self {
PackagePublish::Unrestricted => true,
PackagePublish::Registries(registries) => registries.iter().any(|r| r == registry),
}
}
pub fn can_publish_to_crates_io(&self) -> bool {
self.can_publish_to(Self::CRATES_IO)
}
pub fn is_never(&self) -> bool {
match self {
PackagePublish::Unrestricted => false,
PackagePublish::Registries(registries) => registries.is_empty(),
}
}
}
#[derive(Clone, Debug)]
pub(super) enum PackagePublishImpl {
Unrestricted,
Registries(Box<[String]>),
}
#[derive(Copy, Clone, Debug)]
pub struct PackageLink<'g> {
graph: &'g PackageGraph,
from: &'g PackageMetadataImpl,
to: &'g PackageMetadataImpl,
edge_ix: EdgeIndex<PackageIx>,
inner: &'g PackageLinkImpl,
}
assert_covariant!(PackageLink);
impl<'g> PackageLink<'g> {
pub(super) fn new(
graph: &'g PackageGraph,
source_ix: NodeIndex<PackageIx>,
target_ix: NodeIndex<PackageIx>,
edge_ix: EdgeIndex<PackageIx>,
inner: Option<&'g PackageLinkImpl>,
) -> Self {
let from = graph
.data
.metadata_impl(&graph.dep_graph[source_ix])
.expect("'from' should have associated metadata");
let to = graph
.data
.metadata_impl(&graph.dep_graph[target_ix])
.expect("'to' should have associated metadata");
Self {
graph,
from,
to,
edge_ix,
inner: inner.unwrap_or_else(|| &graph.dep_graph[edge_ix]),
}
}
pub fn from(&self) -> PackageMetadata<'g> {
PackageMetadata::new(self.graph, self.from)
}
pub fn to(&self) -> PackageMetadata<'g> {
PackageMetadata::new(self.graph, self.to)
}
pub fn endpoints(&self) -> (PackageMetadata<'g>, PackageMetadata<'g>) {
(self.from(), self.to())
}
pub fn dep_name(&self) -> &'g str {
&self.inner.dep_name
}
pub fn resolved_name(&self) -> &'g str {
&self.inner.resolved_name
}
pub fn version_req(&self) -> &'g VersionReq {
&self.inner.version_req
}
pub fn registry(&self) -> Option<&'g str> {
self.inner.registry.as_deref()
}
pub fn path(&self) -> Option<&'g Utf8Path> {
self.inner.path.as_deref()
}
pub fn normal(&self) -> DependencyReq<'g> {
DependencyReq {
inner: &self.inner.normal,
}
}
pub fn build(&self) -> DependencyReq<'g> {
DependencyReq {
inner: &self.inner.build,
}
}
pub fn dev(&self) -> DependencyReq<'g> {
DependencyReq {
inner: &self.inner.dev,
}
}
pub fn req_for_kind(&self, kind: DependencyKind) -> DependencyReq<'g> {
match kind {
DependencyKind::Normal => self.normal(),
DependencyKind::Development => self.dev(),
DependencyKind::Build => self.build(),
}
}
pub fn dev_only(&self) -> bool {
self.inner.dev_only()
}
#[allow(dead_code)]
pub(super) fn edge_ix(&self) -> EdgeIndex<PackageIx> {
self.edge_ix
}
#[doc(hidden)]
pub fn as_inner_ptrs(&self) -> PackageLinkPtrs {
PackageLinkPtrs {
from: self.from,
to: self.to,
inner: self.inner,
}
}
}
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[doc(hidden)]
pub struct PackageLinkPtrs {
from: *const PackageMetadataImpl,
to: *const PackageMetadataImpl,
inner: *const PackageLinkImpl,
}
#[derive(Clone, Debug)]
pub(crate) struct PackageLinkImpl {
pub(super) dep_name: String,
pub(super) resolved_name: String,
pub(super) version_req: VersionReq,
pub(super) registry: Option<String>,
pub(super) path: Option<Utf8PathBuf>,
pub(super) normal: DependencyReqImpl,
pub(super) build: DependencyReqImpl,
pub(super) dev: DependencyReqImpl,
}
impl PackageLinkImpl {
#[inline]
fn dev_only(&self) -> bool {
self.normal.enabled().is_never() && self.build.enabled().is_never()
}
}
#[derive(Clone, Debug)]
pub struct DependencyReq<'g> {
pub(super) inner: &'g DependencyReqImpl,
}
impl<'g> DependencyReq<'g> {
pub fn is_present(&self) -> bool {
!self.inner.enabled().is_never()
}
pub fn status(&self) -> EnabledStatus<'g> {
self.inner.enabled()
}
pub fn default_features(&self) -> EnabledStatus<'g> {
self.inner.default_features()
}
pub fn no_default_features(&self) -> EnabledStatus<'g> {
self.inner.no_default_features()
}
pub fn features(&self) -> impl Iterator<Item = &'g str> + use<'g> {
self.inner.all_features()
}
pub fn feature_status(&self, feature: &str) -> EnabledStatus<'g> {
self.inner.feature_status(feature)
}
}
#[derive(Copy, Clone, Debug)]
pub struct EnabledStatus<'g> {
required: PlatformStatus<'g>,
optional: PlatformStatus<'g>,
}
assert_covariant!(EnabledStatus);
impl<'g> EnabledStatus<'g> {
pub(super) fn new(required: &'g PlatformStatusImpl, optional: &'g PlatformStatusImpl) -> Self {
Self {
required: PlatformStatus::new(required),
optional: PlatformStatus::new(optional),
}
}
pub fn is_never(&self) -> bool {
self.required.is_never() && self.optional.is_never()
}
pub fn required_on(&self, platform_spec: &PlatformSpec) -> EnabledTernary {
self.required.enabled_on(platform_spec)
}
pub fn enabled_on(&self, platform_spec: &PlatformSpec) -> EnabledTernary {
let required = self.required.enabled_on(platform_spec);
let optional = self.optional.enabled_on(platform_spec);
required | optional
}
pub fn required_status(&self) -> PlatformStatus<'g> {
self.required
}
pub fn optional_status(&self) -> PlatformStatus<'g> {
self.optional
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(super) enum NamedFeatureDep {
NamedFeature(Box<str>),
OptionalDependency(Box<str>),
DependencyNamedFeature {
dep_name: Box<str>,
feature: Box<str>,
weak: bool,
},
}
impl NamedFeatureDep {
#[inline]
pub(super) fn named_feature(feature_name: impl Into<String>) -> Self {
Self::NamedFeature(feature_name.into().into_boxed_str())
}
#[inline]
pub(super) fn optional_dependency(dep_name: impl Into<String>) -> Self {
Self::OptionalDependency(dep_name.into().into_boxed_str())
}
#[inline]
pub(super) fn dep_named_feature(
dep_name: impl Into<String>,
feature: impl Into<String>,
weak: bool,
) -> Self {
Self::DependencyNamedFeature {
dep_name: dep_name.into().into_boxed_str(),
feature: feature.into().into_boxed_str(),
weak,
}
}
}
impl fmt::Display for NamedFeatureDep {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::NamedFeature(feature) => write!(f, "{feature}"),
Self::OptionalDependency(dep_name) => write!(f, "dep:{dep_name}"),
Self::DependencyNamedFeature {
dep_name,
feature,
weak,
} => {
write!(
f,
"{}{}/{}",
dep_name,
if *weak { "?" } else { "" },
feature
)
}
}
}
}
#[derive(Clone, Debug, Default)]
pub(super) struct DependencyReqImpl {
pub(super) required: DepRequiredOrOptional,
pub(super) optional: DepRequiredOrOptional,
}
impl DependencyReqImpl {
fn all_features(&self) -> impl Iterator<Item = &str> {
self.required
.all_features()
.chain(self.optional.all_features())
}
pub(super) fn enabled(&self) -> EnabledStatus<'_> {
self.make_status(|req_impl| &req_impl.build_if)
}
pub(super) fn default_features(&self) -> EnabledStatus<'_> {
self.make_status(|req_impl| &req_impl.default_features_if)
}
pub(super) fn no_default_features(&self) -> EnabledStatus<'_> {
self.make_status(|req_impl| &req_impl.no_default_features_if)
}
pub(super) fn feature_status(&self, feature: &str) -> EnabledStatus<'_> {
static DEFAULT_STATUS: PlatformStatusImpl = PlatformStatusImpl::Specs(Vec::new());
self.make_status(|req_impl| {
req_impl
.feature_targets
.get(feature)
.unwrap_or(&DEFAULT_STATUS)
})
}
fn make_status(
&self,
pred_fn: impl Fn(&DepRequiredOrOptional) -> &PlatformStatusImpl,
) -> EnabledStatus<'_> {
EnabledStatus::new(pred_fn(&self.required), pred_fn(&self.optional))
}
}
#[derive(Clone, Debug, Default)]
pub(super) struct DepRequiredOrOptional {
pub(super) build_if: PlatformStatusImpl,
pub(super) default_features_if: PlatformStatusImpl,
pub(super) no_default_features_if: PlatformStatusImpl,
pub(super) feature_targets: BTreeMap<String, PlatformStatusImpl>,
}
impl DepRequiredOrOptional {
pub(super) fn all_features(&self) -> impl Iterator<Item = &str> {
self.feature_targets.keys().map(|s| s.as_str())
}
}