From b16514de3b794a05c0c95887e90d18b330ce3882 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 2 Jan 2025 14:52:57 -0600 Subject: [PATCH 1/3] refactor(manifest): Fully qualify manifest env vars This simplifies the macro and makes the content searchable --- src/cargo/core/manifest.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 88845ff26e0..726091d55b6 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -172,23 +172,21 @@ macro_rules! metadata_envs { pub fn should_track(key: &str) -> bool { let keys = [$($key),*]; - key.strip_prefix("CARGO_PKG_") - .map(|key| keys.iter().any(|k| *k == key)) - .unwrap_or_default() + keys.iter().any(|k| *k == key) } pub fn var<'a>(meta: &'a ManifestMetadata, key: &str) -> Option> { - key.strip_prefix("CARGO_PKG_").and_then(|key| match key { + match key { $($key => Some(Self::$field(meta)),)* _ => None, - }) + } } pub fn vars(meta: &ManifestMetadata) -> impl Iterator)> { [ $( ( - concat!("CARGO_PKG_", $key), + $key, Self::$field(meta), ), )* @@ -202,14 +200,14 @@ macro_rules! metadata_envs { // If these change we need to trigger a rebuild. // NOTE: The env var name will be prefixed with `CARGO_PKG_` metadata_envs! { - (description, "DESCRIPTION"), - (homepage, "HOMEPAGE"), - (repository, "REPOSITORY"), - (license, "LICENSE"), - (license_file, "LICENSE_FILE"), - (authors, "AUTHORS", |m: &ManifestMetadata| m.authors.join(":")), - (rust_version, "RUST_VERSION", |m: &ManifestMetadata| m.rust_version.as_ref().map(ToString::to_string).unwrap_or_default()), - (readme, "README"), + (description, "CARGO_PKG_DESCRIPTION"), + (homepage, "CARGO_PKG_HOMEPAGE"), + (repository, "CARGO_PKG_REPOSITORY"), + (license, "CARGO_PKG_LICENSE"), + (license_file, "CARGO_PKG_LICENSE_FILE"), + (authors, "CARGO_PKG_AUTHORS", |m: &ManifestMetadata| m.authors.join(":")), + (rust_version, "CARGO_PKG_RUST_VERSION", |m: &ManifestMetadata| m.rust_version.as_ref().map(ToString::to_string).unwrap_or_default()), + (readme, "CARGO_PKG_README"), } impl ManifestMetadata { From 711d6bef36ceec9ac7e7f7f953ee4bc16821b266 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 2 Jan 2025 15:03:11 -0600 Subject: [PATCH 2/3] refactor(manifest): Make env macro as minimal as possible Macros add a lot of code complexity. This tries to reduce it by making the macro do the bare minimum possible. This does cause some extra branching (unless its compiled out) but that shouldn't be prohibitive. --- src/cargo/core/manifest.rs | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 726091d55b6..b1a54a8e43f 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -156,42 +156,25 @@ macro_rules! get_metadata_env { }; } +struct MetadataEnvs; + macro_rules! metadata_envs { ( $( ($field:ident, $key:literal$(, $to_var:expr)?), )* ) => { - struct MetadataEnvs; impl MetadataEnvs { - $( - fn $field(meta: &ManifestMetadata) -> Cow<'_, str> { - get_metadata_env!(meta, $field$(, $to_var)?) - } - )* - - pub fn should_track(key: &str) -> bool { - let keys = [$($key),*]; - keys.iter().any(|k| *k == key) + fn keys() -> &'static [&'static str] { + &[$($key),*] } - pub fn var<'a>(meta: &'a ManifestMetadata, key: &str) -> Option> { + fn var<'a>(meta: &'a ManifestMetadata, key: &str) -> Option> { match key { - $($key => Some(Self::$field(meta)),)* + $($key => Some(get_metadata_env!(meta, $field$(, $to_var)?)),)* _ => None, } } - - pub fn vars(meta: &ManifestMetadata) -> impl Iterator)> { - [ - $( - ( - $key, - Self::$field(meta), - ), - )* - ].into_iter() - } } } } @@ -213,7 +196,8 @@ metadata_envs! { impl ManifestMetadata { /// Whether the given env var should be tracked by Cargo's dep-info. pub fn should_track(env_key: &str) -> bool { - MetadataEnvs::should_track(env_key) + let keys = MetadataEnvs::keys(); + keys.iter().any(|k| *k == env_key) } pub fn env_var<'a>(&'a self, env_key: &str) -> Option> { @@ -221,7 +205,9 @@ impl ManifestMetadata { } pub fn env_vars(&self) -> impl Iterator)> { - MetadataEnvs::vars(self) + MetadataEnvs::keys() + .iter() + .map(|k| (*k, MetadataEnvs::var(self, k).unwrap())) } } From 0414bb5ac1bc5bf55e79c58370da984b91184ba6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 2 Jan 2025 15:06:17 -0600 Subject: [PATCH 3/3] refactor(manifest): Keep struct/impl close for easier viewing --- src/cargo/core/manifest.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index b1a54a8e43f..0273d8fa4d3 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -147,6 +147,24 @@ pub struct ManifestMetadata { pub rust_version: Option, } +impl ManifestMetadata { + /// Whether the given env var should be tracked by Cargo's dep-info. + pub fn should_track(env_key: &str) -> bool { + let keys = MetadataEnvs::keys(); + keys.iter().any(|k| *k == env_key) + } + + pub fn env_var<'a>(&'a self, env_key: &str) -> Option> { + MetadataEnvs::var(self, env_key) + } + + pub fn env_vars(&self) -> impl Iterator)> { + MetadataEnvs::keys() + .iter() + .map(|k| (*k, MetadataEnvs::var(self, k).unwrap())) + } +} + macro_rules! get_metadata_env { ($meta:ident, $field:ident) => { $meta.$field.as_deref().unwrap_or_default().into() @@ -193,24 +211,6 @@ metadata_envs! { (readme, "CARGO_PKG_README"), } -impl ManifestMetadata { - /// Whether the given env var should be tracked by Cargo's dep-info. - pub fn should_track(env_key: &str) -> bool { - let keys = MetadataEnvs::keys(); - keys.iter().any(|k| *k == env_key) - } - - pub fn env_var<'a>(&'a self, env_key: &str) -> Option> { - MetadataEnvs::var(self, env_key) - } - - pub fn env_vars(&self) -> impl Iterator)> { - MetadataEnvs::keys() - .iter() - .map(|k| (*k, MetadataEnvs::var(self, k).unwrap())) - } -} - #[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum TargetKind { Lib(Vec),