diff --git a/Cargo.lock b/Cargo.lock index 4932384..97a2361 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -649,6 +649,7 @@ dependencies = [ "regex", "reqwest", "serde", + "serde_json", "tokio", "toml 0.8.2", "tracing", @@ -1166,9 +1167,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.94" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ "itoa", "ryu", diff --git a/Cargo.toml b/Cargo.toml index 03d2287..9c17f4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ dirs = "5.0.1" toml = "0.8.2" regex = "1.9.6" serde = { version = "1.0.188", features = ["derive"] } +serde_json = "1.0.107" reqwest = { version = "0.11.22", features = ["json"] } tokio = { version = "1.33.0", features = ["rt-multi-thread"] } universal-config = { version = "0.4.3", default-features = false, features = ["toml"] } diff --git a/src/main.rs b/src/main.rs index 833eb2a..7fac151 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,14 +106,14 @@ impl<'a> Service<'a> { drpc.on_ready(move |_| { event_tx .try_send(ServiceEvent::Ready) - .expect("channel to be open") + .expect("channel to be open"); }); drpc.on_error(move |err| { if err .event .get("error_message") - .and_then(|v| v.as_str()) + .and_then(serde_json::value::Value::as_str) .map(|str| str == "Io Error") .unwrap_or_default() { @@ -133,7 +133,7 @@ impl<'a> Service<'a> { } fn start(&mut self) { - self.drpc.start() + self.drpc.start(); } async fn update_state(&mut self, mpd: &MultiHostClient<'a>, status: &Status) { diff --git a/src/mpd_conn.rs b/src/mpd_conn.rs index c698bd4..728a125 100644 --- a/src/mpd_conn.rs +++ b/src/mpd_conn.rs @@ -25,8 +25,8 @@ pub fn get_token_value(song: &Song, status: &Status, token: &str) -> String { "disc" => try_get_first_tag(song.tags.get(&Tag::Disc)), "genre" => try_get_first_tag(song.tags.get(&Tag::Genre)), "track" => try_get_first_tag(song.tags.get(&Tag::Track)), - "duration" => return format_time(get_duration(status)), - "elapsed" => return format_time(get_elapsed(status)), + "duration" => return get_duration(status).map_or_else(|| String::from("N/A"), format_time), + "elapsed" => return get_elapsed(status).map_or_else(|| String::from("N/A"), format_time), _ => Some(token), } .unwrap_or("unknown") @@ -40,13 +40,17 @@ pub fn get_timestamp(status: &Status, mode: TimestampMode) -> ActivityTimestamps .expect("Failed to get system time") .as_secs(); - let elapsed = get_elapsed(status); - let timestamps = ActivityTimestamps::new(); + let Some(elapsed) = get_elapsed(status) else { + return timestamps; + }; + match mode { TimestampMode::Left => { - let duration = get_duration(status); + let Some(duration) = get_duration(status) else { + return timestamps; + }; let remaining = duration - elapsed; timestamps.end(current_time + remaining) @@ -63,17 +67,11 @@ pub fn try_get_first_tag(vec: Option<&Vec>) -> Option<&str> { } /// Gets the duration of the current song -fn get_duration(status: &Status) -> u64 { - status - .duration - .expect("Failed to get duration from MPD status") - .as_secs() +fn get_duration(status: &Status) -> Option { + status.duration.map(|d| d.as_secs()) } /// Gets the elapsed time of the current song -fn get_elapsed(status: &Status) -> u64 { - status - .elapsed - .expect("Failed to get elapsed time from MPD status") - .as_secs() +fn get_elapsed(status: &Status) -> Option { + status.elapsed.map(|e| e.as_secs()) }