Skip to content

Commit

Permalink
refactoring strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2023
1 parent 2ddd8c9 commit cfa2ca6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
10 changes: 2 additions & 8 deletions src/cli/hook_env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use color_eyre::eyre::Result;

use crate::cli::command::Command;
Expand Down Expand Up @@ -28,14 +26,10 @@ impl Command for HookEnv {
config.ensure_installed()?;

self.clear_old_env(out);
let mut env: HashMap<String, String> = config
.env()?
.iter()
.map(|(k, v)| (k.into(), v.into()))
.collect();
let mut env = config.env()?;
env.insert("PATH".into(), config.path_env()?);
env.insert("__RTX_DIR".into(), dirs::CURRENT.to_string_lossy().into());
let diff = EnvDiff::new(&env::PRISTINE_ENV, &env);
let diff = EnvDiff::new(&env::PRISTINE_ENV, env);
let mut patches = diff.to_patches();
patches.push(EnvDiffOperation::Add(
"__RTX_DIFF".into(),
Expand Down
41 changes: 23 additions & 18 deletions src/env_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ pub enum EnvDiffOperation {
pub type EnvDiffPatches = Vec<EnvDiffOperation>;

impl EnvDiff {
pub fn new(original: &HashMap<String, String>, additions: &HashMap<String, String>) -> EnvDiff {
pub fn new<T>(original: &HashMap<String, String>, additions: T) -> EnvDiff
where
T: IntoIterator<Item = (String, String)>,
{
let mut diff = EnvDiff::default();

for (key, new_val) in additions.iter() {
match original.get(key) {
for (key, new_val) in additions.into_iter() {
let key: String = key;
match original.get(&key) {
Some(original_val) => {
if original_val != new_val {
diff.old.insert(key.into(), original_val.into());
diff.new.insert(key.into(), new_val.into());
if original_val != &new_val {
diff.old.insert(key.clone(), original_val.into());
diff.new.insert(key, new_val);
}
}
None => {
diff.new.insert(key.into(), new_val.into());
diff.new.insert(key, new_val);
}
}
}
Expand Down Expand Up @@ -77,7 +81,7 @@ impl EnvDiff {
}
additions.insert(k.into(), v.into());
}
Ok(Self::new(&env::vars().collect(), &additions))
Ok(Self::new(&env::vars().collect(), additions))
}

pub fn deserialize(raw: &str) -> Result<EnvDiff> {
Expand Down Expand Up @@ -140,6 +144,7 @@ impl Debug for EnvDiff {

#[cfg(test)]
mod tests {
use indexmap::indexmap;
use insta::assert_debug_snapshot;

use crate::dirs;
Expand All @@ -148,13 +153,13 @@ mod tests {

#[test]
fn test_diff() {
let diff = EnvDiff::new(&new_from_hashmap(), &new_to_hashmap());
let diff = EnvDiff::new(&new_from_hashmap(), new_to_hashmap());
assert_debug_snapshot!(diff.to_patches());
}

#[test]
fn test_reverse() {
let diff = EnvDiff::new(&new_from_hashmap(), &new_to_hashmap());
let diff = EnvDiff::new(&new_from_hashmap(), new_to_hashmap());
let patches = diff.reverse().to_patches();
let to_remove = patches
.iter()
Expand Down Expand Up @@ -203,7 +208,7 @@ mod tests {

#[test]
fn test_serialize() {
let diff = EnvDiff::new(&new_from_hashmap(), &new_to_hashmap());
let diff = EnvDiff::new(&new_from_hashmap(), new_to_hashmap());
let serialized = diff.serialize().unwrap();
let deserialized = EnvDiff::deserialize(&serialized).unwrap();
assert_debug_snapshot!(deserialized.to_patches());
Expand All @@ -212,13 +217,13 @@ mod tests {
#[test]
fn test_from_bash_script() {
let path = dirs::HOME.join("fixtures/exec-env");
let orig = HashMap::from(
[
("UNMODIFIED_VAR", "unmodified"),
("MODIFIED_VAR", "original"),
]
.map(|(k, v)| (k.into(), v.into())),
);
let orig = indexmap! {
"UNMODIFIED_VAR" => "unmodified",
"MODIFIED_VAR" => "original",
}
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect::<HashMap<String, String>>();
let ed = EnvDiff::from_bash_script(path.as_path(), orig).unwrap();
assert_debug_snapshot!(ed);
}
Expand Down

0 comments on commit cfa2ca6

Please sign in to comment.