Skip to main content

tuwunel_core/info/
cargo.rs

1//! Cargo metadata captured for the running build.
2//!
3//! Build-time macros embed the workspace manifest and selected crate manifests.
4//! This module processes that data lazily for runtime queries about project
5//! features and dependencies.
6
7#[cfg(test)]
8mod tests;
9
10use std::{collections::BTreeMap, sync::OnceLock};
11
12use semver::VersionReq;
13use serde::{Deserialize, de::IgnoredAny};
14use smallstr::SmallString;
15use toml::from_str;
16use tuwunel_macros::cargo_manifest;
17
18use crate::{Result, implement};
19
20type Dependencies = BTreeMap<Name, Dependency>;
21type Features = BTreeMap<String, Vec<IgnoredAny>>;
22type Name = SmallString<[u8; 16]>;
23
24/// The `[features]` table of a crate manifest.
25///
26/// Every other table is ignored, and a manifest without one has no features.
27#[derive(Deserialize)]
28struct Manifest {
29	#[serde(default)]
30	features: Features,
31}
32
33/// The `[workspace.dependencies]` table of the workspace manifest.
34///
35/// A manifest without a `[workspace]` table fails to parse.
36#[derive(Deserialize)]
37struct WorkspaceManifest {
38	workspace: Workspace,
39}
40
41#[derive(Deserialize)]
42struct Workspace {
43	#[serde(default)]
44	dependencies: Dependencies,
45}
46
47/// One `[workspace.dependencies]` entry, a bare version or a table.
48///
49/// A table without a version, such as a git or path dependency, requires any
50/// version (`*`).
51#[derive(Deserialize)]
52#[serde(untagged)]
53enum Dependency {
54	Simple(VersionReq),
55	Detailed {
56		#[serde(default)]
57		version: VersionReq,
58
59		#[serde(default)]
60		features: Vec<Name>,
61	},
62}
63
64// Raw captures of the cargo manifest for each crate. This is provided by a
65// proc-macro at build time since the source directory and the cargo toml's may
66// not be present during execution.
67
68#[cargo_manifest]
69const WORKSPACE_MANIFEST: &'static str = ();
70#[cargo_manifest(crate = "macros")]
71const MACROS_MANIFEST: &'static str = ();
72#[cargo_manifest(crate = "core")]
73const CORE_MANIFEST: &'static str = ();
74#[cargo_manifest(crate = "database")]
75const DATABASE_MANIFEST: &'static str = ();
76#[cargo_manifest(crate = "service")]
77const SERVICE_MANIFEST: &'static str = ();
78#[cargo_manifest(crate = "admin")]
79const ADMIN_MANIFEST: &'static str = ();
80#[cargo_manifest(crate = "router")]
81const ROUTER_MANIFEST: &'static str = ();
82#[cargo_manifest(crate = "main")]
83const MAIN_MANIFEST: &'static str = ();
84
85/// Processed list of features across all project crates. This is generated from
86/// the data in the MANIFEST strings and contains all possible project features.
87/// For *enabled* features see the info::rustc module instead.
88static FEATURES: OnceLock<Vec<String>> = OnceLock::new();
89
90/// Processed list of dependencies. This is generated from the data captured in
91/// the MANIFEST.
92static DEPENDENCIES: OnceLock<Dependencies> = OnceLock::new();
93
94/// Lists dependency names declared by the workspace manifest.
95///
96/// Names borrow from the lazily parsed dependencies, in name order.
97///
98/// # Panics
99///
100/// Panics when the embedded workspace manifest is invalid or lacks its
101/// workspace section.
102pub fn dependencies_names() -> impl Iterator<Item = &'static str> {
103	dependencies().map(|(name, ..)| name)
104}
105
106/// Returns dependencies declared by the embedded workspace manifest.
107///
108/// Each item is a name, its version requirement and the features it enables,
109/// in name order. The manifest is parsed once on first access and retained for
110/// the process lifetime. Package-specific dependency tables are not merged here.
111///
112/// # Panics
113///
114/// Panics when the manifest is invalid or lacks its workspace section.
115pub fn dependencies() -> impl Iterator<Item = (&'static str, &'static VersionReq, &'static [Name])>
116{
117	DEPENDENCIES
118		.get_or_init(|| {
119			init_dependencies()
120				.unwrap_or_else(|e| panic!("Failed to initialize dependencies: {e}"))
121		})
122		.iter()
123		.map(|(name, dependency)| {
124			(name.as_str(), dependency.version(), dependency.enabled_features())
125		})
126}
127
128#[implement(Dependency)]
129#[inline]
130fn version(&self) -> &VersionReq {
131	match self {
132		| Self::Simple(version) | Self::Detailed { version, .. } => version,
133	}
134}
135
136#[implement(Dependency)]
137#[inline]
138fn enabled_features(&self) -> &[Name] {
139	match self {
140		| Self::Simple(_) => &[],
141		| Self::Detailed { features, .. } => features,
142	}
143}
144
145/// List of all possible features for the project. For *enabled* features in
146/// this build see the companion function in info::rustc.
147pub fn features() -> &'static Vec<String> {
148	FEATURES.get_or_init(|| {
149		init_features().unwrap_or_else(|e| panic!("Failed initialize features: {e}"))
150	})
151}
152
153fn init_features() -> Result<Vec<String>> {
154	let mut features = Vec::new();
155	append_features(&mut features, WORKSPACE_MANIFEST)?;
156	append_features(&mut features, MACROS_MANIFEST)?;
157	append_features(&mut features, CORE_MANIFEST)?;
158	append_features(&mut features, DATABASE_MANIFEST)?;
159	append_features(&mut features, SERVICE_MANIFEST)?;
160	append_features(&mut features, ADMIN_MANIFEST)?;
161	append_features(&mut features, ROUTER_MANIFEST)?;
162	append_features(&mut features, MAIN_MANIFEST)?;
163	features.sort();
164	features.dedup();
165
166	Ok(features)
167}
168
169fn append_features(features: &mut Vec<String>, manifest: &str) -> Result {
170	let manifest: Manifest = from_str(manifest)?;
171
172	features.extend(manifest.features.into_keys());
173
174	Ok(())
175}
176
177fn init_dependencies() -> Result<Dependencies> {
178	let manifest: WorkspaceManifest = from_str(WORKSPACE_MANIFEST)?;
179
180	Ok(manifest.workspace.dependencies)
181}