Skip to main content

pedant_core/resolution/rust/
target.rs

1//! The target view: one compilation entry point declared by, or discovered
2//! for, a package.
3
4use std::sync::Arc;
5
6use super::edition::CargoEdition;
7use super::identity::{PackageId, TargetId};
8
9/// The kind of Cargo target an entry point compiles into.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub enum CargoTargetKind {
12    /// The package's single library target.
13    Library,
14    /// A binary target.
15    Binary,
16    /// An example target.
17    Example,
18    /// An integration-test target.
19    Test,
20    /// A benchmark target.
21    Benchmark,
22    /// The package's build script.
23    BuildScript,
24}
25
26impl CargoTargetKind {
27    /// The stable token this kind takes inside a resolution unit key.
28    pub(super) fn token(self) -> &'static str {
29        match self {
30            Self::Library => "lib",
31            Self::Binary => "bin",
32            Self::Example => "example",
33            Self::Test => "test",
34            Self::Benchmark => "bench",
35            Self::BuildScript => "build-script",
36        }
37    }
38}
39
40/// One Cargo target and the source file it enters through.
41#[derive(Debug)]
42pub struct RustTarget {
43    pub(super) id: TargetId,
44    pub(super) package: PackageId,
45    pub(super) name: Arc<str>,
46    pub(super) kind: CargoTargetKind,
47    pub(super) entry_path: Arc<str>,
48    pub(super) edition: CargoEdition,
49}
50
51impl RustTarget {
52    /// This target's project-scoped identity.
53    pub fn id(&self) -> TargetId {
54        self.id
55    }
56
57    /// The package that declares this target.
58    pub fn package(&self) -> PackageId {
59        self.package
60    }
61
62    /// The Cargo target name.
63    pub fn name(&self) -> &str {
64        &self.name
65    }
66
67    /// Which kind of target this is.
68    pub fn kind(&self) -> CargoTargetKind {
69        self.kind
70    }
71
72    /// The repository-relative, `/`-separated entry-point path.
73    pub fn entry_path(&self) -> &str {
74        &self.entry_path
75    }
76
77    /// The edition this target inherits from its package.
78    pub fn edition(&self) -> CargoEdition {
79        self.edition
80    }
81}