Skip to content

Commit 14fcd7a

Browse files
committed
Implement print_top songs of album
1 parent ef0cefe commit 14fcd7a

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/display.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,30 @@ pub fn print_top(entries: &Vec<SongEntry>, asp: Aspect, num: usize) {
2828
pub fn print_top_from_artist(entries: &Vec<SongEntry>, asp: Aspect, artist: &Artist, num: usize) {
2929
match asp {
3030
Aspect::Songs => {
31-
println!("=== TOP {} SONGS FROM {} ===", num, artist.name);
31+
println!("=== TOP {} SONGS FROM {} ===", num, artist);
3232
print_top_helper(gather_songs_with_artist(entries, artist), num);
3333
println!();
3434
}
3535
Aspect::Albums => {
36-
println!("=== TOP {} ALBUMS FROM {} ===", num, artist.name);
36+
println!("=== TOP {} ALBUMS FROM {} ===", num, artist);
3737
print_top_helper(gather_albums_with_artist(entries, artist), num);
3838
println!();
3939
}
4040
_ => println!("gay"),
4141
}
4242
}
4343

44+
pub fn print_top_from_album(entries: &Vec<SongEntry>, asp: Aspect, album: &Album, num: usize) {
45+
match asp {
46+
Aspect::Songs => {
47+
println!("=== TOP {} SONGS FROM {} ===", num, album);
48+
print_top_helper(gather_songs_with_album(entries, album), num);
49+
println!();
50+
}
51+
_ => println!("gay"),
52+
}
53+
}
54+
4455
fn print_top_helper<T: Music>(music_dict: HashMap<T, u32>, num: usize) {
4556
// https://stackoverflow.com/q/34555837/6694963
4657
let mut music_vec: Vec<(&T, &u32)> = music_dict.iter().collect();
@@ -116,6 +127,32 @@ fn gather_songs_with_artist(entries: &Vec<SongEntry>, art: &Artist) -> HashMap<S
116127
songs
117128
}
118129

130+
fn gather_songs_with_album(entries: &Vec<SongEntry>, alb: &Album) -> HashMap<Song, u32> {
131+
let mut songs: HashMap<Song, u32> = HashMap::new();
132+
133+
for entry in entries {
134+
let artist = Artist {
135+
name: entry.artist.clone(),
136+
};
137+
let album = Album {
138+
name: entry.album.clone(),
139+
artist: artist.clone(),
140+
};
141+
if album != *alb {
142+
continue;
143+
}
144+
let song = Song {
145+
name: entry.track.clone(),
146+
album: album.clone(),
147+
id: entry.id.clone(),
148+
};
149+
150+
*songs.entry(song).or_insert(0) += 1;
151+
}
152+
153+
songs
154+
}
155+
119156
fn gather_albums(entries: &Vec<SongEntry>) -> HashMap<Album, u32> {
120157
let mut albums: HashMap<Album, u32> = HashMap::new();
121158

src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use crate::types::Aspect;
77

88
fn main() {
99
// this is only temporary -> later on these files should be added by CLI args
10+
// or both options!
11+
// benchmark! =>
12+
// 1) paths as CLI args
13+
// 2) paths as part of compiled source code!
1014
let root = "/home/filip/Other/SpotifyData/2021-07/";
1115
let paths: Vec<String> = vec![
1216
format!("{}endsong_0.json", root),
@@ -29,4 +33,12 @@ fn main() {
2933
};
3034
display::print_top_from_artist(&entries, Aspect::Songs, &powerwolf, 10);
3135
display::print_top_from_artist(&entries, Aspect::Albums, &powerwolf, 10);
36+
37+
let coat = types::Album {
38+
name: String::from("Coat of Arms"),
39+
artist: types::Artist {
40+
name: "Sabaton".to_string(),
41+
},
42+
};
43+
display::print_top_from_album(&entries, Aspect::Songs, &coat, 50);
3244
}

0 commit comments

Comments
 (0)