From 1b2c6c5b7ed0489a663769e7c67c6d9d6c1de1b7 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 22 Oct 2024 11:21:11 -0400 Subject: [PATCH 1/3] feat: split up PackageJsonDeps into dependencies and devDependencies --- src/lib.rs | 79 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d177c4..e41e23f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,9 +42,28 @@ pub enum PackageJsonDepValue { Workspace(VersionReq), } -pub type PackageJsonDeps = +pub type PackageJsonDepsMap = IndexMap>; +#[derive(Debug, Clone)] +pub struct PackageJsonDeps { + pub dependencies: PackageJsonDepsMap, + pub dev_dependencies: PackageJsonDepsMap, +} + +impl PackageJsonDeps { + /// Gets a package.json dependency entry by alias. + pub fn get( + &self, + alias: &str, + ) -> Option<&Result> { + self + .dependencies + .get(alias) + .or_else(|| self.dev_dependencies.get(alias)) + } +} + #[derive(Debug, Error)] pub enum PackageJsonLoadError { #[error("Failed reading '{}'.", .path.display())] @@ -351,7 +370,7 @@ impl PackageJson { fn insert_deps( deps: Option<&IndexMap>, - result: &mut PackageJsonDeps, + result: &mut PackageJsonDepsMap, ) { if let Some(deps) = deps { for (key, value) in deps { @@ -364,13 +383,17 @@ impl PackageJson { let deps = self.dependencies.as_ref(); let dev_deps = self.dev_dependencies.as_ref(); - let mut result = IndexMap::new(); + let mut result_deps = IndexMap::new(); + let mut result_dev_deps = IndexMap::new(); // favors the deps over dev_deps - insert_deps(deps, &mut result); - insert_deps(dev_deps, &mut result); + insert_deps(deps, &mut result_deps); + insert_deps(dev_deps, &mut result_dev_deps); - result + PackageJsonDeps { + dependencies: result_deps, + dev_dependencies: result_dev_deps, + } } } @@ -421,9 +444,11 @@ mod test { fn get_local_package_json_version_reqs_for_tests( package_json: &PackageJson, ) -> IndexMap> { - package_json - .resolve_local_package_json_deps() + let deps = package_json.resolve_local_package_json_deps(); + deps + .dependencies .into_iter() + .chain(deps.dev_dependencies.into_iter()) .map(|(k, v)| { ( k, @@ -449,31 +474,45 @@ mod test { ])); package_json.dev_dependencies = Some(IndexMap::from([ ("package_b".to_string(), "~2.2".to_string()), - // should be ignored ("other".to_string(), "^3.2".to_string()), ])); - let deps = get_local_package_json_version_reqs_for_tests(&package_json); + let deps = package_json.resolve_local_package_json_deps(); assert_eq!( - deps, - IndexMap::from([ + deps + .dependencies + .into_iter() + .map(|d| (d.0, d.1.unwrap())) + .collect::>(), + Vec::from([ ( "test".to_string(), - Ok(PackageJsonDepValue::Req( - PackageReq::from_str("test@^1.2").unwrap() - )) + PackageJsonDepValue::Req(PackageReq::from_str("test@^1.2").unwrap()) ), ( "other".to_string(), - Ok(PackageJsonDepValue::Req( + PackageJsonDepValue::Req( PackageReq::from_str("package@~1.3").unwrap() - )) + ) ), + ]) + ); + assert_eq!( + deps + .dev_dependencies + .into_iter() + .map(|d| (d.0, d.1.unwrap())) + .collect::>(), + Vec::from([ ( "package_b".to_string(), - Ok(PackageJsonDepValue::Req( + PackageJsonDepValue::Req( PackageReq::from_str("package_b@~2.2").unwrap() - )) - ) + ) + ), + ( + "other".to_string(), + PackageJsonDepValue::Req(PackageReq::from_str("other@^3.2").unwrap()) + ), ]) ); } From e0d5431fa02880114daf5b494cc83ee766f6efa6 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 22 Oct 2024 11:23:38 -0400 Subject: [PATCH 2/3] improve --- src/lib.rs | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e41e23f..8fdc80d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -368,31 +368,22 @@ impl PackageJson { } } - fn insert_deps( - deps: Option<&IndexMap>, - result: &mut PackageJsonDepsMap, - ) { - if let Some(deps) = deps { - for (key, value) in deps { - result - .entry(key.to_string()) - .or_insert_with(|| parse_entry(key, value)); - } + fn get_map(deps: Option<&IndexMap>) -> PackageJsonDepsMap { + let Some(deps) = deps else { + return Default::default(); + }; + let mut result = IndexMap::with_capacity(deps.len()); + for (key, value) in deps { + result + .entry(key.to_string()) + .or_insert_with(|| parse_entry(key, value)); } + result } - let deps = self.dependencies.as_ref(); - let dev_deps = self.dev_dependencies.as_ref(); - let mut result_deps = IndexMap::new(); - let mut result_dev_deps = IndexMap::new(); - - // favors the deps over dev_deps - insert_deps(deps, &mut result_deps); - insert_deps(dev_deps, &mut result_dev_deps); - PackageJsonDeps { - dependencies: result_deps, - dev_dependencies: result_dev_deps, + dependencies: get_map(self.dependencies.as_ref()), + dev_dependencies: get_map(self.dev_dependencies.as_ref()), } } } From 28c472ed181af6cc4d545b8b2bbc65c9a6bde733 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 22 Oct 2024 11:25:50 -0400 Subject: [PATCH 3/3] clippy --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8fdc80d..51a704f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -439,7 +439,7 @@ mod test { deps .dependencies .into_iter() - .chain(deps.dev_dependencies.into_iter()) + .chain(deps.dev_dependencies) .map(|(k, v)| { ( k,