Skip to content

Commit f43bb2a

Browse files
committed
Refactor, re-arrange code into submodules
1 parent 0b1b74b commit f43bb2a

5 files changed

Lines changed: 173 additions & 167 deletions

File tree

src/crufty.rs

Lines changed: 2 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,3 @@
1-
#![allow(dead_code)]
2-
use std::fmt;
3-
use std::path::PathBuf;
4-
5-
use globset::{Glob, GlobSetBuilder};
6-
use ignore::WalkBuilder;
7-
81
pub mod cli;
9-
10-
#[derive(Debug, PartialEq, Eq, Clone)]
11-
pub enum Size {
12-
UnknownSize,
13-
KnownSize(u64),
14-
}
15-
16-
impl fmt::Display for Size {
17-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18-
match self {
19-
Size::UnknownSize => write!(f, "unknown"),
20-
Size::KnownSize(bytes) => {
21-
if *bytes < 1024 {
22-
write!(f, "{} B", bytes)
23-
} else if *bytes < 1024 * 1024 {
24-
write!(f, "{:.1} KB", *bytes as f64 / 1024.0)
25-
} else if *bytes < 1024 * 1024 * 1024 {
26-
write!(f, "{:.1} MB", *bytes as f64 / (1024.0 * 1024.0))
27-
} else {
28-
write!(f, "{:.1} GB", *bytes as f64 / (1024.0 * 1024.0 * 1024.0))
29-
}
30-
}
31-
}
32-
}
33-
}
34-
35-
#[derive(Debug, PartialEq, Eq)]
36-
pub struct ArtifactCandidate {
37-
pub path: PathBuf,
38-
pub size: Size,
39-
}
40-
41-
impl ArtifactCandidate {
42-
fn new(path: PathBuf) -> Self {
43-
ArtifactCandidate {
44-
path,
45-
size: Size::UnknownSize,
46-
}
47-
}
48-
}
49-
50-
impl Ord for ArtifactCandidate {
51-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
52-
self.path.cmp(&other.path)
53-
}
54-
}
55-
56-
impl PartialOrd for ArtifactCandidate {
57-
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
58-
Some(self.cmp(other))
59-
}
60-
}
61-
62-
pub fn fetch_artifacts(root_path: &PathBuf) -> Vec<ArtifactCandidate> {
63-
let mut builder = GlobSetBuilder::new();
64-
// FIX-ME hardcoded pattern for Rust
65-
builder.add(Glob::new("**/target").unwrap());
66-
67-
let globset = builder.build().unwrap();
68-
69-
WalkBuilder::new(root_path)
70-
.git_ignore(false)
71-
.build()
72-
.filter_map(|entry_result| match entry_result {
73-
Err(_) => None,
74-
Ok(entry) if !entry.path().is_dir() => None,
75-
Ok(entry) => {
76-
let path = entry.into_path();
77-
let rel_path = path.strip_prefix(root_path).ok()?;
78-
match globset.is_match(&rel_path) {
79-
true => Some(ArtifactCandidate::new(path)),
80-
false => None,
81-
}
82-
}
83-
})
84-
.collect()
85-
}
86-
87-
#[cfg(test)]
88-
mod tests {
89-
90-
use assert_fs::prelude::*;
91-
use assert_fs::{
92-
fixture::{ChildPath, PathChild},
93-
TempDir,
94-
};
95-
96-
use super::{fetch_artifacts, ArtifactCandidate};
97-
98-
fn mk_subpath(base: &TempDir, rel_path: &str) -> ChildPath {
99-
let sub = base.child(rel_path);
100-
sub.create_dir_all().unwrap();
101-
sub
102-
}
103-
104-
fn mk_rust_project<P: PathChild>(base: &P) {
105-
base.child("target").create_dir_all().unwrap();
106-
base.child("Cargo.toml").touch().unwrap();
107-
}
108-
109-
#[test]
110-
fn test_simple_rust_project_being_scanned_folder() {
111-
// given
112-
let temp = TempDir::new().unwrap();
113-
mk_rust_project(&temp);
114-
115-
// when
116-
let results = fetch_artifacts(&temp.to_path_buf());
117-
118-
// then
119-
assert_eq!(results.len(), 1, "Expected exactly one artifact");
120-
121-
let expected_path = temp.child("target").path().to_path_buf();
122-
let expected = ArtifactCandidate::new(expected_path);
123-
assert_eq!(&results[0], &expected);
124-
125-
temp.close().unwrap();
126-
}
127-
128-
#[test]
129-
fn test_where_scanned_folder_has_three_rust_projects() {
130-
// given
131-
let temp = TempDir::new().unwrap();
132-
let project1 = mk_subpath(&temp, "project1");
133-
mk_rust_project(&project1);
134-
let project2 = mk_subpath(&temp, "project2");
135-
mk_rust_project(&project2);
136-
let project3 = mk_subpath(&temp, "work/project3");
137-
mk_rust_project(&project3);
138-
139-
// when
140-
let mut results = fetch_artifacts(&temp.to_path_buf());
141-
142-
// then
143-
assert_eq!(results.len(), 3, "Expected exactly three artifacts");
144-
results.sort();
145-
146-
let expected_path_1 =
147-
temp.child("project1").child("target").path().to_path_buf();
148-
let expected_1 = ArtifactCandidate::new(expected_path_1);
149-
assert_eq!(&results[0], &expected_1);
150-
151-
let expected_path_2 =
152-
temp.child("project2").child("target").path().to_path_buf();
153-
let expected_2 = ArtifactCandidate::new(expected_path_2);
154-
assert_eq!(&results[1], &expected_2);
155-
156-
let expected_path_3 = temp
157-
.child("work")
158-
.child("project3")
159-
.child("target")
160-
.path()
161-
.to_path_buf();
162-
let expected_3 = ArtifactCandidate::new(expected_path_3);
163-
assert_eq!(&results[2], &expected_3);
164-
165-
temp.close().unwrap();
166-
}
167-
}
2+
pub mod fetcher;
3+
pub mod types;

