Skip to content

Commit 40314cd

Browse files
committed
Add new trait Sizable and modify total_size to work on values of that trait
1 parent 334ed9e commit 40314cd

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

src/crufty/estimator.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,38 @@ use walkdir::WalkDir;
44

55
use super::types::{ArtifactCandidate, Size};
66

7-
pub fn total_size(artifacts: &[ArtifactCandidate]) -> Size {
8-
let total = artifacts
7+
pub trait Sizable {
8+
fn get_size(&self) -> &Size;
9+
fn bytes(&self) -> &u64 {
10+
match self.get_size() {
11+
Size::UnknownSize => &0,
12+
Size::KnownSize(b) => b,
13+
}
14+
}
15+
}
16+
17+
impl Sizable for ArtifactCandidate {
18+
fn get_size(&self) -> &Size {
19+
&self.size
20+
}
21+
}
22+
23+
impl Sizable for Size {
24+
fn get_size(&self) -> &Size {
25+
&self
26+
}
27+
}
28+
29+
impl Sizable for &Size {
30+
fn get_size(&self) -> &Size {
31+
self
32+
}
33+
}
34+
35+
pub fn total_size(sizables: &[impl Sizable]) -> Size {
36+
let total = sizables
937
.iter()
10-
.fold(0, |acc, artifact| match &artifact.size {
11-
Size::UnknownSize => acc,
12-
Size::KnownSize(size) => acc + size,
13-
});
38+
.fold(0, |acc, artifact| acc + artifact.bytes());
1439
Size::KnownSize(total)
1540
}
1641

0 commit comments

Comments
 (0)