empede/src/mpd.rs

78 lines
1.9 KiB
Rust
Raw Normal View History

2023-04-25 01:44:03 +00:00
use std::borrow::Cow;
2023-04-25 01:26:27 +00:00
use mpdrs::lsinfo::LsInfoResponse;
pub(crate) const HOST: &str = "192.168.1.203:6600";
pub(crate) async fn ls(path: &str) -> anyhow::Result<Vec<Entry>> {
let mut mpd = mpdrs::Client::connect(HOST)?;
let info = mpd.lsinfo(path)?;
2023-04-25 01:44:03 +00:00
fn filename(path: &str) -> Cow<str> {
std::path::Path::new(path)
.file_name()
.map(|x| x.to_string_lossy())
.unwrap_or(Cow::Borrowed("n/a"))
}
2023-04-25 01:26:27 +00:00
Ok(info
.iter()
.map(|e| match e {
LsInfoResponse::Song(song) => Entry::Song {
name: song.title.as_ref().unwrap_or(&song.file).clone(),
artist: song.artist.clone().unwrap_or(String::new()),
path: song.file.clone(),
},
LsInfoResponse::Directory { path, .. } => {
Entry::Directory {
2023-04-25 01:44:03 +00:00
name: filename(path).to_string(),
2023-04-25 01:26:27 +00:00
path: path.to_string(),
}
}
LsInfoResponse::Playlist { path, .. } => Entry::Playlist {
2023-04-25 01:44:03 +00:00
name: filename(path).to_string(),
2023-04-25 01:26:27 +00:00
path: path.to_string(),
},
})
.collect())
}
pub(crate) struct QueueItem {
pub(crate) title: String,
pub(crate) playing: bool,
}
pub(crate) async fn playlist() -> anyhow::Result<Vec<QueueItem>> {
let mut client = mpdrs::Client::connect(HOST)?;
let current = client.status()?.song;
let queue = client
.queue()?
.into_iter()
.map(|song| QueueItem {
title: song.title.as_ref().unwrap_or(&song.file).clone(),
playing: current == song.place,
})
.collect();
Ok(queue)
}
pub(crate) enum Entry {
Song {
name: String,
artist: String,
path: String,
},
Directory {
name: String,
path: String,
},
Playlist {
name: String,
path: String,
},
}