From 05dddfde6d4e7fbaae17038e21cea16e88a574a0 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Tue, 16 May 2023 01:53:46 +0200 Subject: [PATCH] Fix error when no track is playing --- src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2a86565..3d0a21e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,8 +53,14 @@ async fn get_player(_req: tide::Request<()>) -> tide::Result { let song = mpd.command("currentsong").await?.into_hashmap(); let status = mpd.command("status").await?.into_hashmap(); - let elapsed = status["elapsed"].parse().unwrap_or(0.0); - let duration = status["duration"].parse().unwrap_or(1.0); + let elapsed = status + .get("elapsed") + .and_then(|e| e.parse().ok()) + .unwrap_or(0.0); + let duration = status + .get("duration") + .and_then(|e| e.parse().ok()) + .unwrap_or(1.0); let mut template = PlayerTemplate { song: if song.is_empty() { None } else { Some(&song) },