|
1 | 1 | use std::collections::HashMap;
|
2 | 2 |
|
3 | 3 | use crate::types::Aspect;
|
| 4 | +use crate::types::AspectFull; |
4 | 5 | use crate::types::Music;
|
5 | 6 | use crate::types::SongEntry;
|
6 | 7 | use crate::types::{Album, Artist, Song};
|
@@ -71,7 +72,7 @@ fn print_top_helper<T: Music>(music_dict: HashMap<T, u32>, num: usize) {
|
71 | 72 | let mus = music_vec.get(i).unwrap();
|
72 | 73 | let m = mus.0;
|
73 | 74 | let n = mus.1;
|
74 |
| - println!("#{}\t{} => {}", i + 1, m, n) |
| 75 | + println!("#{}\t{} | {} plays", i + 1, m, n) |
75 | 76 | }
|
76 | 77 | }
|
77 | 78 |
|
@@ -205,3 +206,111 @@ fn gather_artists(entries: &Vec<SongEntry>) -> HashMap<Artist, u32> {
|
205 | 206 |
|
206 | 207 | artists
|
207 | 208 | }
|
| 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 | +} |
0 commit comments