Set up track listing with the memory database

This commit is contained in:
Savanni D'Gerinel 2023-02-16 09:13:01 -05:00
parent 20703ca921
commit 5c61a48006
2 changed files with 86 additions and 1 deletions

View File

@ -1,4 +1,5 @@
use crate::{
audio::TrackInfo,
database::{Database, MemoryIndex, MusicIndex},
Error, FatalError,
};
@ -168,6 +169,10 @@ impl Core {
})
}
pub fn list_tracks<'a>(&'a self) -> Flow<Vec<TrackInfo>, FatalError, Error> {
self.db.list_tracks().map_err(Error::DatabaseError)
}
pub fn exit(&self) {
let _ = self.control_tx.send(ControlMsg::Exit);
/*
@ -180,9 +185,78 @@ impl Core {
#[cfg(test)]
mod test {
use super::*;
use crate::audio::{TrackId, TrackInfo};
use std::collections::HashSet;
fn with_example_index<F>(f: F)
where
F: Fn(Core),
{
let index = MemoryIndex::new();
index.add_track(TrackInfo {
id: TrackId::from("/home/savanni/Track 1.mp3".to_owned()),
track_number: None,
name: None,
album: None,
artist: None,
});
index.add_track(TrackInfo {
id: TrackId::from("/home/savanni/Track 2.mp3".to_owned()),
track_number: None,
name: None,
album: None,
artist: None,
});
index.add_track(TrackInfo {
id: TrackId::from("/home/savanni/Track 3.mp3".to_owned()),
track_number: None,
name: None,
album: None,
artist: None,
});
index.add_track(TrackInfo {
id: TrackId::from("/home/savanni/Track 4.mp3".to_owned()),
track_number: None,
name: None,
album: None,
artist: None,
});
index.add_track(TrackInfo {
id: TrackId::from("/home/savanni/Track 5.mp3".to_owned()),
track_number: None,
name: None,
album: None,
artist: None,
});
match Core::new(Arc::new(index)) {
Flow::Ok(core) => f(core),
Flow::Err(error) => panic!("{:?}", error),
Flow::Fatal(error) => panic!("{:?}", error),
}
}
#[test]
fn it_lists_tracks() {
let index = MemoryIndex::new();
with_example_index(|core| match core.list_tracks() {
Flow::Ok(tracks) => {
let track_ids = tracks
.iter()
.map(|t| t.id.clone())
.collect::<HashSet<TrackId>>();
assert_eq!(track_ids.len(), 5);
assert_eq!(
track_ids,
HashSet::from([
TrackId::from("/home/savanni/Track 1.mp3".to_owned()),
TrackId::from("/home/savanni/Track 2.mp3".to_owned()),
TrackId::from("/home/savanni/Track 3.mp3".to_owned()),
TrackId::from("/home/savanni/Track 4.mp3".to_owned()),
TrackId::from("/home/savanni/Track 5.mp3".to_owned()),
])
);
}
Flow::Fatal(err) => panic!("fatal error: {:?}", err),
Flow::Err(err) => panic!("error: {:?}", err),
})
}
}

View File

@ -23,6 +23,7 @@ pub trait MusicIndex: Sync + Send {
fn add_track(&self, track: TrackInfo) -> Flow<(), FatalError, DatabaseError>;
fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
fn get_track_info(&self, id: &TrackId) -> Flow<Option<TrackInfo>, FatalError, DatabaseError>;
fn list_tracks<'a>(&'a self) -> Flow<Vec<TrackInfo>, FatalError, DatabaseError>;
}
pub struct MemoryIndex {
@ -60,6 +61,16 @@ impl MusicIndex for MemoryIndex {
};
ok(track)
}
fn list_tracks<'a>(&'a self) -> Flow<Vec<TrackInfo>, FatalError, DatabaseError> {
ok(self
.tracks
.read()
.unwrap()
.values()
.cloned()
.collect::<Vec<TrackInfo>>())
}
}
pub struct ManagedConnection<'a> {