src/crufty/fetcher.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use std::path::PathBuf;
2+
3+
use globset::{Glob, GlobSetBuilder};
4+
use ignore::WalkBuilder;
5+
6+
use super::types::ArtifactCandidate;
7+
8+
pub fn fetch_artifacts(root_path: &PathBuf) -> Vec<ArtifactCandidate> {
9+
let mut builder = GlobSetBuilder::new();
10+
// FIX-ME hardcoded pattern for Rust
11+
builder.add(Glob::new("**/target").unwrap());
12+
13+
let globset = builder.build().unwrap();
14+
15+
WalkBuilder::new(root_path)
16+
.git_ignore(false)
17+
.build()
18+
.filter_map(|entry_result| match entry_result {
19+
Err(_) => None,
20+
Ok(entry) if !entry.path().is_dir() => None,
21+
Ok(entry) => {
22+
let path = entry.into_path();
23+
let rel_path = path.strip_prefix(root_path).ok()?;
24+
match globset.is_match(&rel_path) {
25+
true => Some(ArtifactCandidate::new(path)),
26+
false => None,
27+
}
28+
}
29+
})
30+
.collect()
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
36+
use assert_fs::prelude::*;
37+
use assert_fs::{
38+
fixture::{ChildPath, PathChild},
39+
TempDir,
40+
};
41+
42+
use super::{fetch_artifacts, ArtifactCandidate};
43+
44+
fn mk_subpath(base: &TempDir, rel_path: &str) -> ChildPath {
45+
let sub = base.child(rel_path);
46+
sub.create_dir_all().unwrap();
47+
sub
48+
}
49+
50+
fn mk_rust_project<P: PathChild>(base: &P) {
51+
base.child("target").create_dir_all().unwrap();
52+
base.child("Cargo.toml").touch().unwrap();
53+
}
54+
55+
#[test]
56+
fn test_simple_rust_project_being_scanned_folder() {
57+
// given
58+
let temp = TempDir::new().unwrap();
59+
mk_rust_project(&temp);
60+
61+
// when
62+
let results = fetch_artifacts(&temp.to_path_buf());
63+
64+
// then
65+
assert_eq!(results.len(), 1, "Expected exactly one artifact");
66+
67+
let expected_path = temp.child("target").path().to_path_buf();
68+
let expected = ArtifactCandidate::new(expected_path);
69+
assert_eq!(&results[0], &expected);
70+
71+
temp.close().unwrap();
72+
}
73+
74+
#[test]
75+
fn test_where_scanned_folder_has_three_rust_projects() {
76+
// given
77+
let temp = TempDir::new().unwrap();
78+
let project1 = mk_subpath(&temp, "project1");
79+
mk_rust_project(&project1);
80+
let project2 = mk_subpath(&temp, "project2");
81+
mk_rust_project(&project2);
82+
let project3 = mk_subpath(&temp, "work/project3");
83+
mk_rust_project(&project3);
84+
85+
// when
86+
let mut results = fetch_artifacts(&temp.to_path_buf());
87+
88+
// then
89+
assert_eq!(results.len(), 3, "Expected exactly three artifacts");
90+
results.sort();
91+
92+
let expected_path_1 =
93+
temp.child("project1").child("target").path().to_path_buf();
94+
let expected_1 = ArtifactCandidate::new(expected_path_1);
95+
assert_eq!(&results[0], &expected_1);
96+
97+
let expected_path_2 =
98+
temp.child("project2").child("target").path().to_path_buf();
99+
let expected_2 = ArtifactCandidate::new(expected_path_2);
100+
assert_eq!(&results[1], &expected_2);
101+
102+
let expected_path_3 = temp
103+
.child("work")
104+
.child("project3")
105+
.child("target")
106+
.path()
107+
.to_path_buf();
108+
let expected_3 = ArtifactCandidate::new(expected_path_3);
109+
assert_eq!(&results[2], &expected_3);
110+
111+
temp.close().unwrap();
112+
}
113+
}

