Compare commits

..

4 Commits

19 changed files with 8144 additions and 3125 deletions

View File

@ -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
//! 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.
use std::{error::Error, fmt};
use std::error::Error;
/// Implement this trait for the application's fatal errors.
///
@ -110,37 +110,6 @@ 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.
pub fn ok<A, FE: FatalError, E: Error>(val: A) -> Flow<A, FE, E> {
Flow::Ok(val)
@ -208,25 +177,43 @@ 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]
fn it_can_map_things() {
let success: Flow<i32, FatalError, Error> = ok(15);
let success = ok(15);
assert_eq!(ok(16), success.map(|v| v + 1));
}
#[test]
fn it_can_chain_success() {
let success: Flow<i32, FatalError, Error> = ok(15);
let success = ok(15);
assert_eq!(ok(16), success.and_then(|v| ok(v + 1)));
}
#[test]
fn it_can_handle_an_error() {
let failure: Flow<i32, FatalError, Error> = error(Error::Error);
assert_eq!(
ok::<i32, FatalError, Error>(16),
failure.or_else(|_| ok(16))
);
let failure = error(Error::Error);
assert_eq!(ok(16), failure.or_else(|_| ok(16)));
}
#[test]

View File

@ -1,9 +0,0 @@
server-dev:
cd server && cargo watch -x run
server-test:
cd server && cargo watch -x test
client-dev:
cd client && npm run watch

View File

@ -11,7 +11,6 @@
<div class="controls"><button class="play-pause">Pause</button></div>
<div class="track-list">
<!--
<div class="track-list__grouping">
<ul class="bulletless-list">
<li> By Artist </li>
@ -20,15 +19,61 @@
<li> Dance Music </li>
</ul>
</div>
-->
<div>
<ul class="track-list__tracks bulletless-list">
</ul>
<div class="track-list__tracks">
<table>
<thead>
<tr>
<th> Track # </th>
<th> Title </th>
<th> Artist </th>
<th> Album </th>
<th> Length </th>
</tr>
</thead>
<tbody>
<tr class="track-list__track-row">
<td> 1 </td>
<td> Underground </td>
<td> Lindsey Stirling </td>
<td> Artemis </td>
<td> 4:24 </td>
</tr>
<tr class="track-list__track-row">
<td> 2 </td>
<td> Artemis </td>
<td> Lindsey Stirling </td>
<td> Artemis </td>
<td> 3:54 </td>
</tr>
<tr class="track-list__track-row">
<td> 3 </td>
<td> Til the Light Goes Out </td>
<td> Lindsey Stirling </td>
<td> Artemis </td>
<td> 4:46 </td>
</tr>
<tr class="track-list__track-row">
<td> 4 </td>
<td> Between Twilight </td>
<td> Lindsey Stirling </td>
<td> Artemis </td>
<td> 4:20 </td>
</tr>
<tr class="track-list__track-row">
<td> 5 </td>
<td> Foreverglow </td>
<td> Lindsey Stirling </td>
<td> Artemis </td>
<td> 3:58 </td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="./bundle.js" type="module"></script>
<script src="./dist/main.js" type="module"></script>
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@ -4,9 +4,9 @@
"description": "",
"main": "main.js",
"scripts": {
"build": "webpack",
"build": "browserify src/main.ts -p [ tsify ] > dist/bundle.js",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch"
"watch": "exa index.html styles.css src/* | entr -s 'npm run build'"
},
"author": "Savanni D'Gerinel <savanni@luminescent-dreams.com>",
"license": "GPL-3.0-or-later",
@ -16,12 +16,11 @@
},
"devDependencies": {
"@types/lodash": "^4.14.191",
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.7.3",
"style-loader": "^3.3.1",
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
"babelify": "^10.0.0",
"browserify": "^17.0.0",
"live-server": "^1.2.2",
"tsify": "^5.0.4",
"typescript": "^4.9.4",
"watchify": "^4.0.0"
}
}

View File

