1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Entry point for analyzing Cargo dependency graphs.
//!
//! The main entry point for analyzing graphs is [`PackageGraph`](struct.PackageGraph.html). See its
//! documentation for more details.

use crate::PackageId;
use cargo_metadata::DependencyKind;
use petgraph::prelude::*;
use std::fmt;

mod build;
mod cycles;
#[doc(hidden)]
pub mod feature;
mod graph_impl;
#[cfg(feature = "proptest09")]
mod proptest09;
mod query;
mod query_core;
mod resolve;
mod resolve_core;

pub use crate::petgraph_support::dot::DotWrite;
pub use cycles::*;
pub use graph_impl::*;
use once_cell::sync::Lazy;
use petgraph::graph::IndexType;
#[cfg(feature = "proptest09")]
pub use proptest09::*;
pub use query::*;
pub use resolve::*;
use semver::{Version, VersionReq};

/// The direction in which to follow dependencies.
///
/// Used by the `_directed` methods.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "proptest09", derive(proptest_derive::Arbitrary))]
pub enum DependencyDirection {
    /// Dependencies from this package to other packages.
    Forward,
    /// Reverse dependencies from other packages to this one.
    Reverse,
}

impl DependencyDirection {
    /// Returns the opposite direction to this one.
    pub fn opposite(self) -> Self {
        match self {
            DependencyDirection::Forward => DependencyDirection::Reverse,
            DependencyDirection::Reverse => DependencyDirection::Forward,
        }
    }
}

impl From<Direction> for DependencyDirection {
    fn from(direction: Direction) -> Self {
        match direction {
            Direction::Outgoing => DependencyDirection::Forward,
            Direction::Incoming => DependencyDirection::Reverse,
        }
    }
}

impl From<DependencyDirection> for Direction {
    fn from(direction: DependencyDirection) -> Self {
        match direction {
            DependencyDirection::Forward => Direction::Outgoing,
            DependencyDirection::Reverse => Direction::Incoming,
        }
    }
}

/// Index for PackageGraph. Used for newtype wrapping.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct PackageIx(u32);

/// Index for FeatureGraph. Used for newtype wrapping.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct FeatureIx(u32);

macro_rules! graph_ix {
    ($ix_type: ident) => {
        impl fmt::Display for $ix_type {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "{}", self.0)
            }
        }

        // From the docs for `IndexType`:
        //
        // > Marked `unsafe` because: the trait must faithfully preseve and convert index values.
        unsafe impl IndexType for $ix_type {
            #[inline(always)]
            fn new(x: usize) -> Self {
                $ix_type(x as u32)
            }
            #[inline(always)]
            fn index(&self) -> usize {
                self.0 as usize
            }
            #[inline(always)]
            fn max() -> Self {
                $ix_type(::std::u32::MAX)
            }
        }
    };
}

graph_ix!(PackageIx);
graph_ix!(FeatureIx);

/// Used to group together associated types with a particular graph.
trait GraphSpec {
    type Node;
    type Edge;
    type Ix: IndexType;
}

impl GraphSpec for PackageGraph {
    type Node = PackageId;
    type Edge = PackageEdge;
    type Ix = PackageIx;
}

impl<'g> GraphSpec for feature::FeatureGraph<'g> {
    type Node = feature::FeatureNode;
    type Edge = feature::FeatureEdge;
    type Ix = FeatureIx;
}

pub(crate) fn kind_str(kind: DependencyKind) -> &'static str {
    match kind {
        DependencyKind::Normal => "normal",
        DependencyKind::Build => "build",
        DependencyKind::Development => "dev",
        _ => "unknown",
    }
}

// A requirement of "*" filters out pre-release versions with the semver crate,
// but cargo accepts them.
// See https://github.com/steveklabnik/semver/issues/98.
fn cargo_version_matches(req: &VersionReq, version: &Version) -> bool {
    static MAJOR_WILDCARD: Lazy<VersionReq> = Lazy::new(|| VersionReq::parse("*").unwrap());

    req == &*MAJOR_WILDCARD || req.matches(version)
}