From 62a81cfaaba733fe248c91425cef757aa54db268 Mon Sep 17 00:00:00 2001 From: djairoh Date: Mon, 8 May 2023 16:41:30 +0200 Subject: [PATCH] working on refactoring some functions --- src/structs/config.rs | 18 ++++++++---------- src/update_message.rs | 2 +- src/update_players.rs | 11 ++++++----- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/structs/config.rs b/src/structs/config.rs index ca9b7f9..ae4dc32 100644 --- a/src/structs/config.rs +++ b/src/structs/config.rs @@ -21,16 +21,6 @@ impl Field { } } -// I shouldn't need this; remove when done with testing FIXME -impl Default for Field { - fn default() -> Self { - Self { - field: Default::default(), - num_chars: Default::default() - } - } -} - #[derive(Serialize, Deserialize)] pub struct Rating { pub nil: char, @@ -93,6 +83,14 @@ impl Config { pub fn priorities_to_lower(&mut self) { self.player_priorities = self.player_priorities.iter().map(|i| i.to_lowercase()).collect(); } + + pub fn find_player_priorities_idx(&self, name: &str) -> i32 { + match self.player_priorities.iter() + .position(|x| x.to_ascii_lowercase().eq(&name.to_ascii_lowercase())) { + Some(idx) => idx as i32, + None => i32::MAX, + } + } } fn ms(str: &str) -> String { diff --git a/src/update_message.rs b/src/update_message.rs index 0f5167f..19442c3 100644 --- a/src/update_message.rs +++ b/src/update_message.rs @@ -11,7 +11,7 @@ fn update_prefix(cfg: &Config, data: &mut Data) { data.display_prefix = char.clone(); trace!("updated prefix to {}", data.display_prefix); } else { - data.display_prefix = cfg.player_prefixes.get("default").unwrap().clone(); //todo: error handling + data.display_prefix = cfg.player_prefixes.get("default").unwrap_or(&'>').clone(); trace!("set prefix to default ({})", data.display_prefix); } } diff --git a/src/update_players.rs b/src/update_players.rs index faa4b9a..fc82221 100644 --- a/src/update_players.rs +++ b/src/update_players.rs @@ -6,7 +6,7 @@ pub fn update_players( cfg: &Config, mut data: &mut Data, ) { - let players = pf.find_all().unwrap(); //todo: error handling + let players = pf.find_all().unwrap_or(Vec::new()); if players.is_empty() { data.current_player = None; } else { @@ -14,13 +14,14 @@ pub fn update_players( for player in players { if cfg.player_priorities.contains(&player.identity().to_owned().to_ascii_lowercase()) { let name = player.identity(); - let idx = cfg.player_priorities.iter().position(|x| x.to_ascii_lowercase().eq(&name.to_ascii_lowercase())).unwrap() as i32; //todo: move to function in config; error handling - let status = player.get_playback_status().unwrap(); //todo: error handling - match status { + let idx = cfg.find_player_priorities_idx(name); + if let Ok(status) = player.get_playback_status() { + match status { mpris::PlaybackStatus::Playing => active[0].push((idx, name.to_owned())), mpris::PlaybackStatus::Paused => active[1].push((idx, name.to_owned())), mpris::PlaybackStatus::Stopped => active[2].push((idx, name.to_owned())), - }; + }; + } } } if !active[0].is_empty() {