From fbca4466d069737dc8261e634d88bafe17641758 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sat, 23 Dec 2023 10:07:36 +0100 Subject: [PATCH] Move control routes to separate module and DRY them --- src/main.rs | 95 ++++-------------------------------------- src/mpd.rs | 4 ++ src/routes/controls.rs | 57 +++++++++++++++++++++++++ src/routes/mod.rs | 1 + 4 files changed, 71 insertions(+), 86 deletions(-) create mode 100644 src/routes/controls.rs diff --git a/src/main.rs b/src/main.rs index e011c69..39e3f76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,83 +2,6 @@ mod crate_version; mod mpd; mod routes; -async fn post_play(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - mpd.command("play").await?; - Ok("".into()) -} - -async fn post_pause(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - mpd.command("pause 1").await?; - Ok("".into()) -} - -async fn post_previous(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - mpd.command("previous").await?; - Ok("".into()) -} - -async fn post_next(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - mpd.command("next").await?; - Ok("".into()) -} - -async fn post_consume(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - - let status = mpd.command("status").await?.into_hashmap(); - let consume = status["consume"] == "1"; - - mpd.command(&format!("consume {}", if consume { 0 } else { 1 })) - .await?; - Ok("".into()) -} - -async fn post_random(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - - let status = mpd.command("status").await?.into_hashmap(); - let random = status["random"] == "1"; - - mpd.command(&format!("random {}", if random { 0 } else { 1 })) - .await?; - - Ok("".into()) -} - -async fn post_repeat(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - - let status = mpd.command("status").await?.into_hashmap(); - let repeat = status["repeat"] == "1"; - - mpd.command(&format!("repeat {}", if repeat { 0 } else { 1 })) - .await?; - - Ok("".into()) -} - -async fn post_shuffle(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - mpd.command("shuffle").await?; - Ok("".into()) -} - -async fn post_single(_req: tide::Request<()>) -> tide::Result { - let mut mpd = mpd::get_instance().await; - - let status = mpd.command("status").await?.into_hashmap(); - let single = status["single"] == "1"; - - mpd.command(&format!("single {}", if single { 0 } else { 1 })) - .await?; - - Ok("".into()) -} - async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result<()> { // Update everything on connect sender.send("playlist", "", None).await?; @@ -118,16 +41,16 @@ async fn main() -> tide::Result<()> { app.at("/queue").delete(routes::queue::delete_queue); app.at("/queue/move").post(routes::queue::post_queue_move); - 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("/play").post(routes::controls::post_play); + app.at("/pause").post(routes::controls::post_pause); + app.at("/previous").post(routes::controls::post_previous); + app.at("/next").post(routes::controls::post_next); - app.at("/consume").post(post_consume); - app.at("/random").post(post_random); - app.at("/repeat").post(post_repeat); - app.at("/single").post(post_single); - app.at("/shuffle").post(post_shuffle); + app.at("/consume").post(routes::controls::post_consume); + app.at("/random").post(routes::controls::post_random); + app.at("/repeat").post(routes::controls::post_repeat); + app.at("/single").post(routes::controls::post_single); + app.at("/shuffle").post(routes::controls::post_shuffle); app.at("/static").serve_dir("static/")?; diff --git a/src/mpd.rs b/src/mpd.rs index 5378980..db32dae 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -57,6 +57,10 @@ pub async fn get_instance() -> MutexGuard<'static, Mpd> { instance.lock().await } +pub async fn command(command: &str) -> anyhow::Result { + get_instance().await.command(command).await +} + pub struct CommandResult { properties: Vec<(String, String)>, binary: Option>, diff --git a/src/routes/controls.rs b/src/routes/controls.rs new file mode 100644 index 0000000..40b38c5 --- /dev/null +++ b/src/routes/controls.rs @@ -0,0 +1,57 @@ +use crate::mpd; + +async fn toggle_setting(setting: &str) -> anyhow::Result<()> { + let mut mpd = mpd::get_instance().await; + + let status = mpd.command("status").await?.into_hashmap(); + let value = status[setting] == "1"; + + mpd.command(&format!("{} {}", setting, if value { 0 } else { 1 })) + .await?; + Ok(()) +} + +pub async fn post_play(_req: tide::Request<()>) -> tide::Result { + mpd::command("play").await?; + Ok("".into()) +} + +pub async fn post_pause(_req: tide::Request<()>) -> tide::Result { + mpd::command("pause 1").await?; + Ok("".into()) +} + +pub async fn post_previous(_req: tide::Request<()>) -> tide::Result { + mpd::command("previous").await?; + Ok("".into()) +} + +pub async fn post_next(_req: tide::Request<()>) -> tide::Result { + mpd::command("next").await?; + Ok("".into()) +} + +pub async fn post_consume(_req: tide::Request<()>) -> tide::Result { + toggle_setting("consume").await?; + Ok("".into()) +} + +pub async fn post_random(_req: tide::Request<()>) -> tide::Result { + toggle_setting("random").await?; + Ok("".into()) +} + +pub async fn post_repeat(_req: tide::Request<()>) -> tide::Result { + toggle_setting("repeat").await?; + Ok("".into()) +} + +pub async fn post_shuffle(_req: tide::Request<()>) -> tide::Result { + mpd::command("shuffle").await?; + Ok("".into()) +} + +pub async fn post_single(_req: tide::Request<()>) -> tide::Result { + toggle_setting("single").await?; + Ok("".into()) +} \ No newline at end of file diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 5b02e2a..4463182 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -3,3 +3,4 @@ pub mod browser; pub mod index; pub mod player; pub mod queue; +pub mod controls;