Skip to content

Commit 35b53b8

Browse files
committed
add free function to generate auxiliary envs
1 parent 8ae0773 commit 35b53b8

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/cargo/util/config/mod.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,30 @@ macro_rules! get_value_typed {
124124
};
125125
}
126126

127+
/// Generate `case_insensitive_env` and `normalized_env` from the `env`.
128+
fn make_case_insensitive_and_normalized_env(
129+
env: &HashMap<OsString, OsString>,
130+
) -> (HashMap<String, String>, HashMap<String, String>) {
131+
// See `Config.case_insensitive_env`.
132+
// Maps from uppercased key to actual environment key.
133+
// For example, `"PATH" => "Path"`.
134+
let case_insensitive_env: HashMap<_, _> = env
135+
.keys()
136+
.filter_map(|k| k.to_str())
137+
.map(|k| (k.to_uppercase(), k.to_owned()))
138+
.collect();
139+
// See `Config.normalized_env`.
140+
// Maps from normalized (uppercased with "-" replaced by "_") key
141+
// to actual environment key. For example, `"MY_KEY" => "my-key"`.
142+
let normalized_env = env
143+
.iter()
144+
// Only keep entries where both the key and value are valid UTF-8
145+
.filter_map(|(k, v)| Some((k.to_str()?, v.to_str()?)))
146+
.map(|(k, _)| (k.to_uppercase().replace("-", "_"), k.to_owned()))
147+
.collect();
148+
(case_insensitive_env, normalized_env)
149+
}
150+
127151
/// Indicates why a config value is being loaded.
128152
#[derive(Clone, Copy, Debug)]
129153
enum WhyLoad {
@@ -266,19 +290,7 @@ impl Config {
266290
});
267291

268292
let env: HashMap<_, _> = env::vars_os().collect();
269-
270-
let case_insensitive_env: HashMap<_, _> = env
271-
.keys()
272-
.filter_map(|k| k.to_str())
273-
.map(|k| (k.to_uppercase(), k.to_owned()))
274-
.collect();
275-
276-
let normalized_env = env
277-
.iter()
278-
// Only keep entries where both the key and value are valid UTF-8
279-
.filter_map(|(k, v)| Some((k.to_str()?, v.to_str()?)))
280-
.map(|(k, _)| (k.to_uppercase().replace("-", "_"), k.to_owned()))
281-
.collect();
293+
let (case_insensitive_env, normalized_env) = make_case_insensitive_and_normalized_env(&env);
282294

283295
let cache_key: &OsStr = "CARGO_CACHE_RUSTC_INFO".as_ref();
284296
let cache_rustc_info = match env.get(cache_key) {
@@ -743,14 +755,10 @@ impl Config {
743755
/// Helper primarily for testing.
744756
pub fn set_env(&mut self, env: HashMap<String, String>) {
745757
self.env = env.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
746-
self.case_insensitive_env = self
747-
.env_keys()
748-
.map(|k| (k.to_uppercase(), k.to_owned()))
749-
.collect();
750-
self.normalized_env = self
751-
.env()
752-
.map(|(k, _)| (k.to_uppercase().replace("-", "_"), k.to_owned()))
753-
.collect();
758+
let (case_insensitive_env, normalized_env) =
759+
make_case_insensitive_and_normalized_env(&self.env);
760+
self.case_insensitive_env = case_insensitive_env;
761+
self.normalized_env = normalized_env;
754762
}
755763

756764
/// Returns all environment variables as an iterator, filtering out entries

0 commit comments

Comments
 (0)