Skip to content

Commit 2939e96

Browse files
authored
refactor(manifest): Clean up field -> env var handling (#15008)
### What does this PR try to resolve? This is basically my PR feedback for #14973. ### How should we test and review this PR? ### Additional information
2 parents f30f452 + 0414bb5 commit 2939e96

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

src/cargo/core/manifest.rs

+34-50
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ pub struct ManifestMetadata {
147147
pub rust_version: Option<RustVersion>,
148148
}
149149

150+
impl ManifestMetadata {
151+
/// Whether the given env var should be tracked by Cargo's dep-info.
152+
pub fn should_track(env_key: &str) -> bool {
153+
let keys = MetadataEnvs::keys();
154+
keys.iter().any(|k| *k == env_key)
155+
}
156+
157+
pub fn env_var<'a>(&'a self, env_key: &str) -> Option<Cow<'a, str>> {
158+
MetadataEnvs::var(self, env_key)
159+
}
160+
161+
pub fn env_vars(&self) -> impl Iterator<Item = (&'static str, Cow<'_, str>)> {
162+
MetadataEnvs::keys()
163+
.iter()
164+
.map(|k| (*k, MetadataEnvs::var(self, k).unwrap()))
165+
}
166+
}
167+
150168
macro_rules! get_metadata_env {
151169
($meta:ident, $field:ident) => {
152170
$meta.$field.as_deref().unwrap_or_default().into()
@@ -156,43 +174,24 @@ macro_rules! get_metadata_env {
156174
};
157175
}
158176

177+
struct MetadataEnvs;
178+
159179
macro_rules! metadata_envs {
160180
(
161181
$(
162182
($field:ident, $key:literal$(, $to_var:expr)?),
163183
)*
164184
) => {
165-
struct MetadataEnvs;
166185
impl MetadataEnvs {
167-
$(
168-
fn $field(meta: &ManifestMetadata) -> Cow<'_, str> {
169-
get_metadata_env!(meta, $field$(, $to_var)?)
170-
}
171-
)*
172-
173-
pub fn should_track(key: &str) -> bool {
174-
let keys = [$($key),*];
175-
key.strip_prefix("CARGO_PKG_")
176-
.map(|key| keys.iter().any(|k| *k == key))
177-
.unwrap_or_default()
186+
fn keys() -> &'static [&'static str] {
187+
&[$($key),*]
178188
}
179189

180-
pub fn var<'a>(meta: &'a ManifestMetadata, key: &str) -> Option<Cow<'a, str>> {
181-
key.strip_prefix("CARGO_PKG_").and_then(|key| match key {
182-
$($key => Some(Self::$field(meta)),)*
190+
fn var<'a>(meta: &'a ManifestMetadata, key: &str) -> Option<Cow<'a, str>> {
191+
match key {
192+
$($key => Some(get_metadata_env!(meta, $field$(, $to_var)?)),)*
183193
_ => None,
184-
})
185-
}
186-
187-
pub fn vars(meta: &ManifestMetadata) -> impl Iterator<Item = (&'static str, Cow<'_, str>)> {
188-
[
189-
$(
190-
(
191-
concat!("CARGO_PKG_", $key),
192-
Self::$field(meta),
193-
),
194-
)*
195-
].into_iter()
194+
}
196195
}
197196
}
198197
}
@@ -202,29 +201,14 @@ macro_rules! metadata_envs {
202201
// If these change we need to trigger a rebuild.
203202
// NOTE: The env var name will be prefixed with `CARGO_PKG_`
204203
metadata_envs! {
205-
(description, "DESCRIPTION"),
206-
(homepage, "HOMEPAGE"),
207-
(repository, "REPOSITORY"),
208-
(license, "LICENSE"),
209-
(license_file, "LICENSE_FILE"),
210-
(authors, "AUTHORS", |m: &ManifestMetadata| m.authors.join(":")),
211-
(rust_version, "RUST_VERSION", |m: &ManifestMetadata| m.rust_version.as_ref().map(ToString::to_string).unwrap_or_default()),
212-
(readme, "README"),
213-
}
214-
215-
impl ManifestMetadata {
216-
/// Whether the given env var should be tracked by Cargo's dep-info.
217-
pub fn should_track(env_key: &str) -> bool {
218-
MetadataEnvs::should_track(env_key)
219-
}
220-
221-
pub fn env_var<'a>(&'a self, env_key: &str) -> Option<Cow<'a, str>> {
222-
MetadataEnvs::var(self, env_key)
223-
}
224-
225-
pub fn env_vars(&self) -> impl Iterator<Item = (&'static str, Cow<'_, str>)> {
226-
MetadataEnvs::vars(self)
227-
}
204+
(description, "CARGO_PKG_DESCRIPTION"),
205+
(homepage, "CARGO_PKG_HOMEPAGE"),
206+
(repository, "CARGO_PKG_REPOSITORY"),
207+
(license, "CARGO_PKG_LICENSE"),
208+
(license_file, "CARGO_PKG_LICENSE_FILE"),
209+
(authors, "CARGO_PKG_AUTHORS", |m: &ManifestMetadata| m.authors.join(":")),
210+
(rust_version, "CARGO_PKG_RUST_VERSION", |m: &ManifestMetadata| m.rust_version.as_ref().map(ToString::to_string).unwrap_or_default()),
211+
(readme, "CARGO_PKG_README"),
228212
}
229213

230214
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]

0 commit comments

Comments
 (0)