From be7f5d4dbc41d4267ddded3c7e00a79079de99e7 Mon Sep 17 00:00:00 2001 From: Jacob Casper Date: Thu, 17 Aug 2023 12:21:08 -0500 Subject: [PATCH] Spotify dbus events no longer always include Metadata and Playback Where these could previously be referenced unconditionally, the code now panics due to spotify not always sending these in the dbus properties changed when songs change. Add more match clauses when pulling them from the hash map. --- src/main.rs | 72 +++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 800cf54..d6beac7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,41 +95,49 @@ fn main() -> Result<(), Box> { let spotify_match_map = arc_locked_xset_map.clone(); let _ = spotify_proxy.match_signal( move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { - pc.changed_properties["Metadata"] - .0 - .as_iter() - .and_then(|mut iter| { - // Iterating over a RefArg goes key, value, key, value... Insane honestly. - while let Some(key) = iter.next() { - let key_str = key.as_str()?; - let value = iter.next(); + match pc.changed_properties.get("Metadata") { + Some(variant) => { + variant.0.as_iter().and_then(|mut iter| { + // Iterating over a RefArg goes key, value, key, value... Insane honestly. + while let Some(key) = iter.next() { + let key_str = key.as_str()?; + let value = iter.next(); - match key_str { - "xesam:artist" => { - // Variant holding a variant that should just be a Vec<&str> I - // believe. This is _the recommended_ way to do this by the author. - let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?; - let artist = inner_value.as_str()?; - update_map(spotify_match_map.clone(), "artist", artist); + match key_str { + "xesam:artist" => { + // Variant holding a variant that should just be a Vec<&str> I + // believe. This is _the recommended_ way to do this by the author. + let inner_value = + value?.as_iter()?.next()?.as_iter()?.next()?; + let artist = inner_value.as_str()?; + update_map(spotify_match_map.clone(), "artist", artist); + } + "xesam:title" => { + let title = value?.as_iter()?.next()?.as_str()?; + update_map(spotify_match_map.clone(), "title", title); + } + _ => (), } - "xesam:title" => { - let title = value?.as_iter()?.next()?.as_str()?; - update_map(spotify_match_map.clone(), "title", title); - } - _ => (), } - } - Some(()) - }); - let playback_status = &pc.changed_properties["PlaybackStatus"].0; - update_map( - spotify_match_map.clone(), - "playback_status", - match &*playback_status.as_str().unwrap_or("") { - "Playing" => "🔊", - _ => "🔈", - }, - ); + Some(()) + }); + } + None => (), + } + match &pc.changed_properties.get("PlaybackStatus") { + Some(variant) => { + let playback_status = &variant.0; + update_map( + spotify_match_map.clone(), + "playback_status", + match &*playback_status.as_str().unwrap_or("") { + "Playing" => "🔊", + _ => "🔈", + }, + ); + } + None => (), + } true }, ); -- 2.20.1