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 { impl Library {
pub fn find_covers(&self) -> HashSet<&Song> { fn all_covers(&self) -> HashSet<&Song> {
self.artists self.artists
.iter() .iter()
.fold(HashSet::new(), |mut covers, artist| { .fold(HashSet::new(), |mut covers, artist| {
covers.extend(artist.covered_songs()); covers.extend(artist.covered_songs());
covers covers
}) })
}
pub fn find_library_covers(&self) -> HashSet<&Song> {
self.all_covers()
.into_iter() .into_iter()
.filter(|song| song.is_library_cover(self)) .filter(|song| song.is_library_cover(self))
.collect() .collect()
@ -171,6 +175,11 @@ mod tests {
performer: "Bon Iver".to_string(), performer: "Bon Iver".to_string(),
writers: ["Justin Vernon".to_string()].into(), 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(), .into(),
} }
@ -189,12 +198,12 @@ mod tests {
#[test] #[test]
fn test_covered_songs_no_covers() { fn test_covered_songs_no_covers() {
assert!(bon_iver().covered_songs().is_empty()) assert!(ts().covered_songs().is_empty())
} }
#[test] #[test]
fn test_find_covers() { 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] #[test]
@ -202,7 +211,7 @@ mod tests {
assert!(Library { assert!(Library {
artists: [bon_iver()].into() artists: [bon_iver()].into()
} }
.find_covers() .find_library_covers()
.is_empty()) .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 json = fs::read_to_string(file_path).expect("Failed to open library file");
let library: library::Library = let library: library::Library =
DeJson::deserialize_json(&json).expect("Failed to deserialize library file"); 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!("Found cover!");
println!( println!(
"{}, by {:?}, covered by {}", "{}, by {:?}, covered by {}",