diff --git a/README.md b/README.md index e09c0de..df28827 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # rusty-endsong-parser + Better, more performant version of https://github.com/Filip-Tomasko/endsong-parser-python written in Rust + +## Assumptions made in this program + +- there are no two different artists with the same name + - this is obviously not true, but the only way (that I can think of) + to work around that is use the Spotify API diff --git a/src/display.rs b/src/display.rs index 091e53d..443ae51 100644 --- a/src/display.rs +++ b/src/display.rs @@ -454,17 +454,14 @@ pub fn find_album( album_name: String, artist_name: String, ) -> Option { - let usr_album = Album::new(album_name, artist_name); + // .to_lowercase() so that user input capitalization doesn't matter + let usr_album = Album::new(album_name.to_lowercase(), artist_name.to_lowercase()); for entry in entries { - // .to_lowercase() so that user input capitalization doesn't matter - if entry.album.to_lowercase().eq(&usr_album.name) - && entry - .artist - .to_lowercase() - .eq(&usr_album.artist.name.to_lowercase()) - { - return Some(usr_album); + if Album::new(entry.album.to_lowercase(), entry.artist.to_lowercase()).eq(&usr_album) { + // but here so that the version with proper + // capitalization is returned + return Some(Album::new(entry.album.clone(), entry.artist.clone())); } } None @@ -540,8 +537,6 @@ pub fn find_song( } } - println!("{:?}", song_versions); - if !song_versions.is_empty() { return Some(song_versions); } diff --git a/src/main.rs b/src/main.rs index 29f161c..1b8a0d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,30 +64,21 @@ fn main() { println!(); entries.print_aspect(AspectFull::Song(&final_solution)); - dbg!(display::find_artist(&entries, "Sabaton".to_string())); - dbg!(display::find_album( - &entries, - "Coat of Arms".to_string(), - "Sabaton".to_string() - )); - dbg!(display::find_song_from_album( - &entries, + dbg!(entries.find().artist("Sabaton".to_string())); + dbg!(entries + .find() + .album("COAT OF ARMS".to_string(), "sabaton".to_string(),)); + dbg!(entries.find().song_from_album( "The FINAL SOLutiOn".to_string(), "COAT OF ARMS".to_string(), "sabaton".to_string(), )); - dbg!(display::find_artist( - &entries, - "daduasdy712e qyw7".to_string() - )); + dbg!(entries.find().artist("daduasdy712e qyw7".to_string())); // here to test whether it finds the multiple versions of this song (from many albums) // btw.. fuck Wizardthrone for releasing singles one after the other with each // containing all the songs that were in the previous one ffs - dbg!(display::find_song( - &entries, + dbg!(entries.find().song( "Frozen Winds Of Thyraxia".to_string(), "Wizardthrone".to_string(), )); - - // entries.print_aspect(AspectFull::Artist(&types::Artist::from_str("Wizardthrone"))); } diff --git a/src/types.rs b/src/types.rs index c958190..0b6872a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -231,8 +231,74 @@ impl SongEntries { display::print_aspect(self, asp) } - // Searches for if the given aspect exists within the dataset - // pub fn find() {} + /// Adds search capability + /// + /// Use with methods from [Find]: `.artist()`, `.album()`, + /// `.song_from_album()` and `.song()` + pub fn find(&self) -> Find { + Find(self) + } +} + +/// Used by [SongEntries] as a wrapper for +/// [display::find_artist()], [display::find_album()], +/// [display::find_song_from_album()] and [display::find_song()] +/// +/// # Examples +/// +/// ``` +/// let entries = SongEntries::new(paths); +/// dbg!(entries.find().artist("Sabaton".to_string())); +/// ``` +pub struct Find<'a>(&'a SongEntries); + +// https://users.rust-lang.org/t/how-can-i-return-reference-of-the-struct-field/36325/2 +// so that when you use &self it refers to &self.0 (SongEntries, +// which itself refers to Vec xDD +impl<'a> std::ops::Deref for Find<'a> { + type Target = SongEntries; + fn deref(&self) -> &SongEntries { + self.0 + } +} + +impl<'a> Find<'a> { + /// Searches the entries for if the given artist exists in the dataset + /// + /// Wrapper for [display::find_artist()] + pub fn artist(&self, artist_name: String) -> Option { + display::find_artist(self, artist_name) + } + + /// Searches the entries for if the given album exists in the dataset + /// + /// Wrapper for [display::find_album()] + pub fn album(&self, album_name: String, artist_name: String) -> Option { + display::find_album(self, album_name, artist_name) + } + + /// Searches the entries for if the given song (in that specific album) + /// exists in the dataset + /// + /// Wrapper for [display::find_song_from_album()] + pub fn song_from_album( + &self, + song_name: String, + album_name: String, + artist_name: String, + ) -> Option { + display::find_song_from_album(self, song_name, album_name, artist_name) + } + + /// Searches the dataset for multiple versions of a song + /// + /// Returns a [Vec] containing an instance + /// of [Song] for every album it's been found in + /// + /// Wrapper for [display::find_song()] + pub fn song(&self, song_name: String, artist_name: String) -> Option> { + display::find_song(self, song_name, artist_name) + } } /// A more specific version of [crate::parse::Entry] diff --git a/stuff/ideas.md b/stuff/ideas.md index 2fe4484..08efc93 100644 --- a/stuff/ideas.md +++ b/stuff/ideas.md @@ -14,7 +14,7 @@ or `entries.find().song("The Final Solution", "Coat of Arms", "Sabaton")` maybe also `entries.print_top(Aspect::Songs, 10)` instead of the current `display::print_top(&entries, Aspect::Songs, 10)`? -=> partially implemented! +=> implemented! ## Pure Theory @@ -50,3 +50,5 @@ maybe also `entries.print_top(Aspect::Songs, 10)` instead of the current - [rayon](https://github.com/rayon-rs/rayon) for parallel iterator work!!! - [from this jonhoo talk](https://youtu.be/DnT-LUQgc7s?t=1516) - [tui-rs](https://github.com/fdehau/tui-rs) for terminal UI?!? +- do something about different artists having the same name... + - but that would require the use of the Spotify API -> inefficient