Index related genres

This commit is contained in:
Antoine Gersant 2024-09-30 00:27:49 -07:00
parent 143da76673
commit 232eb7ac12
3 changed files with 31 additions and 0 deletions

View file

@ -24,6 +24,7 @@ pub struct Genre {
pub header: GenreHeader, pub header: GenreHeader,
pub albums: Vec<AlbumHeader>, pub albums: Vec<AlbumHeader>,
pub artists: Vec<ArtistHeader>, pub artists: Vec<ArtistHeader>,
pub related_genres: HashMap<String, u32>,
pub songs: Vec<Song>, pub songs: Vec<Song>,
} }
@ -233,10 +234,19 @@ impl Collection {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// TODO sort songs // TODO sort songs
let related_genres = genre
.related_genres
.iter()
.map(|(genre_key, song_count)| {
(strings.resolve(&genre_key.0).to_owned(), *song_count)
})
.collect();
Genre { Genre {
header: make_genre_header(genre, strings), header: make_genre_header(genre, strings),
albums, albums,
artists, artists,
related_genres,
songs, songs,
} }
}) })
@ -439,6 +449,7 @@ impl Builder {
name: *name, name: *name,
albums: HashSet::new(), albums: HashSet::new(),
artists: HashSet::new(), artists: HashSet::new(),
related_genres: HashMap::new(),
songs: Vec::new(), songs: Vec::new(),
}); });
@ -466,6 +477,23 @@ impl Builder {
virtual_path: song.virtual_path, virtual_path: song.virtual_path,
}); });
} }
let genres = song.genres.clone();
for genre in &genres {
for other_genre in &genres {
if genre == other_genre {
continue;
}
let Some(genre) = self.genres.get_mut(&GenreKey(*genre)) else {
continue;
};
genre
.related_genres
.entry(GenreKey(*other_genre))
.and_modify(|n| *n += 1)
.or_insert(1);
}
}
} }
} }

View file

@ -21,6 +21,7 @@ pub struct Genre {
pub name: Spur, pub name: Spur,
pub albums: HashSet<AlbumKey>, pub albums: HashSet<AlbumKey>,
pub artists: HashSet<ArtistKey>, pub artists: HashSet<ArtistKey>,
pub related_genres: HashMap<GenreKey, u32>,
pub songs: Vec<SongKey>, pub songs: Vec<SongKey>,
} }

View file

@ -334,12 +334,14 @@ impl From<index::GenreHeader> for GenreHeader {
pub struct Genre { pub struct Genre {
#[serde(flatten)] #[serde(flatten)]
pub header: GenreHeader, pub header: GenreHeader,
pub related_genres: HashMap<String, u32>,
} }
impl From<index::Genre> for Genre { impl From<index::Genre> for Genre {
fn from(genre: index::Genre) -> Self { fn from(genre: index::Genre) -> Self {
Self { Self {
header: GenreHeader::from(genre.header), header: GenreHeader::from(genre.header),
related_genres: genre.related_genres,
} }
} }
} }