Skip to main content

pedant_core/resolution/rust/
dependency.rs

1//! The dependency view: one declared Cargo edge and how it activates.
2
3use std::sync::Arc;
4
5use super::identity::{PackageId, TargetId};
6
7/// Which Cargo dependency table declared an edge.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum CargoDependencyKind {
10    /// A `[dependencies]` edge.
11    Normal,
12    /// A `[dev-dependencies]` edge.
13    Development,
14    /// A `[build-dependencies]` edge.
15    Build,
16}
17
18/// Whether an edge is unconditional, or carries a predicate this model records
19/// without evaluating.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub enum DependencyActivation {
22    /// The edge is active in every configuration.
23    Always,
24    /// The edge is active only under the recorded, unevaluated predicate.
25    Conditional(Arc<str>),
26}
27
28/// One dependency edge declared by a package inside the project.
29#[derive(Debug)]
30pub struct RustDependency {
31    pub(super) source: PackageId,
32    pub(super) name: Arc<str>,
33    pub(super) package_name: Arc<str>,
34    pub(super) kind: CargoDependencyKind,
35    pub(super) activation: DependencyActivation,
36    pub(super) package: Option<PackageId>,
37    pub(super) library: Option<TargetId>,
38}
39
40impl RustDependency {
41    /// The package that declares this edge.
42    pub fn source(&self) -> PackageId {
43        self.source
44    }
45
46    /// The namespace-local dependency name, which a rename may change.
47    pub fn name(&self) -> &str {
48        &self.name
49    }
50
51    /// The depended-on package's real Cargo name.
52    pub fn package_name(&self) -> &str {
53        &self.package_name
54    }
55
56    /// Which dependency table declared this edge.
57    pub fn kind(&self) -> CargoDependencyKind {
58        self.kind
59    }
60
61    /// Whether this edge is always active, and under which predicate otherwise.
62    pub fn activation(&self) -> &DependencyActivation {
63        &self.activation
64    }
65
66    /// The in-repository package this edge selects, when it resolves to one.
67    pub fn package(&self) -> Option<PackageId> {
68        self.package
69    }
70
71    /// The in-repository library target this edge selects, when one exists.
72    pub fn library(&self) -> Option<TargetId> {
73        self.library
74    }
75}