Skip to content

Commit

Permalink
Improve logging for missing playlist entries
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed Jan 2, 2025
1 parent 817f2a2 commit 658f789
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src-native/library_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ fn is_false(value: &bool) -> bool {

// These are used to give each playlist entry an ID. This is for example helpful to keep track of a user's selection. These IDs are unique across the entire library, so that it works for folders folders.
pub type ItemId = u32;
/// Track IDs indexed by item IDs
pub static TRACK_ID_MAP: RwLock<Vec<String>> = RwLock::new(Vec::new());

pub fn new_item_ids_from_track_ids(track_ids: &[TrackID]) -> Vec<ItemId> {
Expand All @@ -402,6 +403,11 @@ pub fn get_track_ids_from_item_ids(playlist_item_ids: &[ItemId]) -> Vec<TrackID>
.collect()
}

pub fn get_track_id_from_item_id(playlist_item_id: ItemId) -> TrackID {
let playlist_track_id_map = TRACK_ID_MAP.read().unwrap();
playlist_track_id_map[playlist_item_id as usize].clone()
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[napi(object)]
pub struct Playlist {
Expand Down
19 changes: 12 additions & 7 deletions src-native/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::library_types::{ItemId, Library, Track, TRACK_ID_MAP};
use crate::page::TracksPageOptions;
use crate::playlists::get_tracklist_item_ids;
use alphanumeric_sort::compare_str;
use anyhow::Result;
use anyhow::{Context, Result};
use std::cmp::Ordering;
use std::time::Instant;

Expand Down Expand Up @@ -97,15 +97,20 @@ pub fn sort(options: TracksPageOptions, library: &Library) -> Result<Vec<ItemId>
let id_map = TRACK_ID_MAP.read().unwrap();
let tracks = library.get_tracks();

let mut items: Vec<_> = get_tracklist_item_ids(library, &options.playlist_id)?
let items: Result<Vec<SortItem>> = get_tracklist_item_ids(library, &options.playlist_id)?
.into_iter()
.map(|id| SortItem {
item_id: id,
track: tracks
.get(&id_map[id as usize])
.expect("Track ID non-existant"),
.enumerate()
.map(|(i, id)| {
Ok(SortItem {
item_id: id,
track: tracks.get(&id_map[id as usize]).context(format!(
"Track {i} ({}) does not exist",
id_map[id as usize]
))?,
})
})
.collect();
let mut items = items?;
let item_count = items.len();

if options.sort_key == "index" {
Expand Down

0 comments on commit 658f789

Please sign in to comment.