@ -1,133 +0,0 @@
import { TrackInfo } from "../client";
export class TrackName extends HTMLElement {
container: HTMLElement;
static get observedAttributes() {
return ["name"];
}
constructor() {
super();
this.container = document.createElement("div");
}
get name(): string | null {
return this.getAttribute("name");
}
set name(name: string | null) {
while (this.container.lastChild) {
this.container.removeChild(this.container.lastChild);
}
if (name) {
this.setAttribute("name", name);
this.container.appendChild(document.createTextNode(name));
} else {
this.removeAttribute("name");
}
}
connectedCallback() {
this.appendChild(this.container);
}
}
export class TrackCard extends HTMLElement {
static get observedAttributes() {
return ["id", "trackNumber", "name", "album", "artist"];
}
constructor() {
super();
}
attributeChangeCallback(
attrName: string,
oldValue: string,
newValue: string
): void {
if (newValue !== oldValue) {
this.updateContent();
}
}
get name(): string | null {
return this.getAttribute("name");
}
set name(name: string | null) {
if (name) {
this.setAttribute("name", name);
} else {
this.removeAttribute("open");
}
this.updateContent();
}
get artist(): string | null {
return this.getAttribute("artist");
}
set artist(artist: string | null) {
if (artist) {
this.setAttribute("artist", artist);
} else {
this.removeAttribute("open");
}
this.updateContent();
}
get album(): string | null {
return this.getAttribute("album");
}
set album(album: string | null) {
if (album) {
this.setAttribute("album", album);
} else {
this.removeAttribute("open");
}
this.updateContent();
}
get length(): string | null {
return this.getAttribute("length");
}
set length(length: string | null) {
if (length) {
this.setAttribute("length", length);
} else {
this.removeAttribute("open");
}
this.updateContent();
}
connectedCallback() {
this.updateContent();
}
updateContent() {
const container = document.createElement("div");
container.classList.add("track-card");
this.innerHTML = "";
this.appendChild(container);
while (container.lastChild) {
container.removeChild(container.lastChild);
}
if (this["name"]) {
const trackName = document.createElement("track-name");
trackName.name = this["name"];
container.appendChild(trackName);
}
this["length"] && container.appendChild(document.createTextNode("1:23"));
this["album"] &&
container.appendChild(document.createTextNode("Shatter Me"));
this["artist"] &&
container.appendChild(document.createTextNode("Lindsey Stirling"));
}
}

View File

@ -1,10 +0,0 @@
export interface TrackInfo {
id: string;
track_number?: number;
name?: string;
album?: string;
artist?: string;
}
export const getTracks = (): Promise<TrackInfo[]> =>
fetch("/api/v1/tracks").then((r) => r.json());

View File

@ -1,16 +1,4 @@
import * as _ from "lodash";
import { TrackInfo, getTracks } from "./client";
import { TrackName, TrackCard } from "./blocks/track";
window.customElements.define("track-name", TrackName);
window.customElements.define("track-card", TrackCard);
declare global {
interface HTMLElementTagNameMap {
"track-name": TrackName;
"track-card": TrackCard;
}
}
const replaceTitle = () => {
const title = document.querySelector(".js-title");
@ -19,27 +7,21 @@ const replaceTitle = () => {
}
};
const updateTrackList = (tracks: TrackInfo[]) => {
const track_list = document.querySelector(".track-list__tracks");
if (track_list) {
let track_formats = _.map(tracks, (info) => {
let card: TrackCard = document.createElement("track-card");
card.name = info.name || null;
return card;
});
_.map(track_formats, (trackCard) => {
let listItem = document.createElement("li");
listItem.classList.add("track-list__row");
listItem.appendChild(trackCard);
track_list.appendChild(listItem);
});
} else {
console.log("track_list does not exist");
}
};
/*
const checkWeatherService = () => {
fetch("https://api.weather.gov/")
.then((r) => r.json())
.then((js) => {
const weather = document.querySelector('.js-weather');
weather.innerHTML = js.status;
});
}
*/
const run = () => {
getTracks().then((tracks) => updateTrackList(tracks));
replaceTitle();
console.log(_.map([4, 8], (x) => x * x));
// checkWeatherService();
};
run();

View File