src/crufty/types.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![allow(dead_code)]
2+
use std::fmt;
3+
use std::path::PathBuf;
4+
5+
#[derive(Debug, PartialEq, Eq, Clone)]
6+
pub enum Size {
7+
UnknownSize,
8+
KnownSize(u64),
9+
}
10+
11+
impl fmt::Display for Size {
12+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13+
match self {
14+
Size::UnknownSize => write!(f, "unknown"),
15+
Size::KnownSize(bytes) => {
16+
if *bytes < 1024 {
17+
write!(f, "{} B", bytes)
18+
} else if *bytes < 1024 * 1024 {
19+
write!(f, "{:.1} KB", *bytes as f64 / 1024.0)
20+
} else if *bytes < 1024 * 1024 * 1024 {
21+
write!(f, "{:.1} MB", *bytes as f64 / (1024.0 * 1024.0))
22+
} else {
23+
write!(f, "{:.1} GB", *bytes as f64 / (1024.0 * 1024.0 * 1024.0))
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
#[derive(Debug, PartialEq, Eq)]
31+
pub struct ArtifactCandidate {
32+
pub path: PathBuf,
33+
pub size: Size,
34+
}
35+
36+
impl ArtifactCandidate {
37+
pub fn new(path: PathBuf) -> Self {
38+
ArtifactCandidate {
39+
path,
40+
size: Size::UnknownSize,
41+
}
42+
}
43+
}
44+
45+
impl Ord for ArtifactCandidate {
46+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
47+
self.path.cmp(&other.path)
48+
}
49+
}
50+
51+
impl PartialOrd for ArtifactCandidate {
52+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
53+
Some(self.cmp(other))
54+
}
55+
}

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clap::Parser;
22
use console::{style, Term};
33
use crufty::cli::{Cli, Commands};
4-
use crufty::{fetch_artifacts, Size};
4+
use crufty::fetcher::fetch_artifacts;
5+
use crufty::types::Size;
56
use std::env;
67
use std::io;
78
use std::process;

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)