diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..28ca5ca --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod library; diff --git a/src/library.rs b/src/library.rs index 17914da..6bff014 100644 --- a/src/library.rs +++ b/src/library.rs @@ -1,15 +1,22 @@ -use nanoserde::DeJson; use std::{ collections::HashSet, hash::{Hash, Hasher}, }; +use nanoserde::DeJson; + #[derive(Debug, Clone, DeJson)] pub struct Library { artists: HashSet, } impl Library { + pub fn new(artists: impl IntoIterator) -> Library { + Library { + artists: artists.into_iter().collect(), + } + } + fn all_covers(&self) -> HashSet<&Song> { self.artists .iter() @@ -28,7 +35,7 @@ impl Library { } #[derive(Debug, Clone, PartialEq, Eq, DeJson)] -struct Artist { +pub struct Artist { name: String, members: HashSet, catalog: HashSet, @@ -41,6 +48,18 @@ impl Hash for Artist { } impl Artist { + pub fn new( + name: String, + members: impl IntoIterator, + catalog: impl IntoIterator, + ) -> Artist { + Artist { + name, + members: members.into_iter().collect(), + catalog: catalog.into_iter().collect(), + } + } + fn covered_songs(&self) -> HashSet<&Song> { self.catalog .iter() @@ -64,6 +83,18 @@ impl Hash for Song { } impl Song { + pub fn new( + title: String, + performer: String, + writers: impl IntoIterator, + ) -> Song { + Song { + title, + performer, + writers: writers.into_iter().collect(), + } + } + fn is_library_cover(&self, library: &Library) -> bool { library .artists @@ -187,13 +218,13 @@ mod tests { fn library() -> Library { Library { - artists: HashSet::from([bm(), ts(), bon_iver()]), + artists: [bm(), ts(), bon_iver()].into(), } } #[test] fn test_covered_songs() { - assert!(bm().covered_songs() == HashSet::from([&love_story("black midi"), &schizoid_man()])) + assert!(bm().covered_songs() == [&love_story("black midi"), &schizoid_man()].into()) } #[test] @@ -203,7 +234,7 @@ mod tests { #[test] fn test_find_covers() { - assert!(library().find_library_covers() == HashSet::from([&love_story("black midi")])) + assert!(library().find_library_covers() == [&love_story("black midi")].into()) } #[test] diff --git a/src/main.rs b/src/main.rs index 1ff82a5..9e87683 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,15 @@ -use nanoserde::DeJson; use std::{env, fs}; -mod library; +use nanoserde::DeJson; + +use cover::library::Library; fn main() { let file_path = env::args() .nth(1) .expect("Usage: cover path/to/library.json"); let json = fs::read_to_string(file_path).expect("Failed to open library file"); - let library: library::Library = + let library: Library = DeJson::deserialize_json(&json).expect("Failed to deserialize library file"); for cover in library.find_library_covers() { println!("Found cover!");