Skip to content

Commit ca9aacf

Browse files
committed
Implement print_aspect
1 parent a44983d commit ca9aacf

File tree

3 files changed

+138
-7
lines changed

3 files changed

+138
-7
lines changed

src/display.rs

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22

33
use crate::types::Aspect;
4+
use crate::types::AspectFull;
45
use crate::types::Music;
56
use crate::types::SongEntry;
67
use crate::types::{Album, Artist, Song};
@@ -71,7 +72,7 @@ fn print_top_helper<T: Music>(music_dict: HashMap<T, u32>, num: usize) {
7172
let mus = music_vec.get(i).unwrap();
7273
let m = mus.0;
7374
let n = mus.1;
74-
println!("#{}\t{} => {}", i + 1, m, n)
75+
println!("#{}\t{} | {} plays", i + 1, m, n)
7576
}
7677
}
7778

@@ -205,3 +206,111 @@ fn gather_artists(entries: &Vec<SongEntry>) -> HashMap<Artist, u32> {
205206

206207
artists
207208
}
209+
210+
struct ArtistPlays(Artist, u32);
211+
struct AlbumPlays(Album, u32);
212+
struct SongPlays(Song, u32);
213+
214+
pub fn print_aspect(entries: &Vec<SongEntry>, asp: AspectFull) {
215+
match asp {
216+
AspectFull::Artist(art) => {
217+
println!("=== {} | {} plays ===", art, gather_artist(entries, art).1);
218+
print_artist(entries, gather_albums_with_artist(entries, art));
219+
}
220+
AspectFull::Album(alb) => {
221+
println!("=== {} | {} plays ===", alb, gather_album(entries, alb).1);
222+
print_album(gather_songs_with_album(entries, alb));
223+
}
224+
AspectFull::Song(son) => {
225+
let son = gather_song(entries, son);
226+
println!("{} | {} plays", son.0, son.1);
227+
}
228+
}
229+
}
230+
231+
fn gather_artist(entries: &Vec<SongEntry>, art: &Artist) -> ArtistPlays {
232+
let mut artist_asp = ArtistPlays(art.clone(), 0);
233+
234+
for entry in entries {
235+
let artist = Artist {
236+
name: entry.artist.clone(),
237+
};
238+
239+
if artist == *art {
240+
artist_asp.1 += 1;
241+
}
242+
}
243+
244+
artist_asp
245+
}
246+
247+
fn gather_album(entries: &Vec<SongEntry>, alb: &Album) -> AlbumPlays {
248+
let mut album_asp = AlbumPlays(alb.clone(), 0);
249+
250+
for entry in entries {
251+
let artist = Artist {
252+
name: entry.artist.clone(),
253+
};
254+
let album = Album {
255+
name: entry.album.clone(),
256+
artist: artist.clone(),
257+
};
258+
259+
if album == *alb {
260+
album_asp.1 += 1;
261+
}
262+
}
263+
264+
album_asp
265+
}
266+
267+
fn gather_song(entries: &Vec<SongEntry>, son: &Song) -> SongPlays {
268+
let mut song_asp = SongPlays(son.clone(), 0);
269+
270+
for entry in entries {
271+
let artist = Artist {
272+
name: entry.artist.clone(),
273+
};
274+
let album = Album {
275+
name: entry.album.clone(),
276+
artist: artist.clone(),
277+
};
278+
let song = Song {
279+
name: entry.track.clone(),
280+
album: album.clone(),
281+
// id: entry.id.clone(),
282+
};
283+
284+
if song == *son {
285+
song_asp.1 += 1;
286+
}
287+
}
288+
289+
song_asp
290+
}
291+
292+
fn print_artist(entries: &Vec<SongEntry>, artist: HashMap<Album, u32>) {
293+
let mut artist_vec: Vec<(&Album, &u32)> = artist.iter().collect();
294+
artist_vec.sort_by(|a, b| b.1.cmp(a.1));
295+
296+
for i in 0..artist_vec.len() {
297+
let alb = artist_vec.get(i).unwrap().0;
298+
let mus = gather_songs_with_album(entries, alb);
299+
// calling gather_album here is unnecessary work
300+
// it should add up the total plays somehwere else
301+
println!("--- {} | {} plays ---", alb, gather_album(entries, alb).1);
302+
print_album(mus);
303+
}
304+
}
305+
306+
fn print_album(album: HashMap<Song, u32>) {
307+
let mut album_vec: Vec<(&Song, &u32)> = album.iter().collect();
308+
album_vec.sort_by(|a, b| b.1.cmp(a.1));
309+
310+
for i in 0..album_vec.len() {
311+
let mus = album_vec.get(i).unwrap();
312+
let m = mus.0;
313+
let n = mus.1;
314+
println!("#{}\t{} => {}", i + 1, m, n)
315+
}
316+
}

src/main.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod parse;
44
mod types;
55

66
use crate::types::Aspect;
7+
use crate::types::AspectFull;
78

89
fn main() {
910
// this is only temporary -> later on these files should be added by CLI args
@@ -14,12 +15,12 @@ fn main() {
1415
let root = "/home/filip/Other/SpotifyData/2021-07/";
1516
let paths: Vec<String> = vec![
1617
format!("{}endsong_0.json", root),
17-
format!("{}endsong_1.json", root),
18-
format!("{}endsong_2.json", root),
19-
format!("{}endsong_3.json", root),
20-
format!("{}endsong_4.json", root),
21-
format!("{}endsong_5.json", root),
22-
format!("{}endsong_6.json", root),
18+
// format!("{}endsong_1.json", root),
19+
// format!("{}endsong_2.json", root),
20+
// format!("{}endsong_3.json", root),
21+
// format!("{}endsong_4.json", root),
22+
// format!("{}endsong_5.json", root),
23+
// format!("{}endsong_6.json", root),
2324
];
2425

2526
let entries = parse::parse(paths);
@@ -41,4 +42,19 @@ fn main() {
4142
},
4243
};
4344
display::print_top_from_album(&entries, Aspect::Songs, &coat, 50);
45+
46+
let final_solution = types::Song {
47+
name: String::from("The Final Solution"),
48+
album: coat.clone(),
49+
};
50+
display::print_aspect(
51+
&entries,
52+
AspectFull::Artist(&types::Artist {
53+
name: "Sabaton".to_string(),
54+
}),
55+
);
56+
println!();
57+
display::print_aspect(&entries, AspectFull::Album(&coat));
58+
println!();
59+
display::print_aspect(&entries, AspectFull::Song(&final_solution));
4460
}

src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ use std::fmt::Display;
33

44
use chrono::DateTime;
55

6+
pub enum AspectFull<'a> {
7+
Artist(&'a Artist),
8+
Album(&'a Album),
9+
Song(&'a Song),
10+
}
11+
612
pub enum Aspect {
713
Artists,
814
Albums,

0 commit comments

Comments
 (0)