@ -25,37 +25,15 @@ body {
list-style: none;
}
.track-list__row {
margin-top: 32px;
.track-list__track-row {
background-color: rgb(10, 10, 10);
}
/*
.track-list__row:nth-child(even) {
.track-list__track-row:nth-child(even) {
background-color: rgb(255, 255, 255);
}
.track-list__row:nth-child(odd) {
.track-list__track-row:nth-child(odd) {
background-color: rgb(200, 200, 200);
}
*/
.track-card {
border: 1px solid black;
border-radius: 5px;
padding: 8px;
width: 300px;
height: 100px;
}
.track-card__name {
}
.track-card__length {
}
.track-card__album {
}
.track-card__artist {
}

View File

@ -6,9 +6,6 @@
"rootDir": "src",
"outDir": "./dist",
"lib": ["es2016", "DOM"],
"sourceMap": true,
"strict": true,
"noImplicitAny": true
},
"include": ["src"]
"sourceMap": true
}
}

View File

@ -1,39 +0,0 @@
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/main.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.html$/i,
type: 'asset/resource',
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
plugins: [
new CopyPlugin({
patterns: [
{ from: "index.html", to: "index.html" },
{ from: "styles.css", to: "styles.css" },
],
}),
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};

View File

@ -583,7 +583,6 @@ version = "0.1.0"
dependencies = [
"dbus",
"flow",
"mime_guess",
"mpris",
"rusqlite",
"serde",

View File

@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
dbus = { version = "0.9.7" }
flow = { path = "../../flow" }
mime_guess = "2.0.4"
mpris = { version = "2.0" }
rusqlite = { version = "0.28" }
serde = { version = "1.0", features = ["derive"] }

View File

@ -10,8 +10,15 @@ Luminescent Dreams Tools is distributed in the hope that it will be useful, but
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
*/
use dbus::ffidisp::Connection;
use mpris::{FindingError, PlaybackStatus, Player, PlayerFinder, ProgressTick};
use serde::Serialize;
use std::time::Duration;
use std::{
path::PathBuf,
sync::mpsc::{channel, Receiver, Sender, TryRecvError},
thread,
time::Duration,
};
use thiserror::Error;
pub enum Message {
@ -20,10 +27,10 @@ pub enum Message {
#[derive(Clone, Debug)]
pub enum Event {
Paused(TrackId, Duration),
Playing(TrackId, Duration),
Paused(Track, Duration),
Playing(Track, Duration),
Stopped,
Position(TrackId, Duration),
Position(Track, Duration),
}
#[derive(Debug)]
@ -32,6 +39,17 @@ pub struct DeviceInformation {
pub name: String,
}
pub fn list_devices(conn: Connection) -> Result<Vec<DeviceInformation>, FindingError> {
Ok(PlayerFinder::for_connection(conn)
.find_all()?
.into_iter()
.map(|player| DeviceInformation {
address: player.unique_name().to_owned(),
name: player.identity().to_owned(),
})
.collect())
}
#[derive(Debug, Error)]
pub enum AudioError {
#[error("DBus device was not found")]
@ -95,8 +113,17 @@ impl AsRef<String> for TrackId {
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug)]
pub struct TrackInfo {
pub track_number: Option<i32>,
pub name: Option<String>,
pub album: Option<String>,
pub artist: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Track {
pub id: TrackId,
pub track_number: Option<i32>,
pub name: Option<String>,
@ -104,20 +131,38 @@ pub struct TrackInfo {
pub artist: Option<String>,
}
/*
impl From<&mpris::Metadata> for Track {
fn from(data: &mpris::Metadata) -> Self {
Self {
id: data.track_id().unwrap(),
track_number: data.track_number(),
name: data.title().map(|s| s.to_owned()),
album: data.album_name().map(|s| s.to_owned()),
artist: None,
}
}
}
impl From<mpris::Metadata> for Track {
fn from(data: mpris::Metadata) -> Self {
Self::from(&data)
}
}
*/
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum State {
Playing(TrackInfo),
Paused(TrackInfo),
Playing(Track),
Paused(Track),
Stopped,
}
/*
pub struct CurrentlyPlaying {
track: TrackInfo,
track: Track,
position: Duration,
}
*/
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
@ -135,7 +180,6 @@ pub trait AudioPlayer {
fn play_pause(&self) -> Result<State, AudioError>;
}
/*
pub struct GStreamerPlayer {
url: url::Url,
}
@ -157,7 +201,6 @@ impl AudioPlayer for GStreamerPlayer {
unimplemented!()
}
}
*/
/*
pub struct MprisDevice {

View File

@ -1,92 +1,72 @@
use flow::Flow;
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
sync::Arc,
};
use warp::Filter;
use std::{io::stdin, path::PathBuf, sync::Arc, thread, time::Duration};
// use warp::Filter;
use music_player::{
audio::TrackInfo,
core::Core,
database::{MemoryIndex, MusicIndex},
music_scanner::FileScanner,
};
use music_player::{core::Core, database::MemoryIndex};
fn tracks(index: &Arc<impl MusicIndex>) -> Vec<TrackInfo> {
match index.list_tracks() {
Flow::Ok(tracks) => tracks,
Flow::Err(err) => panic!("error: {}", err),
Flow::Fatal(err) => panic!("fatal: {}", err),
}
}
struct Static(PathBuf);
impl Static {
fn read(self, root: PathBuf) -> String {
/*
let mut path = root;
match self {
Bundle::Index => path.push(PathBuf::from("index.html")),
Bundle::App => path.push(PathBuf::from("bundle.js")),
Bundle::Styles => path.push(PathBuf::from("styles.css")),
};
std::fs::read_to_string(path).expect("to find the file")
*/
let mut path = root;
path.push(self.0);
println!("path: {:?}", path);
std::fs::read_to_string(path).expect("to find the file")
}
/*
fn tracks() -> Vec<Track> {
vec![
Track {
track_number: Some(1),
name: Some("Underground".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/01 - Underground.ogg"),
},
Track {
track_number: Some(2),
name: Some("Artemis".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/02 - Artemis.ogg"),
},
Track {
track_number: Some(3),
name: Some("Til the Light Goes Out".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from(
"/mnt/music/Lindsey Stirling/Artemis/03 - Til the Light Goes Out.ogg",
),
},
Track {
track_number: Some(4),
name: Some("Between Twilight".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/04 - Between Twilight.ogg"),
},
Track {
track_number: Some(5),
name: Some("Foreverglow".to_owned()),
album: Some("Artemis".to_owned()),
artist: Some("Lindsey Stirling".to_owned()),
path: PathBuf::from("/mnt/music/Lindsey Stirling/Artemis/05 - Foreverglow.ogg"),
},
]
}
*/
#[tokio::main]
pub async fn main() {
let dev = std::env::var("DEV")
.ok()
.and_then(|v| v.parse::<bool>().ok())
.unwrap_or(false);
let bundle_root = std::env::var("BUNDLE_ROOT")
.map(|b| PathBuf::from(b))
.unwrap();
let music_root = std::env::var("MUSIC_ROOT")
.map(|b| PathBuf::from(b))
.unwrap();
let index = Arc::new(MemoryIndex::new());
let scanner = FileScanner::new(vec![music_root.clone()]);
let _core = match Core::new(index.clone(), scanner) {
Flow::Ok(core) => core,
Flow::Err(error) => panic!("error: {}", error),
Flow::Fatal(error) => panic!("fatal: {}", error),
};
println!("config: {:?} {:?} {:?}", dev, bundle_root, music_root);
let root = warp::path!().and(warp::get()).map({
let bundle_root = bundle_root.clone();
move || {
warp::http::Response::builder()
.header("content-type", "text/html")
.body(Static(PathBuf::from("index.html")).read(bundle_root.clone()))
match Core::new(Arc::new(MemoryIndex::new())) {
Flow::Ok(core) => {
let mut buf = String::new();
let _ = stdin().read_line(&mut buf).unwrap();
core.exit();
}
});
let assets = warp::path!(String).and(warp::get()).map({
let bundle_root = bundle_root.clone();
move |filename: String| {
let mime_type = mime_guess::from_path(filename.clone())
.first()
.map(|m| m.essence_str().to_owned())
.unwrap_or("text/plain".to_owned());
println!("mime_type: {:?}", mime_type);
// let mut path = PathBuf::from("assets");
// path.push(filename);
warp::http::Response::builder()
.header("content-type", mime_type)
.body(Static(PathBuf::from(filename)).read(bundle_root.clone()))
}
});
Flow::Err(err) => println!("non-fatal error: {:?}", err),
Flow::Fatal(err) => println!("fatal error: {:?}", err),
}
/*
let connection = Connection::new_session().expect("to connect to dbus");
for player in list_players(connection) {
println!("player found: {}", player.identity());
}
*/
/*
let devices = warp::path!("api" / "v1" / "devices")
@ -95,14 +75,10 @@ pub async fn main() {
let conn = Connection::new_session().expect("to connect to dbus");
warp::reply::json(&list_devices(conn))
});
*/
let track_list = warp::path!("api" / "v1" / "tracks").and(warp::get()).map({
let index = index.clone();
move || warp::reply::json(&tracks(&index))
});
/*
let track_list = warp::path!("api" / "v1" / "tracks")
.and(warp::get())
.map(|| warp::reply::json(&tracks()));
let tracks_for_artist = warp::path!("api" / "v1" / "artist" / String)
.and(warp::get())
.map(|_artist: String| warp::reply::json(&tracks()));
@ -127,10 +103,12 @@ pub async fn main() {
.or(tracks_for_artist)
.or(queue)
.or(playing_status);
*/
let routes = root.or(assets).or(track_list);
let server = warp::serve(routes);
server
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
.run(SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
8002,
))
.await;
*/
}

View File

@ -1,8 +1,10 @@
use crate::{
audio::TrackInfo, database::MusicIndex, music_scanner::MusicScanner, Error, FatalError,
database::{Database, MemoryIndex, MusicIndex},
Error, FatalError,
};
use flow::{ok, Flow};
use flow::{error, fatal, ok, return_error, return_fatal, Flow};
use std::{
path::{Path, PathBuf},
sync::{
mpsc::{channel, Receiver, RecvTimeoutError, Sender},
Arc,
@ -12,8 +14,18 @@ use std::{
time::{Duration, Instant},
};
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 enum ControlMsg {
@ -21,8 +33,7 @@ pub enum ControlMsg {
}
pub enum TrackMsg {
UpdateInProgress,
UpdateComplete,
DbUpdate,
}
pub enum PlaybackMsg {
@ -34,68 +45,129 @@ pub enum PlaybackMsg {
pub struct Core {
db: Arc<dyn MusicIndex>,
_track_handle: JoinHandle<()>,
_track_rx: Receiver<TrackMsg>,
_playback_handle: JoinHandle<()>,
_playback_rx: Receiver<PlaybackMsg>,
track_handle: JoinHandle<()>,
track_rx: Receiver<TrackMsg>,
playback_handle: JoinHandle<()>,
playback_rx: Receiver<PlaybackMsg>,
control_tx: Sender<ControlMsg>,
}
impl Core {
pub fn new(
db: Arc<dyn MusicIndex>,
scanner: impl MusicScanner + 'static,
) -> Flow<Core, FatalError, Error> {
let (control_tx, control_rx) = channel::<ControlMsg>();
let db = db;
fn scan_frequency() -> Duration {
Duration::from_secs(60)
}
let (_track_handle, _track_rx) = {
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 {
pub fn new(db: Arc<dyn MusicIndex>) -> Flow<Core, FatalError, Error> {
let (control_tx, control_rx) = channel::<ControlMsg>();
let (track_handle, track_rx) = {
let (track_tx, track_rx) = channel();
let db = db.clone();
let track_handle = thread::spawn(move || {
let mut next_scan = Instant::now();
loop {
if Instant::now() >= next_scan {
let _ = track_tx.send(TrackMsg::UpdateInProgress);
for track in scanner.scan() {
match track {
Ok(track) => db.add_track(track),
Err(_) => ok(()),
};
}
let _ = track_tx.send(TrackMsg::UpdateComplete);
next_scan = Instant::now() + scan_frequency();
}
match control_rx.recv_timeout(Duration::from_millis(1000)) {
Ok(ControlMsg::Exit) => return,
Err(RecvTimeoutError::Timeout) => (),
Err(RecvTimeoutError::Disconnected) => return,
}
}
FileScanner::new(
db,
vec![PathBuf::from("/home/savanni/Music/")],
control_rx,
track_tx,
)
.scan();
});
(track_handle, track_rx)
};
let (_playback_handle, _playback_rx) = {
let (_playback_tx, playback_rx) = channel();
let (playback_handle, playback_rx) = {
let (playback_tx, playback_rx) = channel();
let playback_handle = thread::spawn(move || {});
(playback_handle, playback_rx)
};
ok(Core {
db,
_track_handle,
_track_rx,
_playback_handle,
_playback_rx,
track_handle,
track_rx,
playback_handle,
playback_rx,
control_tx,
})
}
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);
/*
@ -106,49 +178,4 @@ impl Core {
}
#[cfg(test)]
mod test {
use super::*;
use crate::{audio::TrackId, database::MemoryIndex, music_scanner::factories::MockScanner};
use std::collections::HashSet;
fn with_example_index<F>(f: F)
where
F: Fn(Core),
{
let index = MemoryIndex::new();
let scanner = MockScanner::new();
match Core::new(Arc::new(index), scanner) {
Flow::Ok(core) => {
thread::sleep(Duration::from_millis(10));
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),
})
}
}
mod test {}

View File

@ -1,5 +1,5 @@
use crate::{
audio::{TrackId, TrackInfo},
audio::{Track, TrackId, TrackInfo},
FatalError,
};
use flow::{error, ok, Flow};
@ -11,7 +11,7 @@ use std::{
};
use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error)]
pub enum DatabaseError {
#[error("database is unreadable")]
DatabaseUnreadable,
@ -20,14 +20,13 @@ pub enum DatabaseError {
}
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>;
fn add_track(&mut self, track: &TrackInfo) -> Flow<Track, FatalError, DatabaseError>;
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError>;
fn get_track_info(&self, id: &TrackId) -> Flow<Option<Track>, FatalError, DatabaseError>;
}
pub struct MemoryIndex {
tracks: RwLock<HashMap<TrackId, TrackInfo>>,
tracks: RwLock<HashMap<TrackId, Track>>,
}
impl MemoryIndex {
@ -39,13 +38,21 @@ impl MemoryIndex {
}
impl MusicIndex for MemoryIndex {
fn add_track(&self, info: TrackInfo) -> Flow<(), FatalError, DatabaseError> {
fn add_track(&mut self, info: &TrackInfo) -> Flow<Track, 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();
tracks.insert(info.id.clone(), info);
ok(())
tracks.insert(id, track.clone());
ok(track)
}
fn remove_track(&self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
fn remove_track(&mut self, id: &TrackId) -> Flow<(), FatalError, DatabaseError> {
let mut tracks = self.tracks.write().unwrap();
tracks.remove(&id);
ok(())
@ -54,23 +61,13 @@ impl MusicIndex for MemoryIndex {
fn get_track_info<'a>(
&'a self,
id: &TrackId,
) -> Flow<Option<TrackInfo>, FatalError, DatabaseError> {
) -> Flow<Option<Track>, FatalError, DatabaseError> {
let track = {
let tracks = self.tracks.read().unwrap();
tracks.get(&id).cloned()
};
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> {
@ -107,35 +104,3 @@ impl Database {
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()))
);
});
}
}

View File

@ -1,11 +1,10 @@
pub mod audio;
pub mod core;
pub mod database;
pub mod music_scanner;
use database::DatabaseError;
use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error)]
pub enum Error {
#[error("Database error: {0}")]
DatabaseError(DatabaseError),
@ -17,7 +16,7 @@ impl From<DatabaseError> for Error {
}
}
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error)]
pub enum FatalError {
#[error("Unexpected error")]
UnexpectedError,

View File

@ -1,182 +0,0 @@
use crate::audio::{TrackId, TrackInfo};
use std::{
fs::{DirEntry, ReadDir},
path::PathBuf,
};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ScannerError {
#[error("Cannot scan {0}")]
CannotScan(PathBuf),
#[error("Not found {0}")]
NotFound(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: Send {
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a>;
}
pub struct FileScanner {
roots: Vec<PathBuf>,
}
impl FileScanner {
pub fn new(roots: Vec<PathBuf>) -> Self {
Self { roots }
}
}
pub struct FileIterator {
dirs: Vec<PathBuf>,
file_iter: Option<ReadDir>,
}
impl FileIterator {
fn scan_file(&self, path: PathBuf) -> Result<TrackInfo, ScannerError> {
Ok(TrackInfo {
id: TrackId::from(path.to_str().unwrap().to_owned()),
album: None,
artist: None,
name: path
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.to_owned()),
track_number: None,
})
}
}
enum EntryInfo {
Dir(PathBuf),
File(PathBuf),
}
impl Iterator for FileIterator {
type Item = Result<TrackInfo, ScannerError>;
fn next(&mut self) -> Option<Self::Item> {
fn process_entry(entry: DirEntry) -> Result<EntryInfo, ScannerError> {
entry
.metadata()
.map_err(ScannerError::from)
.map(|metadata| {
if metadata.is_dir() {
EntryInfo::Dir(entry.path())
} else {
EntryInfo::File(entry.path())
}
})
}
let next_file = match &mut self.file_iter {
Some(iter) => iter.next(),
None => None,
};
match next_file {
Some(Ok(entry)) => match process_entry(entry) {
Ok(EntryInfo::Dir(path)) => {
self.dirs.push(path);
self.next()
}
Ok(EntryInfo::File(path)) => Some(self.scan_file(path)),
Err(err) => Some(Err(err)),
},
Some(Err(err)) => Some(Err(ScannerError::from(err))),
None => match self.dirs.pop() {
Some(dir) => match dir.read_dir() {
Ok(entry) => {
self.file_iter = Some(entry);
self.next()
}
Err(err) => {
if err.kind() == std::io::ErrorKind::NotFound {
Some(Err(ScannerError::NotFound(dir)))
} else {
Some(Err(ScannerError::from(err)))
}
}
},
None => None,
},
}
}
}
impl MusicScanner for FileScanner {
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> {
Box::new(FileIterator {
dirs: self.roots.clone(),
file_iter: None,
})
}
}
#[cfg(test)]
pub mod factories {
use super::*;
use crate::audio::TrackId;
pub struct MockScanner {
data: Vec<TrackInfo>,
}
impl MockScanner {
pub fn new() -> Self {
Self {
data: vec![
TrackInfo {
id: TrackId::from("/home/savanni/Track 1.mp3".to_owned()),
track_number: Some(1),
name: Some("Track 1".to_owned()),
album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()),
},
TrackInfo {
id: TrackId::from("/home/savanni/Track 2.mp3".to_owned()),
track_number: Some(2),
name: Some("Track 2".to_owned()),
album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()),
},
TrackInfo {
id: TrackId::from("/home/savanni/Track 3.mp3".to_owned()),
track_number: Some(3),
name: Some("Track 3".to_owned()),
album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()),
},
TrackInfo {
id: TrackId::from("/home/savanni/Track 4.mp3".to_owned()),
track_number: Some(4),
name: Some("Track 4".to_owned()),
album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()),
},
TrackInfo {
id: TrackId::from("/home/savanni/Track 5.mp3".to_owned()),
track_number: Some(5),
name: Some("Track 5".to_owned()),
album: Some("Savanni's Demo".to_owned()),
artist: Some("Savanni".to_owned()),
},
],
}
}
}
impl MusicScanner for MockScanner {
fn scan<'a>(&'a self) -> Box<dyn Iterator<Item = Result<TrackInfo, ScannerError>> + 'a> {
Box::new(self.data.iter().map(|t| Ok(t.clone())))
}
}
}