Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace vectors with hashsets for dependencies #12

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}
Expand Down
9 changes: 6 additions & 3 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>;
fn deps(&self) -> &HashSet<String>;
}

pub fn scan_project_deps<T: Project>(mut project: T) {
Expand Down Expand Up @@ -97,7 +97,7 @@ fn should_scan_file<T: Project>(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() {
Expand All @@ -111,7 +111,10 @@ mod tests {
assert_eq!(should_scan_file::<NodeProject>("foo.rs"), false);
assert_eq!(should_scan_file::<NodeProject>("foo.jssx"), false);
assert_eq!(should_scan_file::<NodeProject>("package.json"), false);
assert_eq!(should_scan_file::<NodeProject>("foo/node_modules/foo.ts"), false);
assert_eq!(
should_scan_file::<NodeProject>("foo/node_modules/foo.ts"),
false
);
}

#[test]
Expand Down
37 changes: 17 additions & 20 deletions src/project/node_js.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use super::Project;

Expand All @@ -12,13 +12,13 @@ pub struct NodePackagesHandler {
}

pub struct NodeProject {
deps: Vec<String>,
deps: HashSet<String>,
}

impl NodeProject {
pub fn new() -> Self {
Self {
deps: Vec::new()
deps: HashSet::new(),
}
}
}
Expand All @@ -35,7 +35,7 @@ impl Project for NodeProject {
self.deps.len()
}

fn deps(&self) -> &Vec<String> {
fn deps(&self) -> &HashSet<String> {
&self.deps
}
}
Expand All @@ -49,20 +49,17 @@ fn is_used_in_package_scripts(parsed_file: &NodePackagesHandler, name: &str) ->
false
}

fn get_deps_names(parsed_file: NodePackagesHandler) -> Vec<String> {
let mut names: Vec<String> =
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<String> {
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)]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
31 changes: 15 additions & 16 deletions src/project/rust.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use serde::Deserialize;
use toml::Table;

Expand All @@ -9,14 +11,12 @@ pub struct RustPackagesHandler {
}

pub struct RustProject {
deps: Vec<String>,
deps: HashSet<String>,
}

impl RustProject {
pub fn new() -> Self {
Self {
deps: Vec::new()
}
Self { deps: HashSet::new() }
}
}

Expand All @@ -32,20 +32,19 @@ impl Project for RustProject {
self.deps.len()
}

fn deps(&self) -> &Vec<String> {
fn deps(&self) -> &HashSet<String> {
&self.deps
}
}

fn get_deps_names(parsed_file: RustPackagesHandler) -> Vec<String> {
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<String> {
parsed_file
.dependencies
.iter()
.fold(HashSet::new(), |mut acc, (name, _version)| {
acc.insert(name.into());
acc
})
}

#[cfg(test)]
Expand All @@ -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]
Expand All @@ -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()]));
}
}