Skip to content

Commit

Permalink
Replace find::songs_from_album with helper fns Map->Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Aug 21, 2024
1 parent 8413d2e commit 6ab3823
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
2 changes: 1 addition & 1 deletion endsong_ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn test_two(entries: &SongEntries) {

let ct = Album::new("Waking The Fallen", "Avenged Sevenfold");
let mut alb_dur = TimeDelta::try_seconds(0).unwrap();
let ct_songs = entries.find().songs_from_album(&ct);
let ct_songs = get_sorted_list(gather::songs_from(entries, &ct));
for song in &ct_songs {
println!(
"{} - {}",
Expand Down
6 changes: 1 addition & 5 deletions endsong_ui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,7 @@ fn match_plot_artist_albums(
let art = read_artist(rl, entries)?;

let albums_map = gather::albums_from_artist(entries, &art);
let albums = albums_map
.iter()
.sorted_unstable_by_key(|t| (std::cmp::Reverse(t.1), t.0))
.map(|(aspect, _)| aspect)
.collect_vec();
let albums = get_sorted_ref_list(&albums_map);

let mut traces = vec![];
for (count, alb) in albums.into_iter().enumerate() {
Expand Down
10 changes: 0 additions & 10 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,4 @@ impl<'a> Find<'a> {
pub fn song(&self, song_name: &str, artist_name: &str) -> Option<Vec<Song>> {
find::song(self.0, song_name, artist_name)
}

/// Returns a [`Vec<Song>`] with all the songs in the given album
///
/// # Panics
///
/// Panics if `album` is not in the dataset
#[must_use]
pub fn songs_from_album(&self, album: &Album) -> Vec<Song> {
find::songs_from_album(self.0, album)
}
}
32 changes: 18 additions & 14 deletions src/find.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
//! Module responsible for finding artists, albums and songs in the dataset
//!
//! Use functions here instead of manually creating aspects to make sure
//! they actually exist in the dataset!
//!
//! ```
//! use endsong::prelude::*;
//!
//! // create SongEntries from a single file
//! let paths = vec![format!(
//! "{}/stuff/example_endsong/endsong_0.json",
//! std::env::current_dir().unwrap().display()
//! )];
//! let entries = SongEntries::new(&paths).unwrap();
//!
//! // example artist
//! let artist: Artist = entries.find().artist("sabaTON").unwrap().remove(0);
//! assert_eq!(artist, Artist::new("Sabaton"));
//! ```
use itertools::Itertools;

Expand Down Expand Up @@ -119,20 +137,6 @@ pub fn song(entries: &[SongEntry], song_name: &str, artist_name: &str) -> Option
Some(song_versions)
}

/// Returns a [`Vec<Song>`] with all the songs in the given album
///
/// # Panics
///
/// Panics if `album` is not in the dataset
pub fn songs_from_album(entries: &[SongEntry], album: &Album) -> Vec<Song> {
entries
.iter()
.filter(|entry| album.is_entry(entry))
.unique()
.map(Song::from)
.collect_vec()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 13 additions & 1 deletion src/gather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
//! Using [`&SongEntries`][crate::entry::SongEntries] is also possible for data for the whole dataset
//! since it implements [`Deref`][std::ops::Deref] to the [`Vec<SongEntry>`] it contains.
//!
//! Use [`get_sorted_list`][crate::get_sorted_list] and
//! [`get_sorted_ref_list`][crate::get_sorted_ref_list] to transform the [`HashMap`]s
//! from the functions here into [`Vec`]s sorted by playcount
//!
//! # Examples
//! ```rust
//! use endsong::prelude::*;
//! use itertools::Itertools;
//!
//! // create SongEntries from a single file
//! let paths = vec![format!(
Expand All @@ -19,7 +24,7 @@
//! let entries = SongEntries::new(&paths).unwrap();
//!
//! // example artist
//! let artist = Artist::new("Sabaton");
//! let artist: Artist = entries.find().artist("Sabaton").unwrap().remove(0);
//!
//! // get all albums from the artist with their plays
//! let _ = gather::albums_from_artist(&entries, &artist);
Expand All @@ -28,6 +33,13 @@
//! let start_date = parse_date("2020-11-14").unwrap();
//! let end_date = parse_date("now").unwrap();
//! let _ = gather::albums_from_artist(entries.between(&start_date, &end_date), &artist);
//!
//! // to get a list of albums from the artist sorted
//! // primarily by their playcount descending
//! // and then alphabetically
//! let albums_map = gather::albums_from_artist(&entries, &artist);
//! let albums: Vec<&Album> = get_sorted_ref_list(&albums_map);
//! let albums_owned: Vec<Album> = get_sorted_list(gather::albums_from_artist(&entries, &artist));
//! ```
use std::collections::HashMap;
Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ pub mod prelude {
pub use crate::aspect::{Album, Artist, Song};
pub use crate::aspect::{HasSongs, Music};

pub use crate::get_sorted_list;
pub use crate::get_sorted_ref_list;
pub use crate::parse_date;

// time and date related
pub use chrono::{DateTime, Local, NaiveDateTime, TimeDelta, TimeZone};
}

use std::collections::HashMap;

use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
use itertools::Itertools;

use aspect::Music;

/// Converts a `YYYY-MM-DD` string to a [`DateTime<Local>`]
/// in the context of the [`Local`] timezone
///
Expand Down Expand Up @@ -102,6 +110,28 @@ pub fn parse_date(date: &str) -> Result<DateTime<Local>, chrono::format::ParseEr
}
}

/// Makes a list of the aspect sorted by its playcount descending
/// and then the name alphabetically
#[allow(clippy::implicit_hasher)]
#[must_use]
pub fn get_sorted_list<Asp: Music>(map: HashMap<Asp, usize>) -> Vec<Asp> {
map.into_iter()
.sorted_unstable_by_key(|t: &(Asp, usize)| (std::cmp::Reverse(t.1), t.0.clone()))
.map(|(aspect, _)| aspect)
.collect()
}

/// Makes a list of references to the aspect sorted by its playcount descending
/// and then the name alphabetically
#[allow(clippy::implicit_hasher)]
#[must_use]
pub fn get_sorted_ref_list<Asp: Music>(map: &HashMap<Asp, usize>) -> Vec<&Asp> {
map.iter()
.sorted_unstable_by_key(|t: &(&Asp, &usize)| (std::cmp::Reverse(t.1), t.0))
.map(|(aspect, _)| aspect)
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 6ab3823

Please sign in to comment.