Skip to content

Commit

Permalink
Add associative method wrappers for find_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Jul 11, 2022
1 parent d6e0263 commit 99a3680
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 30 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 6 additions & 11 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,14 @@ pub fn find_album(
album_name: String,
artist_name: String,
) -> Option<Album> {
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
Expand Down Expand Up @@ -540,8 +537,6 @@ pub fn find_song(
}
}

println!("{:?}", song_versions);

if !song_versions.is_empty() {
return Some(song_versions);
}
Expand Down
23 changes: 7 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
}
70 changes: 68 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SongEntry> 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<Artist> {
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<Album> {
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<Song> {
display::find_song_from_album(self, song_name, album_name, artist_name)
}

/// Searches the dataset for multiple versions of a song
///
/// Returns a [Vec<Song>] 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<Vec<Song>> {
display::find_song(self, song_name, artist_name)
}
}

/// A more specific version of [crate::parse::Entry]
Expand Down
4 changes: 3 additions & 1 deletion stuff/ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit 99a3680

Please sign in to comment.