Compare commits
4 Commits
4163ccb5c2
...
86708ebdb2
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 86708ebdb2 | |
Savanni D'Gerinel | 5c61a48006 | |
Savanni D'Gerinel | 20703ca921 | |
Savanni D'Gerinel | 0d00b17cb4 |
|
@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Lum
|
||||||
//! Where the sled.rs library uses `Result<Result<A, Error>, FatalError>`, these are a little hard to
|
//! Where the sled.rs library uses `Result<Result<A, Error>, FatalError>`, these are a little hard to
|
||||||
//! work with. This library works out a set of utility functions that allow us to work with the
|
//! work with. This library works out a set of utility functions that allow us to work with the
|
||||||
//! nested errors in the same way as a regular Result.
|
//! nested errors in the same way as a regular Result.
|
||||||
use std::error::Error;
|
use std::{error::Error, fmt};
|
||||||
|
|
||||||
/// Implement this trait for the application's fatal errors.
|
/// Implement this trait for the application's fatal errors.
|
||||||
///
|
///
|
||||||
|
@ -110,6 +110,37 @@ impl<A, FE, E> From<Result<A, E>> for Flow<A, FE, E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<A, FE, E> fmt::Debug for Flow<A, FE, E>
|
||||||
|
where
|
||||||
|
A: fmt::Debug,
|
||||||
|
FE: fmt::Debug,
|
||||||
|
E: fmt::Debug,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Flow::Ok(val) => f.write_fmt(format_args!("Flow::Ok {:?}", val)),
|
||||||
|
Flow::Err(err) => f.write_fmt(format_args!("Flow::Err {:?}", err)),
|
||||||
|
Flow::Fatal(err) => f.write_fmt(format_args!("Flow::Fatal {:?}", err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, FE, E> PartialEq for Flow<A, FE, E>
|
||||||
|
where
|
||||||
|
A: PartialEq,
|
||||||
|
FE: PartialEq,
|
||||||
|
E: PartialEq,
|
||||||
|
{
|
||||||
|
fn eq(&self, rhs: &Self) -> bool {
|
||||||
|
match (self, rhs) {
|
||||||
|
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
|
||||||
|
(Flow::Err(_), Flow::Err(_)) => true,
|
||||||
|
(Flow::Fatal(_), Flow::Fatal(_)) => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience function to create an ok value.
|
/// Convenience function to create an ok value.
|
||||||
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
|
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
|
||||||
Flow::Ok(val)
|
Flow::Ok(val)
|
||||||
|
@ -177,43 +208,25 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Flow<i32, FatalError, Error> {
|
|
||||||
fn eq(&self, rhs: &Self) -> bool {
|
|
||||||
match (self, rhs) {
|
|
||||||
(Flow::Ok(val), Flow::Ok(rhs)) => val == rhs,
|
|
||||||
(Flow::Err(_), Flow::Err(_)) => true,
|
|
||||||
(Flow::Fatal(_), Flow::Fatal(_)) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for Flow<i32, FatalError, Error> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Flow::Ok(val) => f.write_fmt(format_args!("Flow::Ok {}", val)),
|
|
||||||
Flow::Err(err) => f.write_fmt(format_args!("Flow::Err {:?}", err)),
|
|
||||||
Flow::Fatal(err) => f.write_fmt(format_args!("Flow::Fatal {:?}", err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_map_things() {
|
fn it_can_map_things() {
|
||||||
let success = ok(15);
|
let success: Flow<i32, FatalError, Error> = ok(15);
|
||||||
assert_eq!(ok(16), success.map(|v| v + 1));
|
assert_eq!(ok(16), success.map(|v| v + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_chain_success() {
|
fn it_can_chain_success() {
|
||||||
let success = ok(15);
|
let success: Flow<i32, FatalError, Error> = ok(15);
|
||||||
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
|
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_can_handle_an_error() {
|
fn it_can_handle_an_error() {
|
||||||
let failure = error(Error::Error);
|
let failure: Flow<i32, FatalError, Error> = error(Error::Error);
|
||||||
assert_eq!(ok(16), failure.or_else(|_| ok(16)));
|
assert_eq!(
|
||||||
|
ok::<i32, FatalError, Error>(16),
|
||||||
|
failure.or_else(|_| ok(16))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -27,10 +27,10 @@ pub enum Message {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
Paused(Track, Duration),
|
Paused(TrackId, Duration),
|
||||||
Playing(Track, Duration),
|
Playing(TrackId, Duration),
|
||||||
Stopped,
|
Stopped,
|
||||||
Position(Track, Duration),
|
Position(TrackId, Duration),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -113,14 +113,16 @@ impl AsRef<String> for TrackId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
pub struct TrackInfo {
|
pub struct TrackInfo {
|
||||||
|
pub id: TrackId,
|
||||||
pub track_number: Option<i32>,
|
pub track_number: Option<i32>,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
pub album: Option<String>,
|
pub album: Option<String>,
|
||||||
pub artist: Option<String>,
|
pub artist: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct Track {
|
pub struct Track {
|
||||||
|
@ -130,6 +132,7 @@ pub struct Track {
|
||||||
pub album: Option<String>,
|
pub album: Option<String>,
|
||||||
pub artist: Option<String>,
|
pub artist: Option<String>,
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
impl From<&mpris::Metadata> for Track {
|
impl From<&mpris::Metadata> for Track {
|
||||||
|
@ -154,13 +157,13 @@ impl From<mpris::Metadata> for Track {
|
||||||
#[derive(Clone, Debug, Serialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub enum State {
|
pub enum State {
|
||||||
Playing(Track),
|
Playing(TrackInfo),
|
||||||
Paused(Track),
|
Paused(TrackInfo),
|
||||||
Stopped,
|
Stopped,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CurrentlyPlaying {
|
pub struct CurrentlyPlaying {
|
||||||
track: Track,
|
track: TrackInfo,
|
||||||
position: Duration,
|
position: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
|
audio::TrackInfo,
|
||||||
database::{Database, MemoryIndex, MusicIndex},
|
database::{Database, MemoryIndex, MusicIndex},
|
||||||
Error, FatalError,
|
Error, FatalError,
|
||||||
};
|
};
|
||||||
|
@ -14,20 +15,6 @@ use std::{
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum ScannerError {
|
|
||||||
#[error("Cannot scan {0}")]
|
|
||||||
CannotScan(PathBuf),
|
|
||||||
#[error("IO error {0}")]
|
|
||||||
IO(std::io::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<std::io::Error> for ScannerError {
|
|
||||||
fn from(err: std::io::Error) -> Self {
|
|
||||||
Self::IO(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum ControlMsg {
|
pub enum ControlMsg {
|
||||||
Exit,
|
Exit,
|
||||||
}
|
}
|
||||||
|
@ -52,87 +39,6 @@ pub struct Core {
|
||||||
control_tx: Sender<ControlMsg>,
|
control_tx: Sender<ControlMsg>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_frequency() -> Duration {
|
|
||||||
Duration::from_secs(60)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FileScanner {
|
|
||||||
db: Arc<dyn MusicIndex>,
|
|
||||||
control_rx: Receiver<ControlMsg>,
|
|
||||||
tracker_tx: Sender<TrackMsg>,
|
|
||||||
next_scan: Instant,
|
|
||||||
music_directories: Vec<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileScanner {
|
|
||||||
fn new(
|
|
||||||
db: Arc<dyn MusicIndex>,
|
|
||||||
roots: Vec<PathBuf>,
|
|
||||||
control_rx: Receiver<ControlMsg>,
|
|
||||||
tracker_tx: Sender<TrackMsg>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
db,
|
|
||||||
control_rx,
|
|
||||||
tracker_tx,
|
|
||||||
next_scan: Instant::now(),
|
|
||||||
music_directories: roots,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan(&mut self) {
|
|
||||||
loop {
|
|
||||||
match self.control_rx.recv_timeout(Duration::from_millis(100)) {
|
|
||||||
Ok(ControlMsg::Exit) => return,
|
|
||||||
Err(RecvTimeoutError::Timeout) => (),
|
|
||||||
Err(RecvTimeoutError::Disconnected) => return,
|
|
||||||
}
|
|
||||||
if Instant::now() >= self.next_scan {
|
|
||||||
for root in self.music_directories.iter() {
|
|
||||||
self.scan_dir(vec![root.clone()]);
|
|
||||||
}
|
|
||||||
self.next_scan = Instant::now() + scan_frequency();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_dir(&self, mut paths: Vec<PathBuf>) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
while let Some(dir) = paths.pop() {
|
|
||||||
println!("scanning {:?}", dir);
|
|
||||||
return_error!(self.scan_dir_(&mut paths, dir));
|
|
||||||
}
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_dir_(
|
|
||||||
&self,
|
|
||||||
paths: &mut Vec<PathBuf>,
|
|
||||||
dir: PathBuf,
|
|
||||||
) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
let dir_iter = return_error!(Flow::from(dir.read_dir().map_err(ScannerError::from)));
|
|
||||||
for entry in dir_iter {
|
|
||||||
match entry {
|
|
||||||
Ok(entry) if entry.path().is_dir() => paths.push(entry.path()),
|
|
||||||
Ok(entry) => {
|
|
||||||
let _ = return_fatal!(self.scan_file(entry.path()).or_else(|err| {
|
|
||||||
println!("scan_file failed: {:?}", err);
|
|
||||||
ok::<(), FatalError, ScannerError>(())
|
|
||||||
}));
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
println!("scan_dir could not read path: ({:?})", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scan_file(&self, path: PathBuf) -> Flow<(), FatalError, ScannerError> {
|
|
||||||
ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Core {
|
impl Core {
|
||||||
pub fn new(db: Arc<dyn MusicIndex>) -> Flow<Core, FatalError, Error> {
|
pub fn new(db: Arc<dyn MusicIndex>) -> Flow<Core, FatalError, Error> {
|
||||||
let (control_tx, control_rx) = channel::<ControlMsg>();
|
let (control_tx, control_rx) = channel::<ControlMsg>();
|
||||||
|
@ -140,15 +46,7 @@ impl Core {
|
||||||
let (track_handle, track_rx) = {
|
let (track_handle, track_rx) = {
|
||||||
let (track_tx, track_rx) = channel();
|
let (track_tx, track_rx) = channel();
|
||||||
let db = db.clone();
|
let db = db.clone();
|
||||||
let track_handle = thread::spawn(move || {
|
let track_handle = thread::spawn(move || {});
|
||||||
FileScanner::new(
|
|
||||||
db,
|
|
||||||
vec![PathBuf::from("/home/savanni/Music/")],
|
|
||||||
control_rx,
|
|
||||||
track_tx,
|
|
||||||
)
|
|
||||||
.scan();
|
|
||||||
});
|
|
||||||
(track_handle, track_rx)
|
(track_handle, track_rx)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -168,6 +66,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) {
|
pub fn exit(&self) {
|
||||||
let _ = self.control_tx.send(ControlMsg::Exit);
|
let _ = self.control_tx.send(ControlMsg::Exit);
|
||||||
/*
|
/*
|
||||||
|
@ -178,4 +80,80 @@ impl Core {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod 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() {
|
||||||
|
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),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
audio::{Track, TrackId, TrackInfo},
|
audio::{TrackId, TrackInfo},
|
||||||
FatalError,
|
FatalError,
|
||||||
};
|
};
|
||||||
use flow::{error, ok, Flow};
|
use flow::{error, ok, Flow};
|
||||||
|
@ -11,7 +11,7 @@ use std::{
|
||||||
};
|
};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
pub enum DatabaseError {
|
pub enum DatabaseError {
|
||||||
#[error("database is unreadable")]
|
#[error("database is unreadable")]
|
||||||
DatabaseUnreadable,
|
DatabaseUnreadable,
|
||||||
|
@ -20,13 +20,14 @@ pub enum DatabaseError {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait MusicIndex: Sync + Send {
|
pub trait MusicIndex: Sync + Send {
|
||||||
fn add_track(&mut self, track: &TrackInfo) -> Flow<Track, FatalError, DatabaseError>;
|
fn add_track(&self, track: TrackInfo) -> Flow<(), FatalError, DatabaseError>;
|
||||||
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
|
fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
|
||||||
fn get_track_info(&self, id: &TrackId) -> Flow<Option<Track>, 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 {
|
pub struct MemoryIndex {
|
||||||
tracks: RwLock<HashMap<TrackId, Track>>,
|
tracks: RwLock<HashMap<TrackId, TrackInfo>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MemoryIndex {
|
impl MemoryIndex {
|
||||||
|
@ -38,21 +39,13 @@ impl MemoryIndex {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MusicIndex for MemoryIndex {
|
impl MusicIndex for MemoryIndex {
|
||||||
fn add_track(&mut self, info: &TrackInfo) -> Flow<Track, FatalError, DatabaseError> {
|
fn add_track(&self, info: TrackInfo) -> Flow<(), FatalError, DatabaseError> {
|
||||||
let id = TrackId::default();
|
|
||||||
let track = Track {
|
|
||||||
id: id.clone(),
|
|
||||||
track_number: info.track_number,
|
|
||||||
name: info.name.clone(),
|
|
||||||
album: info.album.clone(),
|
|
||||||
artist: info.artist.clone(),
|
|
||||||
};
|
|
||||||
let mut tracks = self.tracks.write().unwrap();
|
let mut tracks = self.tracks.write().unwrap();
|
||||||
tracks.insert(id, track.clone());
|
tracks.insert(info.id.clone(), info);
|
||||||
ok(track)
|
ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
|
fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
|
||||||
let mut tracks = self.tracks.write().unwrap();
|
let mut tracks = self.tracks.write().unwrap();
|
||||||
tracks.remove(&id);
|
tracks.remove(&id);
|
||||||
ok(())
|
ok(())
|
||||||
|
@ -61,13 +54,23 @@ impl MusicIndex for MemoryIndex {
|
||||||
fn get_track_info<'a>(
|
fn get_track_info<'a>(
|
||||||
&'a self,
|
&'a self,
|
||||||
id: &TrackId,
|
id: &TrackId,
|
||||||
) -> Flow<Option<Track>, FatalError, DatabaseError> {
|
) -> Flow<Option<TrackInfo>, FatalError, DatabaseError> {
|
||||||
let track = {
|
let track = {
|
||||||
let tracks = self.tracks.read().unwrap();
|
let tracks = self.tracks.read().unwrap();
|
||||||
tracks.get(&id).cloned()
|
tracks.get(&id).cloned()
|
||||||
};
|
};
|
||||||
ok(track)
|
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> {
|
pub struct ManagedConnection<'a> {
|
||||||
|
@ -104,3 +107,35 @@ impl Database {
|
||||||
pool.push(conn);
|
pool.push(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn with_memory_index<F>(f: F)
|
||||||
|
where
|
||||||
|
F: Fn(&dyn MusicIndex),
|
||||||
|
{
|
||||||
|
let index = MemoryIndex::new();
|
||||||
|
f(&index)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_saves_and_loads_data() {
|
||||||
|
with_memory_index(|index| {
|
||||||
|
let info = TrackInfo {
|
||||||
|
id: TrackId::from("track_1".to_owned()),
|
||||||
|
track_number: None,
|
||||||
|
name: None,
|
||||||
|
album: None,
|
||||||
|
artist: None,
|
||||||
|
};
|
||||||
|
index.add_track(info.clone());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
Flow::Ok(Some(info)),
|
||||||
|
index.get_track_info(&TrackId::from("track_1".to_owned()))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
pub mod audio;
|
pub mod audio;
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod database;
|
pub mod database;
|
||||||
|
pub mod music_scanner;
|
||||||
use database::DatabaseError;
|
use database::DatabaseError;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Database error: {0}")]
|
#[error("Database error: {0}")]
|
||||||
DatabaseError(DatabaseError),
|
DatabaseError(DatabaseError),
|
||||||
|
@ -16,7 +17,7 @@ impl From<DatabaseError> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error, PartialEq)]
|
||||||
pub enum FatalError {
|
pub enum FatalError {
|
||||||
#[error("Unexpected error")]
|
#[error("Unexpected error")]
|
||||||
UnexpectedError,
|
UnexpectedError,
|
||||||
|
|
|
@ -0,0 +1,114 @@
|
||||||
|
use crate::{
|
||||||
|
core::{ControlMsg, TrackMsg},
|
||||||
|
database::MusicIndex,
|
||||||
|
FatalError,
|
||||||
|
};
|
||||||
|
use flow::{ok, return_error, return_fatal, Flow};
|
||||||
|
use std::{
|
||||||
|
path::PathBuf,
|
||||||
|
sync::{
|
||||||
|
mpsc::{Receiver, RecvTimeoutError, Sender},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
time::{Duration, Instant},
|
||||||
|
};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
fn scan_frequency() -> Duration {
|
||||||
|
Duration::from_secs(60)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ScannerError {
|
||||||
|
#[error("Cannot scan {0}")]
|
||||||
|
CannotScan(PathBuf),
|
||||||
|
#[error("IO error {0}")]
|
||||||
|
IO(std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for ScannerError {
|
||||||
|
fn from(err: std::io::Error) -> Self {
|
||||||
|
Self::IO(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait MusicScanner {
|
||||||
|
fn scan(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FileScanner {
|
||||||
|
db: Arc<dyn MusicIndex>,
|
||||||
|
control_rx: Receiver<ControlMsg>,
|
||||||
|
tracker_tx: Sender<TrackMsg>,
|
||||||
|
next_scan: Instant,
|
||||||
|
music_directories: Vec<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileScanner {
|
||||||
|
fn new(
|
||||||
|
db: Arc<dyn MusicIndex>,
|
||||||
|
roots: Vec<PathBuf>,
|
||||||
|
control_rx: Receiver<ControlMsg>,
|
||||||
|
tracker_tx: Sender<TrackMsg>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
db,
|
||||||
|
control_rx,
|
||||||
|
tracker_tx,
|
||||||
|
next_scan: Instant::now(),
|
||||||
|
music_directories: roots,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scan(&mut self) {
|
||||||
|
loop {
|
||||||
|
match self.control_rx.recv_timeout(Duration::from_millis(100)) {
|
||||||
|
Ok(ControlMsg::Exit) => return,
|
||||||
|
Err(RecvTimeoutError::Timeout) => (),
|
||||||
|
Err(RecvTimeoutError::Disconnected) => return,
|
||||||
|
}
|
||||||
|
if Instant::now() >= self.next_scan {
|
||||||
|
for root in self.music_directories.iter() {
|
||||||
|
self.scan_dir(vec![root.clone()]);
|
||||||
|
}
|
||||||
|
self.next_scan = Instant::now() + scan_frequency();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scan_dir(&self, mut paths: Vec<PathBuf>) -> Flow<(), FatalError, ScannerError> {
|
||||||
|
while let Some(dir) = paths.pop() {
|
||||||
|
println!("scanning {:?}", dir);
|
||||||
|
return_error!(self.scan_dir_(&mut paths, dir));
|
||||||
|
}
|
||||||
|
ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scan_dir_(
|
||||||
|
&self,
|
||||||
|
paths: &mut Vec<PathBuf>,
|
||||||
|
dir: PathBuf,
|
||||||
|
) -> Flow<(), FatalError, ScannerError> {
|
||||||
|
let dir_iter = return_error!(Flow::from(dir.read_dir().map_err(ScannerError::from)));
|
||||||
|
for entry in dir_iter {
|
||||||
|
match entry {
|
||||||
|
Ok(entry) if entry.path().is_dir() => paths.push(entry.path()),
|
||||||
|
Ok(entry) => {
|
||||||
|
let _ = return_fatal!(self.scan_file(entry.path()).or_else(|err| {
|
||||||
|
println!("scan_file failed: {:?}", err);
|
||||||
|
ok::<(), FatalError, ScannerError>(())
|
||||||
|
}));
|
||||||
|
()
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("scan_dir could not read path: ({:?})", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scan_file(&self, path: PathBuf) -> Flow<(), FatalError, ScannerError> {
|
||||||
|
ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue