From 96a514d3c61a892ed6a1305b11d935068420f128 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Tue, 25 Apr 2023 16:32:35 +0200 Subject: [PATCH] Simplify mpd calls --- src/main.rs | 38 ++++++++++++++++++++------------------ src/mpd.rs | 24 +++++++++++++----------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3d4bae2..f2b2212 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ struct IndexQuery { async fn index(req: tide::Request<()>) -> tide::Result { let query: IndexQuery = req.query()?; - let entries = mpd::ls(&query.path).await?; + let entries = mpd::ls(&query.path)?; let template = IndexTemplate { path: Path::new(&query.path) .iter() @@ -44,7 +44,7 @@ struct QueueTemplate { } async fn get_queue(_req: tide::Request<()>) -> tide::Result { - let queue = mpd::playlist().await?; + let queue = mpd::playlist()?; let template = QueueTemplate { queue }; Ok(template.into()) } @@ -55,30 +55,28 @@ struct PostQueueQuery { } async fn post_queue(req: tide::Request<()>) -> tide::Result { - let mut client = mpdrs::Client::connect(mpd::HOST)?; let query: PostQueueQuery = req.query()?; - client.add(&query.path)?; + mpd::connect()?.add(&query.path)?; Ok("".into()) } async fn post_play(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpdrs::Client::connect(mpd::HOST)?; - mpd.play()?; + mpd::connect()?.play()?; Ok("".into()) } + async fn post_pause(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpdrs::Client::connect(mpd::HOST)?; - mpd.pause(true)?; + mpd::connect()?.pause(true)?; Ok("".into()) } + async fn post_previous(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpdrs::Client::connect(mpd::HOST)?; - mpd.prev()?; + mpd::connect()?.prev()?; Ok("".into()) } + async fn post_next(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpdrs::Client::connect(mpd::HOST)?; - mpd.next()?; + mpd::connect()?.next()?; Ok("".into()) } @@ -97,11 +95,11 @@ async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result buffer.clear(); reader.read_line(&mut buffer).await?; - if buffer == "changed: playlist\n" { - sender.send("queue", "", None).await?; - } else if buffer == "changed: player\n" { - sender.send("player", "", None).await?; - } + let (_, changed) = buffer + .trim_end() + .split_once(": ") + .ok_or(anyhow!("unexpected response from MPD"))?; + sender.send(changed, "", None).await?; buffer.clear(); reader.read_line(&mut buffer).await?; @@ -119,14 +117,18 @@ async fn main() -> tide::Result<()> { let mut app = tide::new(); app.with(tide_tracing::TraceMiddleware::new()); + app.at("/").get(index); app.at("/queue").get(get_queue); + + app.at("/sse").get(tide::sse::endpoint(sse)); + app.at("/queue").post(post_queue); app.at("/play").post(post_play); app.at("/pause").post(post_pause); app.at("/previous").post(post_previous); app.at("/next").post(post_next); - app.at("/sse").get(tide::sse::endpoint(sse)); + app.listen("0.0.0.0:8080").await?; Ok(()) } diff --git a/src/mpd.rs b/src/mpd.rs index 3f25df6..b342630 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -4,9 +4,12 @@ use mpdrs::lsinfo::LsInfoResponse; pub(crate) const HOST: &str = "192.168.1.203:6600"; -pub(crate) async fn ls(path: &str) -> anyhow::Result> { - let mut mpd = mpdrs::Client::connect(HOST)?; - let info = mpd.lsinfo(path)?; +pub(crate) fn connect() -> Result { + mpdrs::Client::connect(HOST) +} + +pub(crate) fn ls(path: &str) -> anyhow::Result> { + let info = connect()?.lsinfo(path)?; fn filename(path: &str) -> Cow { std::path::Path::new(path) @@ -24,12 +27,10 @@ pub(crate) async fn ls(path: &str) -> anyhow::Result> { path: song.file.clone(), }, - LsInfoResponse::Directory { path, .. } => { - Entry::Directory { - name: filename(path).to_string(), - path: path.to_string(), - } - } + LsInfoResponse::Directory { path, .. } => Entry::Directory { + name: filename(path).to_string(), + path: path.to_string(), + }, LsInfoResponse::Playlist { path, .. } => Entry::Playlist { name: filename(path).to_string(), @@ -44,8 +45,8 @@ pub(crate) struct QueueItem { pub(crate) playing: bool, } -pub(crate) async fn playlist() -> anyhow::Result> { - let mut client = mpdrs::Client::connect(HOST)?; +pub(crate) fn playlist() -> anyhow::Result> { + let mut client = connect()?; let current = client.status()?.song; @@ -57,6 +58,7 @@ pub(crate) async fn playlist() -> anyhow::Result> { playing: current == song.place, }) .collect(); + Ok(queue) }