From 978b29e9761f551997303609c1f3ef74debb2de3 Mon Sep 17 00:00:00 2001 From: Mathieu Bes Date: Tue, 26 Nov 2024 20:22:03 +0100 Subject: [PATCH] chore: replace vectors with hashsets for dependencies --- src/main.rs | 2 +- src/project/mod.rs | 9 ++++++--- src/project/node_js.rs | 37 +++++++++++++++++-------------------- src/project/rust.rs | 31 +++++++++++++++---------------- 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index f8bfeff..fb526ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() { match cli.command { Some(Commands::All) => { todo!("Add project detection with the configured package name constant.") - }, + } Some(Commands::NodeJS) => scan_project_deps(NodeProject::new()), Some(Commands::Rust) => scan_project_deps(RustProject::new()), None => {} diff --git a/src/project/mod.rs b/src/project/mod.rs index ede8cb3..71d3f4a 100644 --- a/src/project/mod.rs +++ b/src/project/mod.rs @@ -14,7 +14,7 @@ pub trait Project { const EXCLUDED_PATHS: &[&str]; fn parse_deps(&mut self, deps_file_content: &str) -> usize; - fn deps(&self) -> &Vec; + fn deps(&self) -> &HashSet; } pub fn scan_project_deps(mut project: T) { @@ -97,7 +97,7 @@ fn should_scan_file(file_path: &str) -> bool { #[cfg(test)] mod tests { - use super::{should_scan_file, node_js::NodeProject, rust::RustProject}; + use super::{node_js::NodeProject, rust::RustProject, should_scan_file}; #[test] fn node_js_should_scan_file() { @@ -111,7 +111,10 @@ mod tests { assert_eq!(should_scan_file::("foo.rs"), false); assert_eq!(should_scan_file::("foo.jssx"), false); assert_eq!(should_scan_file::("package.json"), false); - assert_eq!(should_scan_file::("foo/node_modules/foo.ts"), false); + assert_eq!( + should_scan_file::("foo/node_modules/foo.ts"), + false + ); } #[test] diff --git a/src/project/node_js.rs b/src/project/node_js.rs index a1ed313..3b36abc 100644 --- a/src/project/node_js.rs +++ b/src/project/node_js.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use super::Project; @@ -12,13 +12,13 @@ pub struct NodePackagesHandler { } pub struct NodeProject { - deps: Vec, + deps: HashSet, } impl NodeProject { pub fn new() -> Self { Self { - deps: Vec::new() + deps: HashSet::new(), } } } @@ -35,7 +35,7 @@ impl Project for NodeProject { self.deps.len() } - fn deps(&self) -> &Vec { + fn deps(&self) -> &HashSet { &self.deps } } @@ -49,20 +49,17 @@ fn is_used_in_package_scripts(parsed_file: &NodePackagesHandler, name: &str) -> false } -fn get_deps_names(parsed_file: NodePackagesHandler) -> Vec { - let mut names: Vec = - parsed_file - .dependencies - .iter() - .fold(Vec::new(), |mut acc, (name, _version)| { - if name.starts_with("@types/") || is_used_in_package_scripts(&parsed_file, name) { - return acc; - } - acc.push(name.into()); - acc - }); - names.sort(); - names +fn get_deps_names(parsed_file: NodePackagesHandler) -> HashSet { + parsed_file + .dependencies + .iter() + .fold(HashSet::new(), |mut acc, (name, _version)| { + if name.starts_with("@types/") || is_used_in_package_scripts(&parsed_file, name) { + return acc; + } + acc.insert(name.into()); + acc + }) } #[cfg(test)] @@ -87,7 +84,7 @@ mod tests { .dependencies .insert("@types/foo".into(), "0.1.0".into()); - assert_eq!(get_deps_names(packages_handler), vec!["bar", "foo"]); + assert_eq!(get_deps_names(packages_handler), HashSet::from(["bar".into(), "foo".into()])); } #[test] @@ -113,7 +110,7 @@ mod tests { assert_eq!(project.parse_deps(file_content), 3); assert_eq!(project.deps.len(), 3); - assert_eq!(project.deps, vec!["bar", "bazz", "foo"]); + assert_eq!(project.deps, HashSet::from(["bar".into(), "bazz".into(), "foo".into()])); } #[test] diff --git a/src/project/rust.rs b/src/project/rust.rs index 5305932..16fa4ba 100644 --- a/src/project/rust.rs +++ b/src/project/rust.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use serde::Deserialize; use toml::Table; @@ -9,14 +11,12 @@ pub struct RustPackagesHandler { } pub struct RustProject { - deps: Vec, + deps: HashSet, } impl RustProject { pub fn new() -> Self { - Self { - deps: Vec::new() - } + Self { deps: HashSet::new() } } } @@ -32,20 +32,19 @@ impl Project for RustProject { self.deps.len() } - fn deps(&self) -> &Vec { + fn deps(&self) -> &HashSet { &self.deps } } -fn get_deps_names(parsed_file: RustPackagesHandler) -> Vec { - let mut names = Vec::from_iter( - parsed_file - .dependencies - .iter() - .map(|(name, _version)| name.clone()), - ); - names.sort(); - names +fn get_deps_names(parsed_file: RustPackagesHandler) -> HashSet { + parsed_file + .dependencies + .iter() + .fold(HashSet::new(), |mut acc, (name, _version)| { + acc.insert(name.into()); + acc + }) } #[cfg(test)] @@ -64,7 +63,7 @@ mod tests { .dependencies .insert("bar".into(), "0.1.0".into()); - assert_eq!(get_deps_names(packages_handler), Vec::from(["bar", "foo"])); + assert_eq!(get_deps_names(packages_handler), HashSet::from(["bar".into(), "foo".into()])); } #[test] @@ -77,6 +76,6 @@ mod tests { assert_eq!(project.parse_deps(file_content), 2); assert_eq!(project.deps.len(), 2); - assert_eq!(project.deps, Vec::from(["bar", "foo"])); + assert_eq!(project.deps, HashSet::from(["bar".into(), "foo".into()])); } }