Skip to content

Commit 2d3cfb5

Browse files
committed
use hashset instead vectors
1 parent 27f19ee commit 2d3cfb5

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

src/main.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use git2::Repository;
2+
use std::collections::HashSet;
23
use std::env;
34
use glob::Pattern;
45
use std::process::Command;
56
use std::time::Instant;
67

7-
#[derive(Clone)]
8+
#[derive(Clone, Debug)]
89
struct PatternFilter {
910
pattern: String,
1011
exclude: bool,
@@ -32,13 +33,18 @@ fn main() {
3233
let duration = start.elapsed();
3334
println!("Getting changed files done in: {:?}", duration);
3435

36+
println!("Changed files: {:?}", changed_files);
37+
3538
let start = Instant::now();
3639
let filtered_files = filter(changed_files, include_patterns_filters, exclude_patterns_filters);
3740
let duration = start.elapsed();
3841
println!("Filtering files done in: {:?}", duration);
3942

4043
let count = get_count(filtered_files.clone());
4144

45+
println!("Filtered files: {:?}", filtered_files);
46+
println!("Count: {}", count);
47+
4248
Command::new("sh")
4349
.arg("-c")
4450
.arg(format!("echo \"DIFF_FILES={:?}\" >> $GITHUB_OUTPUT", filtered_files))
@@ -53,10 +59,17 @@ fn main() {
5359
}
5460

5561
fn create_patterns_filters(arg: &str) -> Vec<PatternFilter> {
56-
let patterns = arg
62+
let binding = arg
5763
.split('=')
5864
.last()
5965
.expect("Failed to get patterns")
66+
.replace(" ", "")
67+
.replace("\n", ",")
68+
.replace("\r", "")
69+
.replace(",,", ",")
70+
.to_string();
71+
72+
let patterns = binding
6073
.split(',')
6174
.collect::<Vec<&str>>();
6275

@@ -120,13 +133,16 @@ fn get_changed_files() -> Vec<String> {
120133
changed_files
121134
}
122135

123-
fn filter(changed_files: Vec<String>, include_patterns_filters: Vec<PatternFilter>, exclude_patterns_filters: Vec<PatternFilter>) -> Vec<String> {
136+
fn filter(changed_files: Vec<String>, include_patterns_filters: Vec<PatternFilter>, exclude_patterns_filters: Vec<PatternFilter>) -> HashSet<String> {
124137
let filtered_files: Vec<String> = include_patterns_filters
125138
.iter()
126139
.flat_map(|pattern| filter_files_by_pattern(pattern, &changed_files, &exclude_patterns_filters))
127140
.collect();
128141

129-
filtered_files
142+
let mut hash_set_filtered_files = HashSet::new();
143+
hash_set_filtered_files.extend(filtered_files.clone());
144+
145+
hash_set_filtered_files
130146
}
131147

132148
fn filter_files_by_pattern(pattern_filter: &PatternFilter, files: &Vec<String>, exclude_patterns: &Vec<PatternFilter>) -> Vec<String> {
@@ -150,7 +166,7 @@ fn filter_files_by_pattern(pattern_filter: &PatternFilter, files: &Vec<String>,
150166
filtered_files
151167
}
152168

153-
fn get_count(filtered_files: Vec<String>) -> usize {
169+
fn get_count(filtered_files: HashSet<String>) -> usize {
154170
filtered_files.len()
155171
}
156172

src/tests/integration.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod integration {
88
fn test_filter() {
99
let arg = "--patterns=*.rs,!*..txt";
1010
let files = vec![
11-
String::from("main.rs"),
11+
String::from("src/main.rs"),
1212
String::from("lib.rs"),
1313
String::from("test.txt"),
1414
];
@@ -21,10 +21,12 @@ mod integration {
2121

2222
let count = get_count(filtered_files.clone());
2323

24-
assert_eq!(
25-
filtered_files,
26-
vec![String::from("main.rs"), String::from("lib.rs")]
27-
);
24+
let expected_filtered_files = HashSet::from([
25+
String::from("src/main.rs"),
26+
String::from("lib.rs"),
27+
]);
28+
29+
assert_eq!(filtered_files, expected_filtered_files);
2830
assert_eq!(count, 2);
2931
}
3032
}

src/tests/unit.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ mod unit {
4040
#[test]
4141
fn test_filter() {
4242
let files = vec![
43-
String::from("main.rs"),
43+
String::from("src/main.rs"),
4444
String::from("lib.rs"),
4545
String::from("test.txt"),
4646
];
@@ -49,6 +49,10 @@ mod unit {
4949
pattern: String::from("*.rs"),
5050
exclude: false,
5151
},
52+
PatternFilter {
53+
pattern: String::from("src/**"),
54+
exclude: false,
55+
},
5256
PatternFilter {
5357
pattern: String::from("*.txt"),
5458
exclude: false,
@@ -59,10 +63,12 @@ mod unit {
5963
exclude: true,
6064
}];
6165
let filtered_files = filter(files, include_patterns_filters, exclude_patterns_filters);
62-
assert_eq!(
63-
filtered_files,
64-
vec![String::from("main.rs"), String::from("lib.rs")]
65-
);
66+
let expected_filtered_files = HashSet::from([
67+
String::from("src/main.rs"),
68+
String::from("lib.rs"),
69+
]);
70+
71+
assert_eq!(filtered_files, expected_filtered_files);
6672
}
6773

6874
#[test]
@@ -123,11 +129,11 @@ mod unit {
123129

124130
#[test]
125131
fn test_get_count() {
126-
let files = vec![
132+
let files = HashSet::from([
127133
String::from("main.rs"),
128134
String::from("lib.rs"),
129135
String::from("version.txt"),
130-
];
136+
]);
131137
let count = get_count(files);
132138
assert_eq!(count, 3);
133139
}

0 commit comments

Comments
 (0)