-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1abab5
commit 3a74846
Showing
7 changed files
with
188 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,51 @@ | ||
use std::{ | ||
convert::{AsRef, From}, | ||
fmt::{self, Display}, | ||
fs, | ||
path::Path, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct Metric { | ||
value: u64, | ||
pub value: u64, | ||
} | ||
|
||
impl Metric { | ||
pub fn init_lc<P: AsRef<Path>>(path: P) -> Option<Self> { | ||
let data = fs::read(path.as_ref()).ok(); | ||
|
||
let Some(text) = data else { | ||
return None; | ||
}; | ||
|
||
let lines = text.into_iter().fold(0, |acc, item| { | ||
if u32::from(item) == u32::from('\n') { | ||
acc + 1 | ||
} else { | ||
acc | ||
} | ||
}); | ||
|
||
u64::try_from(lines).map(|value| Self { value }).ok() | ||
} | ||
} | ||
|
||
impl From<u64> for Metric { | ||
fn from(value: u64) -> Self { | ||
Self { value } | ||
} | ||
} | ||
|
||
impl Display for Metric { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
<u64 as Display>::fmt(&self.value, f) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_lc() { | ||
let metric = Metric::init_lc("tests/data/nemesis.txt") | ||
.expect("Expected 'tests/data/nemesis.txt' to exist"); | ||
|
||
assert_eq!(metric.value, 4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use indoc::indoc; | ||
|
||
mod utils; | ||
|
||
#[test] | ||
fn line_count() { | ||
assert_eq!( | ||
utils::run_cmd(&["--disk-usage", "line", "tests/data"]), | ||
indoc!( | ||
"6 ┌─ cassildas_song.md | ||
6 ┌─ the_yellow_king | ||
1 ├─ nylarlathotep.txt | ||
4 ├─ nemesis.txt | ||
2 ├─ necronomicon.txt | ||
1 │ ┌─ lipsum.txt | ||
1 ├─ lipsum | ||
10 │ ┌─ polaris.txt | ||
10 ├─ dream_cycle | ||
24 data | ||
3 directories, 6 files" | ||
) | ||
) | ||
} |