Refactor slightly

main
mat ess 2023-04-18 00:56:32 -04:00
parent db3641200c
commit bd64f45e56
2 changed files with 14 additions and 5 deletions

View File

@ -10,13 +10,17 @@ pub struct Library {
}
impl Library {
pub fn find_covers(&self) -> HashSet<&Song> {
fn all_covers(&self) -> HashSet<&Song> {
self.artists
.iter()
.fold(HashSet::new(), |mut covers, artist| {
covers.extend(artist.covered_songs());
covers
})
}
pub fn find_library_covers(&self) -> HashSet<&Song> {
self.all_covers()
.into_iter()
.filter(|song| song.is_library_cover(self))
.collect()
@ -171,6 +175,11 @@ mod tests {
performer: "Bon Iver".to_string(),
writers: ["Justin Vernon".to_string()].into(),
},
Song {
title: "The Times They Are A-Changin'".to_string(),
performer: "Bon Iver".to_string(),
writers: ["Bob Dylan".to_string()].into(),
},
]
.into(),
}
@ -189,12 +198,12 @@ mod tests {
#[test]
fn test_covered_songs_no_covers() {
assert!(bon_iver().covered_songs().is_empty())
assert!(ts().covered_songs().is_empty())
}
#[test]
fn test_find_covers() {
assert!(library().find_covers() == HashSet::from([&love_story("black midi")]))
assert!(library().find_library_covers() == HashSet::from([&love_story("black midi")]))
}
#[test]
@ -202,7 +211,7 @@ mod tests {
assert!(Library {
artists: [bon_iver()].into()
}
.find_covers()
.find_library_covers()
.is_empty())
}
}

View File

@ -10,7 +10,7 @@ fn main() {
let json = fs::read_to_string(file_path).expect("Failed to open library file");
let library: library::Library =
DeJson::deserialize_json(&json).expect("Failed to deserialize library file");
for cover in library.find_covers() {
for cover in library.find_library_covers() {
println!("Found cover!");
println!(
"{}, by {:?}, covered by {}",