From 5c61a48006217371624d3ac428fe74d6c639c82a Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 16 Feb 2023 09:13:01 -0500 Subject: [PATCH] Set up track listing with the memory database --- music-player/server/src/core.rs | 76 ++++++++++++++++++++++++++++- music-player/server/src/database.rs | 11 +++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/music-player/server/src/core.rs b/music-player/server/src/core.rs index 107247e..a6eb170 100644 --- a/music-player/server/src/core.rs +++ b/music-player/server/src/core.rs @@ -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, 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) + 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::>(); + 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), + }) } } diff --git a/music-player/server/src/database.rs b/music-player/server/src/database.rs index 125231e..72a05ac 100644 --- a/music-player/server/src/database.rs +++ b/music-player/server/src/database.rs @@ -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, FatalError, DatabaseError>; + fn list_tracks<'a>(&'a self) -> Flow, FatalError, DatabaseError>; } pub struct MemoryIndex { @@ -60,6 +61,16 @@ impl MusicIndex for MemoryIndex { }; ok(track) } + + fn list_tracks<'a>(&'a self) -> Flow, FatalError, DatabaseError> { + ok(self + .tracks + .read() + .unwrap() + .values() + .cloned() + .collect::>()) + } } pub struct ManagedConnection<'a> {